-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsuper_simple_shell.c
78 lines (71 loc) · 1.13 KB
/
super_simple_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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <string.h>
int main(void)
{
char *command;
size_t size = 0;
char *tok;
char **argv;
int i;
pid_t child_check;
int status;
int check;
while (1)
{
write(1, "Enter full path command$ ", 25);
command = malloc(sizeof(char) * 1024);
argv = malloc(sizeof(char) * 1024);
check = getline(&command, &size, stdin);
if (check == -1)
{
if (check == EOF)
{
write(1, "\n", 1);
free(argv);
free(command);
return (1);
}
perror("Error");
free(argv);
free(command);
return (1);
}
for (i = 0; command[i] != 0; i++)
{
}
command[i - 1] = '\0';
tok = strtok(command, " ");
for (i = 0; tok != 0; i++)
{
argv[i] = tok;
tok = strtok(NULL, " ");
}
argv[i] = NULL;
child_check = fork();
if (child_check == -1)
{
perror("Error");
}
if (child_check == 0)
{
if(execve(argv[0], argv, NULL) == -1)
{
perror("Error");
free(argv);
free(command);
exit(98);
}
}
else
{
wait(&status);
}
}
free(argv);
free(command);
return (0);
}