#include <unistd.h>
#include <string.h>
#include <sys/wait.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <stdio.h>
#include <stdlib.h>
#define MAX_LINE 1024
void run_shell(char * cmd){
char * str = cmd;
int size = strlen(str);
char blank[] = " ";
char * token = strtok(str, blank);
char * argv[3];
int i = 0;
while(token != NULL){
argv[i] = token;
i++;
token = strtok(NULL, blank);
}
argv[i] = NULL;
pid_t pid;
if((pid = fork()) == 0){
if(execvp(argv[0], argv) < 0){
perror("Invalid Command!\n");
exit(0);
}
}
else{
if(waitpid(pid, NULL, 0) < 0){
perror("Some Problem Program!\n");
exit(0);
}
}
printf("\n");
}
int main(){
char comand[MAX_LINE];
char * username;
printf("Eter your name : ");
scanf("%s", username);
while(1){
char cwd[MAX_LINE];
getcwd(cwd, sizeof(cwd));
printf("%s %s$ > ", username, cwd);
fgets(comand, MAX_LINE, stdin);
printf("\n");
run_shell(comand);
}
}
I'm making a small shell. But when I enter a command, the execvp function causes an error. When I print out argv[], I can see that it has been tokenized without any problems. It's hard to guess the my code internal movement because I don't know much about the standard input/output. What's the problem? What should I fix?