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