-
Notifications
You must be signed in to change notification settings - Fork 0
/
Myshell.c
54 lines (50 loc) · 1007 Bytes
/
Myshell.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
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <unistd.h>
#include <sys/wait.h>
#include <readline/readline.h>
char **read_input(char *input)
{
char **cmd = malloc(8 * sizeof(char *));
int index = 0;
char *parsed = strtok(input, " ");
while (parsed != NULL)
{
cmd[index] = parsed;
index++;
parsed = strtok(NULL, " ");
}
cmd[index] = NULL;
return cmd;
}
int main()
{
int st;
char **cmd;
char *input;
pid_t child_pid;
while (1)
{
input = readline("myshell> ");
cmd = read_input(input);
if (!cmd[0])
{
free(input);
free(cmd);
continue;
}
child_pid = fork();
if (child_pid == 0)
{
execvp(cmd[0], cmd);
printf("No command\n");
}
else
waitpid(child_pid, &st, WUNTRACED);
free(input);
free(cmd);
}
return 0;
}