-
Notifications
You must be signed in to change notification settings - Fork 0
/
mainPipes.c
322 lines (273 loc) · 7.41 KB
/
mainPipes.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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <dirent.h>
#include <errno.h>
#include <pwd.h>
#include <fcntl.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdbool.h>
#include <ctype.h>
#include "arglist.h"
#define MAX_SIZE 256
#define INIT_ARG_SIZE 5
#define MAX_REDIR_FILE_SIZE 64
void parseInput(arglist* arg_list, char* line, bool* is_in_redir, bool* is_out_redir, bool* is_append, char* redir_file);
char* getCmdPath(char* cmd, char* path);
void executeIoacct(pid_t pid, int* read_bytes, int* write_bytes);
void parsePipes(arglist* args_list1, arglist* arg_list2);
int main()
{
char host_name[MAX_SIZE];
char* user_name = NULL;
char* curr_dir = NULL;
char* last_dir = NULL;
char line[MAX_SIZE];
char redir_file[MAX_REDIR_FILE_SIZE];
arglist arg_list, arg_list2;
int read_bytes = 0;
int write_bytes = 0;
// Flags
bool is_ioacct_cmd;
bool is_background_process;
bool is_in_redir;
bool is_out_redir;
bool is_append;
while(1)
{
// Reset flags
is_ioacct_cmd = false;
is_background_process = false;
is_in_redir = false;
is_out_redir = false;
is_append = false;
// Print prompt
gethostname(host_name, sizeof(host_name));
user_name = getlogin();
// user_name = getpwuid(getuid())->pw_name;
curr_dir = getcwd(NULL, 0);
printf("%s@%s:%s $ ", host_name, user_name, curr_dir);
// Parse input
fgets(line, sizeof(line), stdin);
argListCreate(&arg_list, INIT_ARG_SIZE);
argListCreate(&arg_list2, INIT_ARG_SIZE);
parseInput(&arg_list, line, &is_in_redir, &is_out_redir, &is_append, redir_file);
parsePipes(&arg_list, &arg_list);
if (arg_list.size == 0) continue;
if (strcmp(arg_list.args[0], "ioacct") == 0)
{
argListRemove(&arg_list, 0);
is_ioacct_cmd = true;
}
// Check if the command should be a background process
size_t last_arg_len = strlen(arg_list.args[arg_list.size-1]);
if(arg_list.args[arg_list.size-1][last_arg_len-1] == '&')
{
if (last_arg_len > 1)
arg_list.args[arg_list.size-1][last_arg_len-1] = '\0';
else if(last_arg_len == 1)
argListRemove(&arg_list, arg_list.size-1);
is_background_process = true;
}
if(strcmp(arg_list.args[0], "exit") == 0)
return (arg_list.size == 1) ? 0 : atoi(arg_list.args[1]);
if(strcmp(arg_list.args[0], "cd") == 0 && !is_background_process)
{
if(arg_list.size == 1 || strcmp(arg_list.args[1], "~") == 0)
chdir(getpwuid(getuid())->pw_dir);
else if(strcmp(arg_list.args[1], "-") == 0)
chdir(last_dir);
else
chdir(arg_list.args[1]);
if(last_dir != NULL) free(last_dir);
last_dir = curr_dir;
if(is_ioacct_cmd)
{
printf("bytes read: -1\n");
printf("bytes written: -1\n");
}
}
else
{
char* cmdPath = getCmdPath(arg_list.args[0], getenv("PATH"));
if (cmdPath != NULL)
{
argListRemove(&arg_list, 0);
argListAdd(&arg_list, cmdPath, 0);
// To catch/cleanup old background processes
pid_t child_finished = waitpid(-1, (int *)NULL, WNOHANG);
pid_t child = fork();
if (child == 0)
{
fflush(0);
// Input redirect
if (is_in_redir)
{
int fd = open(redir_file, O_RDONLY);
dup2(fd, STDIN_FILENO);
close(fd);
}
// Output redirect
if (is_out_redir)
{
int fd = creat(redir_file, 0644);
dup2(fd, STDOUT_FILENO);
close(fd);
}
// Append redirect
if (is_append)
{
int fd = open(redir_file, O_WRONLY|O_APPEND);
dup2(fd, STDOUT_FILENO);
close(fd);
}
int rtn = execv(cmdPath, arg_list.args);
}
else if (child < 0)
{
exit(EXIT_FAILURE);
}
else if (is_background_process)
{
// printf("back!\n");
pid_t child_finished = waitpid(-1, (int *)NULL, WNOHANG);
// if(is_ioacct_cmd) executeIoacct(child, &read_bytes, &write_bytes);
}
else if (is_ioacct_cmd)
{
while(waitpid(-1, (int *)NULL, WNOHANG) == 0)
{
executeIoacct(child, &read_bytes, &write_bytes);
}
printf("bytes read: %d\n", read_bytes);
printf("bytes written: %d\n", write_bytes);
}
else
{
pid_t child_finished = waitpid(-1, (int *)NULL, 0);
}
}
else
{
printf("Command '%s' NOT FOUND!\n", arg_list.args[0]);
}
}
// Memory clean up
argListDestroy(&arg_list);
argListDestroy(&arg_list2);
// free(user_name);
}
return 0;
}
void parseInput(arglist* arg_list, char* line, bool* is_in_redir, bool* is_out_redir, bool* is_append, char* redir_file)
{
char* token = strtok(line, " \n");
while(token)
{
argListAdd(arg_list, token, arg_list->size);
token = strtok(NULL, " \n");
}
if (arg_list->size >= 3) {
if (strcmp(arg_list->args[arg_list->size-2], "<") == 0) *is_in_redir = true;
if (strcmp(arg_list->args[arg_list->size-2], ">") == 0) *is_out_redir = true;
if (strcmp(arg_list->args[arg_list->size-2], ">>") == 0) *is_append = true;
if (*is_out_redir || *is_in_redir || *is_append) {
strcpy(redir_file, arg_list->args[arg_list->size-1]);
argListRemove(arg_list, arg_list->size-1);
argListRemove(arg_list, arg_list->size-1);
}
}
}
char* getCmdPath(char* cmd, char* path)
{
DIR *d;
struct dirent *dir;
char* token;
char* cmdPath;
path = strdup(path);
token = strtok(path, ":");
while(token)
{
d = opendir(token);
if(d)
{
while ((dir = readdir(d)) != NULL)
if(strcmp(cmd, dir->d_name) == 0)
{
cmdPath = token;
strcat(cmdPath, "/");
strcat(cmdPath, cmd);
closedir(d);
return cmdPath;
}
}
closedir(d);
token = strtok(NULL, ":");
}
free(path);
return NULL;
}
void executeIoacct(pid_t pid, int* read_bytes, int* write_bytes)
{
char filename[64];
sprintf(filename, "/proc/%d/io", (int)pid);
// printf("%s\n", filename);
FILE* fp;
fp = fopen(filename, "r");
if(fp == NULL)
{
// printf("read bytes: -1\n");
// printf("write bytes: -1\n");
;}
else
{
char line[64];
int i = 0;
for(;i < 2; i++)
{
fgets(line, sizeof(line), fp);
if(i == 0 || i == 1)
{
char* byte_data = strtok(line, " \n");
byte_data = strtok(NULL, " \n");
if(i == 0)
*read_bytes = atoi(byte_data);
else if(i == 1)
*write_bytes = atoi(byte_data);
}
}
}
}
void parsePipes(arglist* args_list1, arglist* args_list2)
{
argListPrint(args_list1);
bool found = false;
int i=0;
for(;i < args_list1->size; i++)
{
if(strcmp(args_list1->args[i], "|") == 0) // check for spaces or whatever
{
found = true;
break;
}
}
if(!found)
return;
printf(":%d:", i);
argListRemove(args_list1, i);
int numToRemove = args_list1->size - i;
for(;i <= args_list1->size; i++)
{
argListAdd(args_list2, args_list1->args[i], args_list2->size);
}
int j=0;
for(;j < numToRemove-1; j++)
{
argListRemove(args_list1, 2);
}
argListPrint(args_list1);
argListPrint(args_list2);
}