PDA

View Full Version : execvp in C help


Phoncible
01-24-2010, 05:04 AM
trying to read in a command from a user and then execute using execvp (for a linux machine)

char* cmd[4];
printf("what command\n");
scanf("%s",&cmd[0]);
cmd[1] = NULL;
execvp(cmd[0],cmd);


This doesn't work. Trying w/ the simple command of "ls" which should just show the contents of the current directory in linux, but it just prints a blank line.
Have also tried scanf("%s",cmd) and other variations along those lines.
Tried w/ getline and it just printed out junk.

Any help with this would be useful. Thanks

oracleguy
01-24-2010, 05:18 PM
char* cmd[4];
printf("what command\n");
scanf("%s", &cmd);
execvp(cmd, NULL);


Also remember if you type more than 3 characters it will not leave enough room for the null so when the string is read a bunch of garbage will also be read until a null is found. So you might want to increase the size of the array.