1: #include "stdio.h"
2: #include "signal.h"
3:
4: #define EASY 1
5: #define MEDIUM 2
6: #define HARD 3
7:
8: mysys(s)
9: char *s;
10: {
11: /* like "system" but rips off "mv", etc.*/
12: /* also tries to guess if can get away with exec cmd */
13: /* instead of sh cmd */
14: char p[300];
15: char *np[40];
16: register char *t;
17: int nv, type, stat;
18:
19: type = EASY; /* we hope */
20: for (t = s; *t && type != HARD; t++) {
21: switch (*t) {
22: case '*':
23: case '[':
24: case '?':
25: case '>':
26: case '<':
27: case '$':
28: case '\'':
29: case '"':
30: type = MEDIUM;
31: break;
32: case '|':
33: case ';':
34: case '&':
35: type = HARD;
36: break;
37: }
38: }
39: switch (type) {
40: case HARD:
41: return(system(s));
42: case MEDIUM:
43: strcpy(p, "exec ");
44: strcat(p, s);
45: return(system(p));
46: case EASY:
47: strcpy(p,s);
48: nv = getargs(p, np);
49: t=np[0];
50: if ((strcmp(t, "mv") == 0)||
51: (strcmp(t, "cp") == 0)||
52: (strcmp(t, "rm") == 0)||
53: (strcmp(t, "ls") == 0) ) {
54: if (fork() == 0) {
55: char b[100];
56: signal(SIGINT, SIG_DFL);
57: strcpy(b, "/bin/");
58: strcat(b, t);
59: np[nv] = 0;
60: execv(b, np);
61: fprintf(stderr, "Execv failed\n");
62: exit(1);
63: }
64: wait(&stat);
65: return(stat);
66: }
67: return(system(s));
68: }
69: }
70:
71: /*
72: * system():
73: * same as library version, except that resets
74: * default handling of signals in child, so that
75: * user gets the behavior he expects.
76: */
77:
78: system(s)
79: char *s;
80: {
81: int status, pid, w;
82: register int (*istat)(), (*qstat)();
83:
84: istat = signal(SIGINT, SIG_IGN);
85: qstat = signal(SIGQUIT, SIG_IGN);
86: if ((pid = fork()) == 0) {
87: signal(SIGINT, SIG_DFL);
88: signal(SIGQUIT, SIG_DFL);
89: execl("/bin/sh", "sh", "-c", s, 0);
90: _exit(127);
91: }
92: while ((w = wait(&status)) != pid && w != -1)
93: ;
94: if (w == -1)
95: status = -1;
96: signal(SIGINT, istat);
97: signal(SIGQUIT, qstat);
98: return(status);
99: }
100:
101: getargs(s, v)
102: char *s, **v;
103: {
104: int i;
105:
106: i = 0;
107: for (;;) {
108: v[i++]=s;
109: while (*s != 0 && *s!=' '&& *s != '\t')
110: s++;
111: if (*s == 0)
112: break;
113: *s++ =0;
114: while (*s == ' ' || *s == '\t')
115: s++;
116: if (*s == 0)
117: break;
118: }
119: return(i);
120: }
Defined functions
mysys
defined in line
8; used 1 times
Defined macros
EASY
defined in line
4; used 1 times
HARD
defined in line
6; used 2 times
MEDIUM
defined in line
5; used 1 times