xref: /original-bsd/old/make/dosys.c (revision 5fb3de76)
1 static	char *sccsid = "@(#)dosys.c	4.1 (Berkeley) 81/02/28";
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( stat(name,&stbuff) < 0)
149 	if(force)
150 		goto create;
151 	else
152 		{
153 		fprintf(stderr, "touch: file %s does not exist.\n", name);
154 		return;
155 		}
156 
157 if(stbuff.st_size == 0)
158 	goto create;
159 
160 if( (fd = open(name, 2)) < 0)
161 	goto bad;
162 
163 if( read(fd, junk, 1) < 1)
164 	{
165 	close(fd);
166 	goto bad;
167 	}
168 lseek(fd, 0L, 0);
169 if( write(fd, junk, 1) < 1 )
170 	{
171 	close(fd);
172 	goto bad;
173 	}
174 close(fd);
175 return;
176 
177 bad:
178 	fprintf(stderr, "Cannot touch %s\n", name);
179 	return;
180 
181 create:
182 	if( (fd = creat(name, 0666)) < 0)
183 		goto bad;
184 	close(fd);
185 }
186