Commit | Line | Data |
---|---|---|
b46f5ef9 | 1 | static char *sccsid = "@(#)dosys.c 4.13 (Berkeley) %G%"; |
7bd4480c BJ |
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 | \f | |
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 | |
e3a6f18e | 39 | if((waitpid = vfork()) == 0) |
7bd4480c BJ |
40 | { |
41 | enbint(SIG_DFL); | |
cd5e107a | 42 | doclose(); |
7bd4480c BJ |
43 | |
44 | #ifdef SHELLENV | |
45 | if (shellcom == 0) shellcom = SHELLCOM; | |
46 | shellstr = rindex(shellcom, '/') + 1; | |
dbe1779e | 47 | execl(shellcom, shellstr, (nohalt ? "-c" : "-ec"), comstring, 0); |
7bd4480c | 48 | #else |
dbe1779e | 49 | execl(SHELLCOM, "sh", (nohalt ? "-c" : "-ec"), comstring, 0); |
7bd4480c BJ |
50 | #endif |
51 | fatal("Couldn't load Shell"); | |
52 | } | |
53 | ||
54 | return( await() ); | |
55 | } | |
56 | ||
57 | ||
58 | ||
59 | ||
31e81748 | 60 | void intrupt(); |
7bd4480c BJ |
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 | } | |
cd5e107a SL |
75 | |
76 | /* | |
77 | * Close open directory files before exec'ing | |
78 | */ | |
79 | doclose() | |
80 | { | |
81 | register struct dirhdr *od; | |
82 | ||
83 | for (od = firstod; od; od = od->nxtopendir) | |
84 | if (od->dirfc != NULL) | |
85 | /* | |
86 | * vfork kludge... | |
87 | * we cannot call closedir since this will modify | |
88 | * the parents data space; just call close directly. | |
89 | */ | |
c01bbea7 | 90 | (void)close(dirfd(od->dirfc)); |
cd5e107a | 91 | } |
7bd4480c BJ |
92 | \f |
93 | ||
94 | ||
6f90c301 | 95 | #define MAXARGV 400 |
7bd4480c BJ |
96 | |
97 | doexec(str) | |
98 | register char *str; | |
99 | { | |
100 | register char *t; | |
6f90c301 | 101 | char *argv[MAXARGV]; |
7bd4480c BJ |
102 | register char **p; |
103 | ||
104 | while( *str==' ' || *str=='\t' ) | |
105 | ++str; | |
106 | if( *str == '\0' ) | |
107 | return(-1); /* no command */ | |
108 | ||
109 | p = argv; | |
110 | for(t = str ; *t ; ) | |
111 | { | |
6f90c301 SL |
112 | if (p >= argv + MAXARGV) |
113 | fatal1("%s: Too many arguments.", str); | |
7bd4480c BJ |
114 | *p++ = t; |
115 | while(*t!=' ' && *t!='\t' && *t!='\0') | |
116 | ++t; | |
117 | if(*t) | |
118 | for( *t++ = '\0' ; *t==' ' || *t=='\t' ; ++t) | |
119 | ; | |
120 | } | |
121 | ||
122 | *p = NULL; | |
123 | ||
5ec84434 | 124 | if((waitpid = vfork()) == 0) |
7bd4480c BJ |
125 | { |
126 | enbint(SIG_DFL); | |
cd5e107a | 127 | doclose(); |
7bd4480c BJ |
128 | enbint(intrupt); |
129 | execvp(str, argv); | |
130 | fatal1("Cannot load %s",str); | |
131 | } | |
132 | ||
133 | return( await() ); | |
134 | } | |
135 | \f | |
136 | #include <errno.h> | |
137 | ||
7bd4480c BJ |
138 | #include <sys/stat.h> |
139 | ||
140 | ||
141 | ||
7bd4480c BJ |
142 | touch(force, name) |
143 | int force; | |
144 | char *name; | |
145 | { | |
146 | struct stat stbuff; | |
147 | char junk[1]; | |
148 | int fd; | |
149 | ||
150 | if( 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 | ||
159 | if(stbuff.st_size == 0) | |
160 | goto create; | |
161 | ||
162 | if( (fd = open(name, 2)) < 0) | |
163 | goto bad; | |
164 | ||
165 | if( read(fd, junk, 1) < 1) | |
166 | { | |
167 | close(fd); | |
168 | goto bad; | |
169 | } | |
170 | lseek(fd, 0L, 0); | |
171 | if( write(fd, junk, 1) < 1 ) | |
172 | { | |
173 | close(fd); | |
174 | goto bad; | |
175 | } | |
176 | close(fd); | |
177 | return; | |
178 | ||
179 | bad: | |
180 | fprintf(stderr, "Cannot touch %s\n", name); | |
181 | return; | |
182 | ||
183 | create: | |
184 | if( (fd = creat(name, 0666)) < 0) | |
185 | goto bad; | |
186 | close(fd); | |
187 | } |