|
|
1.1 root 1: #include "defs"
2: #include <signal.h>
3:
4: dosys(comstring,nohalt)
5: register char *comstring;
6: int nohalt;
7: {
8: register int status;
9:
10: if(metas(comstring))
11: status = doshell(comstring,nohalt);
12: else status = doexec(comstring);
13:
14: return(status);
15: }
16:
17:
18:
19: metas(s) /* Are there are any Shell meta-characters? */
20: register char *s;
21: {
22: register char c;
23:
24: while( (funny[c = *s++] & META) == 0 )
25: ;
26: return( c );
27: }
28:
29: doshell(comstring,nohalt)
30: char *comstring;
31: int nohalt;
32: {
33: #ifdef SHELLENV
34: char *getenv(), *rindex();
35: char *shellcom = getenv("SHELL");
36: char *shellstr;
37: #endif
38: if((waitpid = vfork()) == 0)
39: {
40: enbint(SIG_DFL);
41: doclose();
42:
43: #ifdef SHELLENV
44: if (shellcom == 0) shellcom = SHELLCOM;
45: shellstr = rindex(shellcom, '/') + 1;
46: execl(shellcom, shellstr, (nohalt ? "-c" : "-ce"), comstring, 0);
47: #else
48: execl(SHELLCOM, "sh", (nohalt ? "-c" : "-ce"), comstring, 0);
49: #endif
50: fatal("Couldn't load Shell");
51: }
52:
53: return( await() );
54: }
55:
56:
57:
58:
59: int intrupt();
60:
61: await()
62: {
63: int status;
64: register int pid;
65:
66: enbint(SIG_IGN);
67: while( (pid = wait(&status)) != waitpid)
68: if(pid == -1)
69: fatal("bad wait code");
70: waitpid = 0;
71: enbint(intrupt);
72: return(status);
73: }
74:
75:
76:
77:
78:
79:
80: doclose() /* Close open directory files before exec'ing */
81: {
82: register struct opendir *od;
83:
84: for (od = firstod; od; od = od->nxtopendir)
85: if (od->dirfc != NULL)
86: /* fclose(od->dirfc); */
87: close(od->dirfc->_file);
88: }
89:
90:
91:
92:
93:
94: doexec(str)
95: register char *str;
96: {
97: register char *t;
98: char *argv[200];
99: register char **p;
100:
101: while( *str==' ' || *str=='\t' )
102: ++str;
103: if( *str == '\0' )
104: return(-1); /* no command */
105:
106: p = argv;
107: for(t = str ; *t ; )
108: {
109: *p++ = t;
110: while(*t!=' ' && *t!='\t' && *t!='\0')
111: ++t;
112: if(*t)
113: for( *t++ = '\0' ; *t==' ' || *t=='\t' ; ++t)
114: ;
115: }
116:
117: *p = NULL;
118:
119: if((waitpid = vfork()) == 0)
120: {
121: enbint(SIG_DFL);
122: doclose();
123: enbint(intrupt);
124: execvp(str, argv);
125: fatal1("Cannot load %s",str);
126: }
127:
128: return( await() );
129: }
130:
131: #include <errno.h>
132:
133: #include <sys/types.h>
134: #include <sys/stat.h>
135:
136:
137:
138:
139: touch(force, name)
140: int force;
141: char *name;
142: {
143: struct stat stbuff;
144: char junk[1];
145: int fd;
146:
147: if( stat(name,&stbuff) < 0)
148: if(force)
149: goto create;
150: else
151: {
152: fprintf(stderr, "touch: file %s does not exist.\n", name);
153: return;
154: }
155:
156: if(stbuff.st_size == 0)
157: goto create;
158:
159: if( (fd = open(name, 2)) < 0)
160: goto bad;
161:
162: if( read(fd, junk, 1) < 1)
163: {
164: close(fd);
165: goto bad;
166: }
167: lseek(fd, 0L, 0);
168: if( write(fd, junk, 1) < 1 )
169: {
170: close(fd);
171: goto bad;
172: }
173: close(fd);
174: return;
175:
176: bad:
177: fprintf(stderr, "Cannot touch %s\n", name);
178: return;
179:
180: create:
181: if( (fd = creat(name, 0666)) < 0)
182: goto bad;
183: close(fd);
184: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.