-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathshell.c
60 lines (49 loc) · 859 Bytes
/
shell.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
#include "shell.h"
int main(void)
{
char *line, **commands, **path, *path_val;
size_t n = 0;
int i;
pid_t cpid;
while(1)
{
i = 0;
commands = malloc(30);
path = malloc(50);
line = NULL;
printf("$ ");
getline(&line, &n, stdin);
line = strtok(line, "\n");
path_val = getenv("PATH");
commands[i] = strtok(line, " ");
while(commands[i])
{
i++;
commands[i] = strtok(NULL, " ");
}
i = 0;
path[i] = malloc(100);
path[i] = strtok(path_val, ":");
while(path[i])
{
path[i] = strcat(path[i], "/ls");
i++;
path[i] = malloc(100);
path[i] = strtok(NULL, ":");
}
printf("%s\n", path[0]);
cpid = fork();
if (cpid == -1)
return (-1);
else if (cpid == 0)
{
execve(commands[0], commands, NULL);
}
else
wait(NULL);
free(path);
free(commands);
free(line);
}
return (_SUCCESS);
}