【问题标题】:c execvp grep environment variables, when grep does not find anythingc execvp grep 环境变量,当 grep 没有找到任何东西时
【发布时间】:2015-12-06 14:59:41
【问题描述】:

我正在尝试构建一个研究环境变量的小型 c 程序。 当我使用参数运行程序时,它应该模仿

printenv | grep 参数列表 |排序 |少

所以我使用管道连接所有这些输出,但现在问题是当我输入一个不存在的环境变量时,程序仍然运行所有内容,理想情况下我希望它在 grep 之后退出。

这是我使用 execvp 和 perror 执行 grep 的代码,但 perror 仅在 execvp 不起作用时出现。

我从手册页中知道 grep 在选择一行或多行时给出 0,在没有选择行时给出 1,在发生错误时给出 >1。当 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);}

【问题讨论】:

  • 您也必须发布其余代码。仅提供这些信息并不容易。
  • grep(或通过exec*()运行的任何程序)的返回码将可通过调用wait*()提供给调用exec*()的进程的父进程。跨度>

标签: c linux bash


【解决方案1】:

如果 execvp 失败,将调用 _exit。

execvp 的手册页:

返回值 如果任何 exec() 系列函数返回,则将发生错误。返回值为-1,设置全局变量errno表示错误。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-09-16
    • 1970-01-01
    • 1970-01-01
    • 2021-10-29
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多