您的位置:首页 > 运维架构 > Shell

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

2011-11-30 14:17 369 查看
#include <stdio.h>
#include <fcntl.h>
#include <stdlib.h>
#include <string.h>

/*
* 使用格式: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);
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: