我正在尝试构建一个研究环境变量的小型 C 程序。 当我使用参数运行程序时,它应该模仿
printenv | grep parameterlist | sort | less
所以我使用管道连接所有这些输出,但现在的问题是当我输入一个不存在的环境变量时,程序仍然会运行所有内容,理想情况下我希望它在 grep 之后退出。
这是我使用 execvp 和 perror 执行 grep 的代码,但只有当 execvp 不起作用时才会出现 perror。
I know from the man pages that grep gives 0 when one or more lines selected and 1 when No lines where selected and >1 when an error occurred.当 grep 未选择任何行并退出程序而不继续进行 sort 和 less 时,我该如何处理?
execvp( "grep", argv); /* runs grep */
perror( "Cannot exec grep" ); exit( 1 );
/* CHILD that handles grep function.
This child executes grep.
*/
if(pid == 0)
{
if(-1 == dup2(pipa[READ],STDIN_FILENO)) /* redirect pipe1 to stdin */
{perror("Cannot dup"); exit(EXIT_FAILURE);}
if(-1 == dup2(pipa2[WRITE],STDOUT_FILENO)) /* redirect stdout to pipe2 */
{perror("Cannot dup"); exit(EXIT_FAILURE);}
if(-1 == close(pipa[WRITE])) /* Close write, we dont need it */
{perror("Cannot close pipe (write-end)[GREP]"); exit(EXIT_FAILURE);}
if(-1 == close(pipa[READ])) /* Close read on pipe1 */
{perror("Cannot close pipe (read-end)[GREP]"); exit(EXIT_FAILURE);}
if(-1 == close(pipa2[READ])) /* Close read on pipe2, we dont need it */
{perror("Cannot close pipe2 (read-end)[GREP]"); exit(EXIT_FAILURE);}
if(-1 == close(pipa2[WRITE])) /* Close write on pipe2, we we have it on stdout now */
{perror("Cannot close pipe2 (read-end)[GREP]"); exit(EXIT_FAILURE);}
if(argc > 1) /* filter or no */
{
argv[0] = "grep";
printf("%s",argv[0]);
execvp( "grep", argv); /* runs grep */
perror( "Cannot exec grep" ); exit( 1 );
}
exit(0);
}
/* PARENT */
if(argc < 2) /* No arguments? */
{
if(-1 == dup2(pipa2[WRITE],pipa[WRITE])) /* Redirect stdin to pipe2 write (which is in STDOUT_FILENO) */
{perror("Cannot dup"); exit(EXIT_FAILURE);}
}
/* Sends enivomrent data to grep */
for(i=0; envp[i] != 0; i++)
{
write(pipa[WRITE],envp[i],strlen(envp[i]));
write(pipa[WRITE],"\n",1);
}
nchildren++;
/* Create 3rd pipe */
if(pipe(pipa3) == -1)
{exit(EXIT_FAILURE);}
/* Close 1st pipe */
if(-1 == close(pipa[WRITE]))
{perror("Cannot close pipe (write-end)"); exit(EXIT_FAILURE);}
if(-1 == close(pipa[READ]))
{perror("Cannot close pipe (read-end)"); exit(EXIT_FAILURE);}
/* Close write from second pipe */
if(-1 == close(pipa2[WRITE]))
{perror("Cannot close pipe (read-end)"); exit(EXIT_FAILURE);}
/* fork again */
if((pid = fork()) == -1) /* ERROR */
{exit(EXIT_FAILURE);}
/* CHILD that handles sort function.
This child executes sort.
*/
if(pid == 0)
{
if(-1 == dup2(pipa2[READ],STDIN_FILENO)) /* redirect pipe2 read to stdin */
{perror("Cannot dup"); exit(EXIT_FAILURE);}
if(-1 == dup2(pipa3[WRITE],STDOUT_FILENO)) /* redirect stdout to pipe3 write */
{perror("Cannot dup"); exit(EXIT_FAILURE);}
if(-1 == close(pipa2[READ])) /* Close write, we have it on stdin now */
{perror("Cannot close pipe (write-end)[SORT]"); exit(EXIT_FAILURE);}
if(-1 == close(pipa3[READ])) /* Close read on pipe3, we dont need it */
{perror("Cannot close pipe (write-end)[SORT]"); exit(EXIT_FAILURE);}
if(-1 == close(pipa3[WRITE])) /* Close write on pipe3, we have it on stdout now*/
{perror("Cannot close pipe (write-end)[SORT]"); exit(EXIT_FAILURE);}
(void) execlp( "sort", "", NULL ); /* runs sort */
perror( "Cannot exec sort" ); exit( 1 );
}
/* PARENT */
if(-1 == close(pipa2[READ])) /* Close read from pipe2 */
{perror("Cannot close pipe (read-end)[PARENT]"); exit(1);}
if(-1 == close(pipa3[WRITE])) /* Close write from pipe3*/
{perror("Cannot close pipe (write-end)[PARENT]"); exit(1);}
最佳答案
如果 execvp 失败,将调用 _exit。
execvp 的手册页:
返回值 如果任何 exec() 系列函数返回,就会发生错误。返回值为-1,全局变量errno将被设置以指示错误。
关于c execvp grep环境变量,当grep没有找到任何东西时,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34118801/