-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathxsh_execute_cmd.c
More file actions
101 lines (91 loc) · 2.34 KB
/
xsh_execute_cmd.c
File metadata and controls
101 lines (91 loc) · 2.34 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
#include <xsh.h>
#include <xsh_cmd.h>
#include <xsh_process.h>
#include <dirent.h>
#include <string.h>
#include <sys/types.h>
#include <sys/wait.h>
#include <unistd.h>
#include <sys/stat.h>
#include <stdio.h>
#include <stdlib.h>
static boolean xsh_is_internal(char *cmd) {
if(
strcmp(cmd, "exit") == 0 ||
strcmp(cmd, "fg") == 0 ||
strcmp(cmd, "bg") == 0 ||
strcmp(cmd, "jobs") == 0
) {
return TRUE;
} else {
return FALSE;
}
}
static int xsh_execute_internal(char *cmd, char *argv[]) {
if(strcmp(cmd, "exit") == 0) {
xsh_exit();
} else if(strcmp(cmd, "jobs") == 0) {
xsh_process_table *ptr = process_table;
xsh_process_entry *entry;
while(ptr != NULL) {
printf("[%d]: %d %d", ptr->entry.entry_id, ptr->entry.pid, ptr->entry.state);
ptr = ptr->next;
}
}
return 0;
}
static int xsh_execute_external(struct str_llist * list, char *cmd, boolean fg, char *argv[]) {
pid_t pid;
int retval;
struct str_llist * ptr = NULL;
for(ptr = list; ptr != NULL; ptr = ptr->next){
struct stat sb;
struct dirent *pDirent;
DIR *pDir;
pDir = opendir(ptr->str);
if(pDir == NULL){
return -1;
}
char *path = ptr->str;
char *buf = (char *) malloc(strlen(path) + strlen(cmd) + 2);
buf[0] = '\0';
buf = strcat(buf, path);
if(buf[strlen(buf) - 1] != '/') {
buf = strcat(buf, "/");
}
while((pDirent = readdir(pDir)) != NULL) {
if(strcmp(pDirent->d_name, cmd) == 0) {
buf = strcat(buf, cmd);
if(stat(buf, &sb) == 0 && sb.st_mode & S_IXUSR) {
pid = fork();
xsh_process_entry prc;
prc.fg = fg;
prc.pid = getpid();
//xsh_create_process_entry(&prc);
if(pid <= 0) {
execv(buf, argv);
exit(0);
} else {
if(fg) {
waitpid(pid, &retval, 0);
//xsh_delete_process_entry(pid);
}
}
}else{
closedir (pDir);
break;
}
}
}
free(buf);
}
return 0;
}
//linked list of paths, command, size of array, array of parameters
int xsh_execute_cmd(struct str_llist * list, char * cmd, boolean fg, char ** argv) {
if(xsh_is_internal(cmd)) {
return xsh_execute_internal(cmd, argv);
} else {
return xsh_execute_external(list, cmd, fg, argv);
}
}