简体   繁体   中英

Imitate “cat >” command in c (LINUX/UNIX)

I need to imitate the command: "cat > " using c with exec family.

I cant find a way to do it because it doesn't recognize the symbol '>', I've already tried using execlp but maybe my syntax is wrong.

Thanks for the help. appreciated.

The command is cat . The > is interpreted by the shell, redirecting the stdout of the command to a specific file.

Thus, you would need to connect the stdout (file descriptor 1) of the process running cat to a file:

/* emulating `cat >file` */
int fd = open("file", …);
/* use our own fd 1 */
dup2(fd, 1);
close(fd);
/* with new stdout, exec `cat` */
exec("cat");

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM