forked from sidneyriffic/simple_shell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcmdcall.c
214 lines (210 loc) · 4.2 KB
/
cmdcall.c
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
#include "shell.h"
/**
* checkpath - checks the path
* @av: arguments
* Return: 1
*/
int checkpath(char *av[])
{
char *path, *pathptr, *pathvar, *ptr, *pathenv = "PATH", *linect;
int pathlen, cmdlen;
#ifdef DEBUGMODE
printf("In checkpath\n");
#endif
for (ptr = av[0], cmdlen = 0; *ptr != 0; ptr++)
cmdlen++;
pathvar = _getenv(pathenv);
if (pathvar != pathenv)
{
pathenv = pathvar;
while (*pathvar != 0)
{
#ifdef DEBUGMODE
printf("in loop pathvar:%s:*pathvar:%c\n", pathvar, *pathvar);
#endif
for (pathlen = 0, ptr = pathvar; *ptr != 0 && *ptr != ':'; ptr++)
pathlen++;
path = malloc(sizeof(char) * (cmdlen + pathlen + 2));
if (path == NULL)
{
free(pathenv);
return (-1);
}
pathptr = path;
while (*pathvar != ':' && *pathvar != 0)
*pathptr++ = *pathvar++;
if (pathptr != path)
*pathptr++ = '/';
ptr = av[0];
while (*ptr != 0)
*pathptr++ = *ptr++;
*pathptr = 0;
if (!access(path, F_OK))
{
#ifdef DEBUGMODE
printf("Found path:%s\n", path);
#endif
pathlen = cmdcall(av, path);
free(path);
free(pathenv);
return (pathlen);
}
free(path);
if (*pathvar == ':')
pathvar++;
}
}
linect = itos(linecount(0));
path = getsvar("0");
fprintstrs(2, path, ": ", linect, ": ", av[0], ": not found\n", NULL);
free(path);
free(linect);
if (pathenv != pathvar)
free(pathenv);
return (127);
}
/**
* cmdcall - calls commands
* @av: arguments
* @cmd: command
* Return: retval
*/
int cmdcall(char *av[], char *cmd)
{
pid_t command;
int status;
char *linect, *dolz;
#ifdef DEBUGMODE
printf("In cmdcall av[0]:%s\n", av[0]);
#endif
environ = getallenv();
if (environ == NULL)
return (-1);
#ifdef DEBUGMODE
printf("Forking\n");
#endif
command = fork();
#ifdef DEBUGMODE
printf("command:%d\n", command);
fflush(stdout);
#endif
if (command == 0)
{
#ifdef DEBUGMODE
printf("Executing %s\n", av[0]);
#endif
if (execve(cmd, av, *(getenviron())) == -1)
{
if (!access(cmd, F_OK))
{
printerr(NULL);
exit(126);
}
else
{
linect = itos(linecount(0));
dolz = getsvar("0");
fprintstrs(2, dolz, ": ", linect, ": ", cmd, ": not found\n", NULL);
free(dolz);
free(linect);
exit(127);
}
exit(1);
}
#ifdef DEBUGMODE
printf("Command done\n");
#endif
}
else
{
wait(&status);
}
#ifdef DEBUGMODE
printf("Status %d\n", status);
#endif
free(environ);
return (status);
}
/**
* builtincall - calls builtin commands
* @av: arguments
* Return: retval
*/
int builtincall(char *av[])
{
int retval = 0;
char *retstr;
if (av[0] == NULL)
return (0);
#ifdef DEBUGMODE
printf("In builtincall %p\n%s\n", av[0], av[0]);
printf("av[1]:%s\n", av[1]);
#endif
if (!_strcmp(av[0], "exit"))
{
if (av[1] != NULL)
if (av[1][0] >= '0' && av[1][0] <= '9')
{
retval = _atoi(av[1]);
exitcleanup(av);
exit_hist();
exit(retval);
}
else
{
printerr(": exit: Illegal number: ");
fprintstrs(STDERR_FILENO, av[1], "\n", NULL);
retval = 2;
}
else
{
retstr = getsvar("?");
retval = _atoi(retstr);
free(retstr);
exitcleanup(av);
exit_hist();
exit(retval);
}
}
else if (!_strcmp(av[0], "cd"))
retval = _cd(av);
/*
*
* else if (!_strcmp(av[0], "getenv"))
* retval = !printf("%s\n", _getenv(av[1]));
*/
else if (!_strcmp(av[0], "history"))
retval = print_hist();
else if (!_strcmp(av[0], "help"))
retval = help(av[1]);
else if (!_strcmp(av[0], "env"))
retval = _printenv();
else if (!_strcmp(av[0], "setenv"))
retval = _setenv(av[1], av[2]);
else if (!_strcmp(av[0], "unsetenv"))
retval = _unsetenv(av[1]);
else if (!_strcmp(av[0], "alias"))
retval = aliascmd(av);
else if (!_strcmp(av[0], "unset"))
retval = unsetsvar(av[1]);
else if (!_strcmp(av[0], "unalias"))
retval = unsetalias(av[1]);
else if (av[0][0] != '/' &&
!(av[0][0] == '.' && (av[0][1] == '/' ||
(av[0][1] == '.' && av[0][2] == '/'))))
retval = checkpath(av);
else
retval = cmdcall(av, av[0]);
#ifdef DEBUGMODE
printf("After call back in builtin retval:%d\n", retval);
#endif
if (retval % 256 == 0)
retval /= 256;
retstr = itos(retval % 128);
#ifdef DEBUGMODE
printf("Status string:%s\n", retstr);
#endif
setsvar("?", retstr);
free(retstr);
return (retval);
}