Shell命令编写——write,与终端对话

#include 
#include 
#include 
#include 

/*
 * 使用格式:write user
 * 与终端用户的对话
 *
 */

int main(int ac,char *av[]){

	int fd;
	char buf[50];

	//参数的判断
	if(ac != 2){
		fprintf(stderr,"usage:write0 ttyname\n");
		exit(1);
	}
	
	//打开设备文件
	fd = open(av[1],O_WRONLY);
	if(fd == -1){
		perror(av[1]);
		exit(1);
	}

	//循环输入数据直到终止
	while(fgets(buf,50,stdin)!=NULL){
		if(write(fd,buf,strlen(buf))==-1)
			break;
	}
	close(fd);
}

你可能感兴趣的:(Unix系统编程)