#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <time.h>
#include <string.h>
void getCommand(char* cmd, char** arg_list)
{
pid_t child_pid;
child_pid = fork();
if (child_pid == 0)
{
execvp (cmd, arg_list);
fprintf(stderr, "error");
abort();
}
}
int main(void)
{
printf("Type the command\n");
char *arg_list[] = {NULL, NULL, NULL, NULL, NULL, NULL, NULL};
char cmd[20];
char delim[2] = " ";
char *token;
scanf("%[^\n]", cmd);
token = strtok(cmd, delim);
while (token != NULL)
{
arg_list[0] = token;
token = strtok(NULL, cmd);
}
getCommand (arg_list[0], arg_list);
return 0;
}
What I'm trying to achieve here is I want to read user input, which should be a linux command, and then execute the command. It seems that I can't use strtok to split my string. I'm kinda lost, thanks for the help.
waiton the child.