How to use execvp() to execute a command

The prototype of execvp is

int execvp(const char *file, char *const argv[]);

It expects a pointer to char as the first argument, and a NULL-terminated pointer to an array of char*. You are passing completely wrong arguments.

You are passing a single char as first argument and a char* as the second.

Use execlp instead:

int execlp(const char *file, const char *arg, ...
              /* (char  *) NULL */);

So

char *token = strtok(input," \n");

if(token == NULL)
{
    fprintf(stderr, "only delimiters in line\n");
    exit(1);
}

if(execlp(token, token, NULL) < 0){
    fprintf(stderr, "Error in execution: %s\n", strerror(errno));
    exit(1);
}

Also the convention in UNIX is to print error messages to stderr and a process with an error should have an exit status other than 0.

Leave a Comment