Hello I need to run a program using execvp()
the thing is that im getting a sting as argument
and i cant seem to match the syntax of the command to work properly here is the code:
int executeUserCommand(char *command, int runInBackground) {
pid_t pid;
int status;
pid = fork();
if (pid == 0) {
//child process
execvp(command, &command);
exit(1);
} else {
//perent
if (!runInBackground) {
waitpid(pid, &status, 0);
return WEXITSTATUS(status);
}
}
return 0;
}
and i am using executeUserCommand("./test 1 2 3", 0)
the thing is that the program is running but without the arguments..
what is the problem and how i solve it?
EDIT: i have added two fuctions to help me with the spliting and its still not working.
char *getCommand(char *commandAndArguments) {
char command[256];
memset(command,0,MAX_LENGTH_OF_LINE);
sscanf(commandAndArguments,"%s ", command);
//strncpy(command, commandAndArguments, command - commandAndArguments);
return command;
}
void commandToArrguments(char *stringToSplit) {
const char *p = stringToSplit;
int n, len;
int i = 0;
for (n = 0; n < MAX_NUMBER_OF_COMMANDS; n++) {
for (len=0; len < MAX_LENGTH_OF_LINE-1; len++) {
if (p[0] == '\0') {
break;
}
if (p[0] == ' ') {
p += 1;
break;
}
commandArgumnets[n][len] = *p;
p++;
}
commandArgumnets[n][len] = '\0';
}
}
int executeUserCommand(char *command, int runInBackground) {
pid_t pid;
int status;
pid = fork();
char *commandToExecute = getCommand(command);
if (pid == 0) {
//child process
execvp(commandToExecute, commandArgumnets);
exit(1);
} else {
//perent
if (!runInBackground) {
waitpid(pid, &status, 0);
return WEXITSTATUS(status);
}
}
return 0;
}
commandArgumnets
is defined as global and still not working