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