-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathshell.c
More file actions
190 lines (180 loc) · 4.69 KB
/
shell.c
File metadata and controls
190 lines (180 loc) · 4.69 KB
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
181
182
183
184
185
186
187
188
189
190
#include <stdio.h>
#include <ctype.h>
#include <unistd.h>
#include <stdlib.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/dir.h>
#include <string.h>
#include <signal.h>
#include "command.h"
#include "init.h"
#include "list.h"
#include "pwd.h"
#include "echo.h"
#include "cd.h"
#include "globalVar.h"
#define makeblue printf("\033[1;34m")
#define makedef printf("\033[0m")
#define clear() printf("\033[H\033[J")
int fin = 0;
char *trimwhitespace10(char *str) {
char *end;
while (isspace((unsigned char)*str)) str++;
end = str + strlen(str) - 1;
while (end > str && isspace((unsigned char)*end)) end--;
end[1] = '\0';
return str;
}
char *getStatusID(int id) {
char *pid = (char *)calloc(1, sizeof(char) * 100);
sprintf(pid, "%d", id);
// printf("%d", id);
// printf("%s\n", pid);
char ad1[100] = "/proc/", ad2[100] = "/proc/", ad3[100] = "/proc/";
for (int i = 0; i < strlen(pid); i++) {
ad3[i + 6] = pid[i];
}
{
ad3[strlen(pid) + 6] = '/';
ad3[strlen(pid) + 7] = 's';
ad3[strlen(pid) + 8] = 't';
ad3[strlen(pid) + 9] = 'a';
ad3[strlen(pid) + 10] = 't';
ad3[strlen(pid) + 11] = 'u';
ad3[strlen(pid) + 12] = 's';
ad3[strlen(pid) + 13] = '\0';
}
char pinf[1000000];
int x = 0;
{
FILE *status = fopen(ad3, "r");
while (1) {
char c = fgetc(status);
if (feof(status)) {
break;
}
pinf[x++] = c;
}
fclose(status);
}
pinf[x] = '\n';
// printf("%s ", pinf);
char *str1, *str2, *subtoken, *subtoken1, *subtoken2;
char *token;
char token2[1024];
char *saveptr1, *saveptr2;
int j, i;
for (j = 1, str1 = pinf;; j++, str1 = NULL) {
token = strtok_r(str1, "\n", &saveptr1);
if (token == NULL) break;
token = trimwhitespace10(token);
// printf("%s\n", token);
for (i = 0; i <= strlen(token); i++) token2[i] = token[i];
subtoken = strtok_r(token, " \t", &saveptr2);
subtoken = trimwhitespace10(subtoken);
if (subtoken == NULL)
continue;
else if (strcmp(subtoken, "State:") == 0) {
printf("%s ", token2);
}
}
}
void sigint(int sn) {}
void sigstop(int sn) {}
int main(int argc, char const *argv[]) {
signal(SIGINT, sigint);
signal(SIGCHLD, SIG_IGN);
signal(SIGTSTP, sigstop);
Proccount = 0;
marker = 0;
username = getenv("USER");
initialise(username);
int rv = getcwd(home, sizeof(home));
// if (rv < 0) perror("Error ");
int ss = 0;
{
FILE *status = fopen("/proc/sys/kernel/hostname", "r");
while (1) {
char c = fgetc(status);
if (feof(status)) {
break;
}
sys_name[ss++] = c;
}
sys_name[ss - 1] = '\0';
}
while (1) {
getcwd(curdir, sizeof(curdir));
char input[1024], ch;
int i, f = 1;
for (i = 0; i < Proccount; i++) {
if (status[i] == 0) continue;
int sst;
int v = waitpid(working_proc[i].pid, &sst, WNOHANG);
if (v == 0) {
continue;
}
// if (v == -1) {
// perror("waitpid failed");
// continue;
// }
if (v == -1 || WIFEXITED(sst)) {
const int es = WEXITSTATUS(sst);
printf("Process %s with PID - %d\n", working_proc[i].pname,
working_proc[i].pid);
if (es == 0 && v != -1) {
printf("Exited normally\n");
} else if (v != -1) {
printf("Exited with exit code %d\n", es);
} else {
printf("Killed \n");
}
status[i] = 0;
fin++;
}
}
if (marker) {
for (int i = 0; i < Proccount; i++) {
if (status[i] == 0) continue;
printf("[%d] ", i + 1);
getStatusID(working_proc[i].pid);
printf("%s [%d] \n", working_proc[i].pname, working_proc[i].pid);
}
marker = 0;
}
// if (Proccount - fin > 0)
// printf("%d background processes\n", Proccount - fin);
for (i = 0; i < strlen(home); i++) {
if (curdir[i] != home[i]) f = 0;
}
if (f == 1 &&
(curdir[strlen(home)] == '/' || curdir[strlen(home)] == '\0')) {
curdir[0] = '~';
int f = 1;
for (; i < strlen(curdir); i++) curdir[f++] = curdir[i];
curdir[f] = '\0';
}
makeblue;
printf("<%s@%s:%s> >> ", username, sys_name, curdir);
makedef;
int cur = 0;
while ((ch = getchar()) != '\n') {
input[cur++] = ch;
}
input[cur] = '\0';
struct comm *a = runCommand(home, input, 1);
for (i = 1; i <= a[0].pid; i++) {
working_proc[Proccount].pid = a[i].pid;
strcpy(working_proc[Proccount].pname, a[i].pname);
printf("%s %d\n", working_proc[Proccount].pname,
working_proc[Proccount].pid);
Proccount++;
status[Proccount - 1] = 1;
}
if (a[0].jobs) {
marker = 1;
}
}
return 0;
}