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