设置>>使用C在shell中重定向的文件描述符

时间:2017-11-26 20:09:57

标签: c bash shell file-descriptor io-redirection

我正在尝试识别'>>'在命令行上指定的重定向运算符,并执行>>的操作运算符在普通shell中执行(即将文本附加到现有文件中)。下面提供的代码不会将新文本附加到现有文件中。它会覆盖现有文件的内容。有人能帮助我吗?提前谢谢。

/*-----------------------------------------------*/
/* Setup >> file descriptor per redirection. */
/*-----------------------------------------------*/
if (ofn != NULL) {                /* ofn is output file name */
int fd = open(ofn,O_CREAT|O_WRONLY|O_APPEND,0700);
unlink(ofn);

if (fd < 0) {
    write(2,CANNOTAPPEND,strlen(CANNOTAPPEND));
    exit(1);
}
close(1);
dup(fd);
close(fd);
}
execve(path,eveargs,environ);
write(2,CANNOTEXEC,strlen(CANNOTEXEC));
exit(1);

0 个答案:

没有答案