我写了一个小程序(带有来自SO的代码),方便了printenv | sort | less
printenv | sort | less
printenv | sort | less
。 现在我想实现error handling,我从execvp开始。 它只是检查返回值还有什么? AFAIK我只检查返回值,如果它在此函数中为0则return execvp (cmd [i].argv [0], (char * const *)cmd [i].argv);
。 那是对的吗?
#include #include #include #include #include #include struct command { const char **argv; }; /* Helper function that spawns processes */ int spawn_proc (int in, int out, struct command *cmd) { pid_t pid; if ((pid = fork ()) == 0) { if (in != 0) { dup2 (in, 0); close (in); } if (out != 1) { dup2 (out, 1); close (out); } return execvp (cmd->argv [0], (char * const *)cmd->argv); } return pid; } /* Helper function that forks pipes */ int fork_pipes (int n, struct command *cmd) { int i; int in, fd [2]; for (i = 0; i 1) { /* I'd like an argument */ if (strncmp(argv[1], "cd", 2) && strncmp(argv[1], "exit", 2)) { char *tmp; int len = 1; for( i=1; i<argc; i++) { len += strlen(argv[i]) + 2; } tmp = (char*) malloc(len); tmp[0] = '