-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
610 lines (564 loc) · 14.2 KB
/
main.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
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
//#define _SVID_SOURCE
#define _GNU_SOURCE
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <limits.h>
#include <unistd.h>
#include <sys/wait.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
#include <ctype.h>
#include <dirent.h>
#include <grp.h>
#include <time.h>
#include <pwd.h>
#include "chmod.h"
#define PROC_FS "/proc"
struct dirent *entry;
int shell_active = 1; // требуется для команды exit
// команды оформлены в виде макросов
// доп. информация http://en.wikipedia.org/wiki/C_preprocessor
#define SHCMD(x) int shcmd_##x (char* cmd, char* params[])
#define SHCMD_EXEC(x) shcmd_##x (params[0], params)
#define IS_CMD(x) strncmp(#x,cmd,strlen(cmd)) == 0
// Встроенные команды
// - pwd, exit
// environment variables
char* paste_env(char *str)
{
char *env;
char buf[256];
regex_t preg;
regmatch_t pm;
char *newstr;
int len;
regcomp( &preg, "\\$\\w\\+\\b", REG_ICASE );
while ( regexec(&preg, str, 1, &pm, REG_NOTBOL) == 0 )
{
memset(buf, '\0', 256);
strncpy(buf,&str[pm.rm_so]+1,(pm.rm_eo-pm.rm_so-1 < 256)?pm.rm_eo-pm.rm_so-1:254);
env = getenv(buf);
if(!env) env = "";
len = strlen(str)-strlen(buf)+strlen(env);
newstr=malloc(len);
memset(newstr, '\0', len);
strncpy(newstr,str,pm.rm_so);
strncat(newstr,env,strlen(env));
strncat(newstr,&str[pm.rm_eo],strlen(str)-pm.rm_eo);
str = newstr;
}
regfree(&preg);
return newstr;
}
struct ls_struct
{
int l;
int a;
int i;
};
struct wc_struct
{
int L;
int l;
int w;
int c;
};
#define LIST_LENGTH 4096
int count_line_words(char* line, size_t length, struct wc_struct* flags)
{
char delim[] = " \t\n";
char* word = strtok(line, delim);
int word_count = 0;
if (word == NULL)
return word_count;
word_count++;
while ((word = strtok(NULL, delim)) != NULL)
word_count++;
return word_count;
}
int shcmd_wc(char* cmd, char* params[])
{
int params_count = 0;
while (params[params_count] != NULL)
params_count++;
struct wc_struct flags;
flags.c = flags.l = flags.w = 0;
//flags.L = 0;
char opts[] = "cwlL";
int opt;
while ((opt = getopt(params_count, params, opts)) != -1)
{
switch(opt)
{
case 'c':
flags.c = 1;
break;
case 'w':у
flags.w = 1;
break;
case 'l':
flags.l = 1;
break;
/*
case 'L':
flags.L = 1;
break;*/
}
}
int i;
printf("optind == %d\n", optind);
for (i = 0; i < params_count; i++)
printf("%d: %s\n", i, params[i]);
/*
if (flags.c == flags.w && flags.w == flags.l && flags.l == flags.L && flags.L == 0)
{
flags.c = flags.w = flags.l = 1;
}*/
if (flags.c == flags.w && flags.w == flags.l && flags.l == 0)
{
flags.c = flags.w = flags.l = 1;
}
char* line = NULL;
size_t n;
//size_t total_longest_line = 0;
size_t total_byte_count = 0;
size_t total_line_count = 0;
size_t total_word_count = 0;
size_t file_byte_count = 0;
for (i = optind; i < params_count; i++)
{
//size_t file_longest_line = 0;
file_byte_count = 0;
size_t file_line_count = 0;
size_t file_word_count = 0;
FILE* file = fopen(params[i], "r");
if (file == NULL)
{
perror("fopen");
return -1;
}
ssize_t read_bytes;
while ((read_bytes = getline(&line, &n, file)) != -1)
{
file_byte_count += read_bytes;
file_line_count++;
/*
if (read_bytes > file_longest_line)
{
file_longest_line = read_bytes;
if (line[read_bytes-1] == '\n')
file_longest_line--;
}
*/
if (n <= 1)
continue;
file_word_count += count_line_words(line, n, &flags);
}
total_byte_count += file_byte_count;
total_line_count += file_line_count;
total_word_count += file_word_count;
/*
if (total_longest_line < file_longest_line)
{
total_longest_line = file_longest_line;
}
*/
if (flags.l)
printf("%6d", file_line_count);
if (flags.w)
printf("%6d", file_word_count);
if (flags.c)
printf("%6d", file_byte_count);
/*
if (flags.L)
printf("%6d", file_longest_line);
*/
printf(" %s\n", params[i]);
}
if (total_byte_count != file_byte_count)
{
if (flags.l)
printf("%6d", total_line_count);
if (flags.w)
printf("%6d", total_word_count);
if (flags.c)
printf("%6d", total_byte_count);
/*
if (flags.L)
printf("%6d", total_longest_line);
*/
printf(" total\n");
}
if (line != NULL)
free(line);
return 0;
}
int always_true(const struct dirent *d)
{
return 1;
}
int print_ls_l(const struct dirent* d, const char* path, int print_inode)
{
char time[256];
struct stat st;
struct passwd *pwd;
struct group *grp;
int mode,right_ind;
char* rights = "rwxrwxrwx";
char *nuser,*ngroup;
char fullname[1024];
strcpy(fullname, path);
if (fullname[strlen(fullname)] != '/')
strcat(fullname, "/");
strcat(fullname, d->d_name);
lstat(fullname,&st);
mode = st.st_mode;
if (print_inode)
{
printf("%d ", d->d_ino);
}
S_ISDIR(mode)?printf("d"):printf("-");
mode &= 0777;
right_ind = 0;
while( right_ind < 9 )
{
mode&256?printf("%c",rights[right_ind]):printf("-");
mode <<= 1;
right_ind++;
}
if (pwd=getpwuid(st.st_uid))
nuser=pwd->pw_name;
else
sprintf(nuser,"%d",st.st_uid);
if (grp=getgrgid(st.st_gid))
ngroup=grp->gr_name;
else
sprintf(ngroup,"%d",st.st_gid);
strftime(time, sizeof(time), "%Y-%m-%d %H:%M", localtime(&st.st_mtime));
if (print_inode != 0)
printf("%3d %6s %6s %5d %s %s\n",st.st_nlink,nuser,ngroup,st.st_size, time, d->d_name);
else
printf("%3d %6s %6s %5d %s %s\n",st.st_nlink,nuser,ngroup,st.st_size, time, d->d_name);
return 0;
}
int shcmd_ls(char* cmd, char* params[])
{
int params_count = 0;
while (params[params_count] != NULL)
params_count++;
printf("ls started\n");
struct ls_struct flags;
flags.l = flags.a = flags.i = 0;
char opts[] = "lai";
int opt;
while ((opt = getopt(params_count, params, opts)) != -1)
{
switch(opt)
{
case 'l':
flags.l = 1;
break;
case 'a':
flags.a = 1;
break;
case 'i':
flags.i = 1;
break;
}
}
char path[500];
if (params[optind] == NULL)
strcpy(path, getenv("PWD"));
else
strcpy(path, params[optind]);
printf("optind = %d\n", optind);
printf("path = %s\n", path);
int i;
for (i = 0; i < params_count; i++)
printf("params[%d] = %s\n", i, params[i]);
struct dirent ** entry_list;
int entry_count = scandir(path, &entry_list, always_true, alphasort);
int current_length = 0;
int terminal_width = 80;
for (i = 0; i < entry_count; i++)
{
if (flags.a == 0 && entry_list[i]->d_name[0] == '.')
continue;
if (flags.l)
print_ls_l(entry_list[i], path, flags.i);
else
{
char filename[1024];
if (flags.i == 0)
strcpy(filename, "");
else
sprintf(filename, "%d ", entry_list[i]->d_ino);
strcat(filename, entry_list[i]->d_name);
if (current_length + strlen(filename) + 1 >= terminal_width && current_length > 0)
{
printf("\n");
current_length = 0;
}
printf("%s ", filename);
current_length += strlen(filename) + 2;
}
}
printf("\n");
return 0;
}
typedef struct proc_t
{
int pid;
char cmd[256];
unsigned char state;
int ppid;
int pgrp;
int session;
int tty_nr;
int tpgid;
uint flags;
uint minflt;
uint cminflt;
uint majflt;
uint cmajflt;
int utime;
int stime;
int cutime;
int cstime;
int priority;
uint nice;
int zero;
uint itrealvalue;
int starttime;
uint vsize;
uint rss;
uint rlim;
uint startcode;
uint endcode;
uint startstack;
uint kstkesp;
uint kstkeip;
int signal;
int blocked;
int sigignore;
int sigcatch;
uint wchan;
//additional
int uid;
time_t ctime;
} proc_t;
void read_proc_stat(struct proc_t *ps, char* fname)
{
char path[256];
struct stat st;
sprintf(path,"/%s/%s/stat", PROC_FS, fname);
FILE *file = fopen(path,"r");
fscanf(file,"%d %s %c %d %d %d %d %d %u %u %u %u %u %d %d %d %d %d %u %d %u %d %u %u %u %u %u %u %u %u %d %d %d %d %u",
&ps->pid, ps->cmd, &ps->state, &ps->ppid, &ps->pgrp, &ps->session,
&ps->tty_nr, &ps->tpgid, &ps->flags, &ps->minflt, &ps->cminflt,
&ps->majflt, &ps->cmajflt, &ps->utime, &ps->stime, &ps->cutime,
&ps->cstime, &ps->priority, &ps->nice, &ps->zero, &ps->itrealvalue,
&ps->starttime, &ps->vsize, &ps->rss, &ps->rlim, &ps->startcode,
&ps->endcode, &ps->startstack, &ps->kstkesp, &ps->kstkeip, &ps->signal,
&ps->blocked, &ps->sigignore, &ps->sigcatch, &ps->wchan
);
sprintf(path,"/%s/%s", PROC_FS, fname);
lstat(path,&st);
ps->uid = st.st_uid;
ps->ctime = st.st_ctime;
fclose(file);
}
int shcmd_ps(char* cmd, char* params[])
{
struct proc_t ps;
DIR *Dir = opendir(PROC_FS);
char time[256];
//printf("UID\t PID PPID NI\tSTIME\tTTY\tTIME\tCMD\n");
printf("UID\t PID PPID NI\tSTIME\tCMD\n");
while ((entry = readdir(Dir))) {
if(!isdigit(*entry->d_name))
continue;
read_proc_stat(&ps,entry->d_name);
printf("%s\t",getpwuid(ps.uid)->pw_name);
printf("%5d ",ps.pid);
printf("%5d ",ps.ppid);
printf("%2d\t",ps.nice);
strftime(time, sizeof(time), "%H:%M", localtime(&ps.ctime));
printf("%s\t",time);
//printf("\t");
//printf("\t");
printf("%s\t",ps.cmd);
printf("\n");
}
closedir(Dir);
}
int shcmd_cd(char* cmd, char* params[]){
if (params[1] == NULL) {
fprintf(stderr, "exec: expected argument to \"cd\"\n");
} else {
if (chdir(params[1]) != 0) {
perror("exec");
}
else
{
char cwdd[50];
getcwd(cwdd, 50);
setenv("PWD", cwdd, 1);
}
}
return 1;
}
SHCMD(pwd)
{
printf("%s\n",getenv("PWD"));
return 0;
}
SHCMD(exit)
{
shell_active = 0;
printf("Bye, bye!\n");
return 0;
}
SHCMD (echo) {
int i = 1;
while (params[i] != NULL) {
printf("%s", params[i]);
i++;
}
printf("\n");
}
// выполнение команды с параметрами
void my_exec(char *cmd)
{
char *params[256]; //параметры команды разделенные пробелами
char *token;
int np;
token = strtok(cmd, " ");
np = 0;
while( token && np < 255 )
{
params[np++] = token;
token = strtok(NULL, " ");
}
params[np] = NULL;
// выполнение встроенных команд
if( IS_CMD(pwd) )
SHCMD_EXEC(pwd);
else
if( IS_CMD(exit) )
SHCMD_EXEC(exit);
else
if (IS_CMD(cd))
SHCMD_EXEC(cd);
else
if (IS_CMD(ps))
SHCMD_EXEC(ps);
else
if (IS_CMD(ls))
SHCMD_EXEC(ls);
else
if (IS_CMD(wc))
SHCMD_EXEC(wc);
else
if (IS_CMD(chmod))
SHCMD_EXEC(chmod);
else
if (IS_CMD(echo))
SHCMD_EXEC(echo);
else
{ // иначе вызов внешней команды
execvp(params[0], params);
perror("exec"); // если возникла ошибка при запуске
}
}
// рекурсивная функция обработки конвейера
// параметры: строка команды, количество команд в конвейере, текущая (начинается с 0 )
int exec_conv(char *cmds[], int n, int curr)
{
int fd[2],i;
if( pipe(fd) < 0 )
{
printf("Cannot create pipe\n");
return 1;
}
if( n > 1 && curr < n - 2 )
{ // first n-2 cmds
if( fork() == 0 )
{
dup2(fd[1], 1);
close(fd[0]);
close(fd[1]);
my_exec(cmds[curr]);
exit(0);
}
if( fork() == 0 )
{
dup2(fd[0], 0);
close(fd[0]);
close(fd[1]);
exec_conv(cmds,n,++curr);
exit(0);
}
}
else
{ // 2 last cmds or if only 1 cmd
if( (n == 1) && (strcmp(cmds[0],"exit") == 0 || strstr(cmds[0], "cd") != NULL))
{ // для случая команды exit обходимся без fork, иначе не сможем завершиться
close(fd[0]);
close(fd[1]);
my_exec(cmds[curr]);
return 0;
}
if( fork() == 0 )
{
if( n > 1 ) // if more then 1 cmd
dup2(fd[1], 1);
close(fd[0]);
close(fd[1]);
my_exec(cmds[curr]);
exit(0);
}
if( n > 1 && fork()==0 )
{
dup2(fd[0], 0);
close(fd[0]);
close(fd[1]);
my_exec(cmds[curr+1]);
exit(0);
}
}
close(fd[0]);
close(fd[1]);
for( i = 0 ; i < n ; i ++ ) // ожидание завершения всех дочерних процессов
wait(0);
return 0;
}
// главная функция, цикл ввода строк (разбивка конвейера, запуск команды)
int main()
{
char cmdline[1024];
char *p, *cmds[256], *token;
int cmd_cnt;
while( shell_active )
{
printf("[%s]# ",getenv("PWD"));
fflush(stdout);
fgets(cmdline,1024,stdin);
paste_env(cmdline);
if( (p = strstr(cmdline,"\n")) != NULL ) p[0] = 0;
token = strtok(cmdline, "|");
for(cmd_cnt = 0; token && cmd_cnt < 256; cmd_cnt++ )
{
cmds[cmd_cnt] = strdup(token);
token = strtok(NULL, "|");
}
cmds[cmd_cnt] = NULL;
if( cmd_cnt > 0 )
{
exec_conv(cmds,cmd_cnt,0);
}
}
return 0;
}