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