So I have the following C code which asks the user to give a command (valid in unix), and then I have to take the string and split it in an array in order to execute the command that the user gave with execvp(). It compiles but the execvp doesn't seem to work. Is something wrong in the way I split the user's input in an array? PS: Some of the includes aren't neccessary but it's not the final program.
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <signal.h>
main() {
char str[64];
int i =0;
char *p = strtok(str," ");
printf("Please give me a Unix command! :\n");
gets(str);
char *array[sizeof(str)];
while (p!=NULL) {
array[i++] = p;
p = strtok (NULL, " ");
}
execvp(str ,array);
perror("execvp");
}
The output I get when I run this is:
Please give me a Unix command! :
ls -l
execvp: No such file or directory