-
Notifications
You must be signed in to change notification settings - Fork 0
/
oksh.c
116 lines (96 loc) · 2.07 KB
/
oksh.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
#include "header.h"
// global 変数
child_t *head = NULL;
child_t *tail = NULL;
int sum_child = 0; // tail number and running process
// foregroundになっているプロセスの情報を保存
pid_t foreground_pid;
bool foreground_check = false;
char *env[512];
int env_num;
int main(void)
{
// jobs
int status;
// Internal command
char *internal_command[] = {"quit", "q"};
if (signal(SIGINT, signal_sigint) == SIG_ERR)
{
exit(EXIT_FAILURE);
}
if (signal(SIGCHLD, signal_fin) == SIG_ERR)
{
exit(EXIT_FAILURE);
}
env_num = get_path(env);
while (1)
{
// char **argv = (char **)calloc(10, sizeof(char *));
// char **argv = (char **)malloc(10 * sizeof(char *));
char **argv = malloc(10 * sizeof(char *));
printf("> ");
int argc = read_cmd(argv);
if (argc < 1)
{
continue;
}
// & check and & delete
bool background_flag = false;
if (argc > 1)
{
if (strncmp(argv[argc - 1], "&", 2) == 0)
{
argv[argc - 1] = NULL;
background_flag = true;
}
}
// inner command start
if (strncmp(argv[0], internal_command[0], 5) == 0 ||
strncmp(argv[0], internal_command[1], 2) == 0)
{
break;
}
if (strncmp(argv[0], "fg", 3) == 0)
{
if (sum_child > 0)
{
fg(argv[1]);
}
continue;
}
if (strncmp(argv[0], "jobs", 5) == 0)
{
jobs();
continue;
}
// inner command end
#if FLAG
// create full path
if (strncmp(argv[0], "/", 1) != 0)
{
if (create_full_path(argv) == -1)
{
puts("not found");
continue;
}
}
#else
#endif
// generate child process
pid_t pid = execute(argv);
printf("start %s (pid: %d)\n", argv[0], pid);
add_process(pid, background_flag);
// wait child process Foreground
if (!background_flag)
{
foreground_check = true;
foreground_pid = pid;
// Foreground do
pid = waitpid(pid, &status, 0);
foreground_check = false;
}
free(*argv);
free(argv);
}
return 1;
}