2022-09-18 06:46:23 +00:00
|
|
|
/*
|
|
|
|
* @Author: 1ridic
|
|
|
|
* @Date: 2022-09-18 14:14:23
|
|
|
|
* @Last Modified by: 1ridic
|
|
|
|
* @Last Modified time: 2022-09-18 14:44:37
|
|
|
|
*/
|
2022-09-18 05:43:33 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <sys/types.h>
|
2022-09-18 06:46:23 +00:00
|
|
|
#include <string.h>
|
2022-09-18 05:43:33 +00:00
|
|
|
#include <unistd.h>
|
2022-09-18 06:46:23 +00:00
|
|
|
#include "builtin.h"
|
|
|
|
|
|
|
|
extern int (*builtin_func[]) (char **);
|
|
|
|
extern char* builtin_cmd[];
|
2022-09-18 05:43:33 +00:00
|
|
|
|
|
|
|
int forkExec(char **args) {
|
|
|
|
pid_t pid;
|
|
|
|
int status;
|
|
|
|
#ifdef DEBUG
|
|
|
|
printf("debug: fork enter.\n");
|
|
|
|
#endif
|
|
|
|
pid = fork();
|
|
|
|
if (pid == 0) {
|
|
|
|
/* child process */
|
|
|
|
if (execvp(args[0], args) == -1) {
|
|
|
|
perror("dish");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
} else if (pid < 0) {
|
|
|
|
/* fork error */
|
|
|
|
perror("dish");
|
|
|
|
} else {
|
2022-09-18 06:46:23 +00:00
|
|
|
extern char volatile isWaiting;
|
|
|
|
isWaiting = 1;
|
2022-09-18 05:43:33 +00:00
|
|
|
/* parent process: wait child process*/
|
|
|
|
pid=wait(&status);
|
2022-09-18 06:46:23 +00:00
|
|
|
isWaiting = 0;
|
2022-09-18 05:43:33 +00:00
|
|
|
|
|
|
|
}
|
|
|
|
return WEXITSTATUS(status);
|
|
|
|
}
|
2022-09-18 06:46:23 +00:00
|
|
|
|
|
|
|
int commandExec(char **args) {
|
|
|
|
int i;
|
|
|
|
|
|
|
|
if (args[0] == NULL) {
|
|
|
|
/* empty command */
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (i = 0; i < getBuiltinNum(); i++) {
|
|
|
|
if (strcmp(args[0], builtin_cmd[i]) == 0) {
|
|
|
|
return (*builtin_func[i])(args);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return forkExec(args);
|
|
|
|
}
|