Niklas Rosencrantz
Niklas Rosencrantz

Reputation: 26652

Why is extended grep not working?

When I try extended grep like this it does not work.

const char *grep[] = { "grep", "-E", "'JOBS|COMPIZ'" };

If I do it just for one string without single quotes then it works. Why? Why can't I build up arguments to extended grep like above? The following with just one string is working.

const char *grep[] = { "grep", "-E", "JOBS" };

My program should do printenv | sort | grep <parameter-list> | less and if no arguments to main then the program should do printenv | sort | less. I already achieved the latter functionality and now I need to achieve the grep of parameter list but I can't seem to do extended grep from within C code.

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

struct command
{
    const char **argv;
};

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;
}

int
fork_pipes (int n, struct command *cmd)
{
    int i;
    pid_t pid;
    int in, fd [2];

    /* The first process should get its input from the original file descriptor 0.  */
    in = 0;

    /* Note the loop bound, we spawn here all, but the last stage of the pipeline.  */
    for (i = 0; i < n - 1; ++i)
    {
        pipe (fd);

        /* f [1] is the write end of the pipe, we carry `in` from the prev iteration.  */
        spawn_proc (in, fd [1], cmd + i);

        /* No need for the write and of the pipe, the child will write here.  */
        close (fd [1]);

        /* Keep the read end of the pipe, the next child will read from there.  */
        in = fd [0];
    }

    /* Last stage of the pipeline - set stdin be the read end of the previous pipe
       and output to the original file descriptor 1. */
    if (in != 0)
        dup2 (in, 0);

    /* Execute the last stage with the current process. */
    return execvp (cmd [i].argv [0], (char * const *)cmd [i].argv);
}

int
main (int argc, char ** argv)
{
    printf("in main...");
    int i;

    if (argc == 1) {
        const char *printenv[] = { "printenv", 0};
        const char *sort[] = { "sort", 0 };
        const char *less[] = { "less", 0 };

        struct command cmd [] = { {printenv}, {sort}, {less} };
        return fork_pipes (3, cmd);
    }
    if (argc > 1) {
    /*char *tmp = argv[1];
    sprintf(tmp, "%s%s", "'", tmp);*/
        for( i=1; i<argc-1; i++)
        {
           /* tmp = "%s%s%s", tmp, "\\|", argv[i];
            printf("tmp:%s", tmp);
        sprintf(tmp, "%s%s%s", tmp, "|", argv[i]);
        sprintf(tmp, "%s%s", tmp, "'");*/
        }

        const char *printenv[] = { "printenv", 0};
        const char *grep[] = { "grep", "-E", "JOB" };
        const char *sort[] = { "sort", 0 };
        const char *less[] = { "less", 0 };

        struct command cmd [] = { {printenv}, {grep}, {sort}, {less} };
        return fork_pipes (4, cmd);
    }




}

Upvotes: 1

Views: 97

Answers (1)

choroba
choroba

Reputation: 241758

Don't use single quotes in the argument. They are needed only on the command line to prevent shell from interpreting the vertical bar.

Upvotes: 4

Related Questions