-
Notifications
You must be signed in to change notification settings - Fork 0
/
builtins.c
180 lines (174 loc) · 3.99 KB
/
builtins.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
#include "shell.h"
#include <procfs.h>
int job_is_completed(job *j);
int job_is_stopped(job *j);
void put_job_in_foreground(job *j, int cont);
void put_job_in_background(job *j, int cont);
void wait_for_job(job *j);
void update_status();
void mark_job_as_running(job *j)
{
process *p;
for (p = j->first_process; p; p = p->next)
p->stopped = 0;
j->notified = 0;
}
void continue_job(job *j, int foreground)
{
int stopped = job_is_stopped(j);
mark_job_as_running(j);
if (foreground)
put_job_in_foreground(j, stopped);
else
put_job_in_background(j, stopped);
}
void print_command(job *j)
{
int procinfofd;
process *p;
struct psinfo procInfo;
for (p = j->first_process; p; p = p->next)
{
char filename[64];
sprintf(filename, "/proc/%d/psinfo", p->pid);
if ((procinfofd = open(filename, O_RDONLY)) == -1)
{
perror(filename);
exit(1);
}
if (read(procinfofd, &procInfo, sizeof(procInfo)) < 0)
{
perror("read");
exit(1);
}
close(procinfofd);
printf("%s", procInfo.pr_psargs);
if (p->next)
printf(" | ");
else
putchar('\n');
}
}
void jobs_info()
{
job *j;
for (j = first_job; j; j = j->next)
{
if (j->builtin || !j->launched)
continue;
printf("# [%d]: ", j->pgid);
if (job_is_stopped(j) && !job_is_completed(j))
{
printf("stopped ");
print_command(j);
j->notified = 1;
}
else if (!job_is_completed(j))
{
printf("active ");
print_command(j);
}
else
{
printf("completed\n");
j->notified = 1;
}
}
}
/* flag == 0: find last stopped background process
flag == 1: find last backgrounded process */
job *find_job(process *p, int flag)
{
if (p->argv[1] == 0)
{
/* w/o argument: find the most recent backgrounded job */
job *j, *jRecorded = 0;
for (j = first_job; j; j = j->next)
{
if (j->builtin || !j->launched)
continue;
if (flag == 0)
{
if (!job_is_completed(j) && job_is_stopped(j))
{
jRecorded = j;
}
}
else
{
if (!job_is_completed(j))
{
jRecorded = j;
}
}
}
return jRecorded;
}
else
{
job *j, *jRecorded = 0;
for (j = first_job; j; j = j->next)
{
if (j->builtin || !j->launched)
continue;
char pgid[20];
sprintf(pgid, "%d", j->pgid);
if (strcmp(pgid, p->argv[1]) == 0)
{
if (flag == 0)
{
if (!job_is_completed(j) && job_is_stopped(j))
{
jRecorded = j;
}
}
else
{
if (!job_is_completed(j))
{
jRecorded = j;
}
}
}
}
return jRecorded;
}
}
void fg_builtin(process *p)
{
job *j = find_job(p, 1);
if (j)
{
fprintf(stderr, "[%d]: continued in foreground\n", j->pgid);
continue_job(j, 1);
}
else
{
fprintf(stderr, "no such job\n");
}
}
void bg_builtin(process *p)
{
job *j = find_job(p, 0);
if (j)
{
fprintf(stderr, "[%d]: continued in background\n", j->pgid);
continue_job(j, 0);
}
else
{
fprintf(stderr, "no such job\n");
}
}
void cd_builtin(process *p)
{
if (p->argv[1] == 0)
{
fprintf(stderr, "cd w/o args is not allowed :(\n");
return;
}
if (chdir(p->argv[1]) == -1)
{
perror("chdir");
}
}