I'm trying to write a shell, and part of its construction is executing code from a user-inputted string (buffer). However, when I attempt to execvp the string with additional inputs (ae. echo a), it always screws the pooch and returns -1. I'm at a loss as to why. Here's the relevant pieces:
char * buffer = calloc(100, sizeof(char));
...
fgets(buffer, 100, stdin);
buffer[strlen(buffer) - 1] = 0; // necessary because of a newline inserted by fgets
...
cmd = strsep(&buffer, " ");
char * str = malloc(50 * sizeof(char));
strcat(str, "./");
strcat(str, cmd);
strcat(str, ".out");
...
i = execvp(str, (char * *) buffer);
The argument buffer
is wrong. The second argument of execvp
is an array of pointers. With this cast, you are hidding a compiler warning, but it does not work.