1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
|
/*
* Copyright (c) 1989, 2010, Oracle and/or its affiliates. All rights reserved.
*/
/* Copyright (c) 1983, 1984, 1985, 1986, 1987, 1988, 1989 AT&T */
/* All Rights Reserved */
/*
* Copyright (c) 1980 Regents of the University of California.
* All rights reserved. The Berkeley Software License Agreement
* specifies the terms and conditions for redistribution.
*/
#include "sh.h"
#include <dirent.h>
#include <string.h>
#include "sh.tconst.h"
/*
* C shell
*/
/*
* System level search and execute of a command.
* We look in each directory for the specified command name.
* If the name contains a '/' then we execute only the full path name.
* If there is no search path then we execute only full path names.
*/
/*
* As we search for the command we note the first non-trivial error
* message for presentation to the user. This allows us often
* to show that a file has the wrong mode/no access when the file
* is not in the last component of the search path, so we must
* go on after first detecting the error.
*/
char *exerr; /* Execution error message */
void pexerr(void);
void texec(struct command *, tchar *, tchar **);
void xechoit(tchar **);
void dohash(char []);
static void tconvert(struct command *, tchar *, tchar **);
extern DIR *opendir_(tchar *);
void
doexec(struct command *t)
{
tchar *sav;
tchar *dp, **pv, **av;
struct varent *v;
bool slash;
int hashval, hashval1, i;
tchar *blk[2];
#ifdef TRACE
tprintf("TRACE- doexec()\n");
#endif
/*
* Glob the command name. If this does anything, then we
* will execute the command only relative to ".". One special
* case: if there is no PATH, then we execute only commands
* which start with '/'.
*/
dp = globone(t->t_dcom[0]);
sav = t->t_dcom[0];
exerr = 0; t->t_dcom[0] = dp;
setname(dp);
xfree(sav);
v = adrof(S_path /* "path" */);
if (v == 0 && dp[0] != '/') {
pexerr();
}
slash = gflag;
/*
* Glob the argument list, if necessary.
* Otherwise trim off the quote bits.
*/
gflag = 0; av = &t->t_dcom[1];
tglob(av);
if (gflag) {
av = glob(av);
if (av == 0)
error("No match");
}
blk[0] = t->t_dcom[0];
blk[1] = 0;
av = blkspl(blk, av);
#ifdef VFORK
Vav = av;
#endif
trim(av);
slash |= any('/', av[0]);
xechoit(av); /* Echo command if -x */
/*
* Since all internal file descriptors are set to close on exec,
* we don't need to close them explicitly here. Just reorient
* ourselves for error messages.
*/
SHIN = 0; SHOUT = 1; SHDIAG = 2; OLDSTD = 0;
/*
* We must do this AFTER any possible forking (like `foo`
* in glob) so that this shell can still do subprocesses.
*/
(void) sigsetmask(0);
/*
* If no path, no words in path, or a / in the filename
* then restrict the command search.
*/
if (v == 0 || v->vec[0] == 0 || slash)
pv = justabs;
else
pv = v->vec;
/* / command name for postpending */
sav = strspl(S_SLASH /* "/" */, *av);
#ifdef VFORK
Vsav = sav;
#endif
if (havhash)
hashval = hashname(*av);
i = 0;
#ifdef VFORK
hits++;
#endif
do {
if (!slash && pv[0][0] == '/' && havhash) {
hashval1 = hash(hashval, i);
if (!bit(xhash, hashval1))
goto cont;
}
/* don't make ./xxx */
if (pv[0][0] == 0 || eq(pv[0], S_DOT /* "." */)) {
texec(t, *av, av);
} else {
dp = strspl(*pv, sav);
#ifdef VFORK
Vdp = dp;
#endif
texec(t, dp, av);
#ifdef VFORK
Vdp = 0;
#endif
xfree(dp);
}
#ifdef VFORK
misses++;
#endif
cont:
pv++;
i++;
} while (*pv);
#ifdef VFORK
hits--;
#endif
#ifdef VFORK
Vsav = 0;
Vav = 0;
#endif
xfree(sav);
xfree((char *)av);
pexerr();
}
void
pexerr(void)
{
#ifdef TRACE
tprintf("TRACE- pexerr()\n");
#endif
/* Couldn't find the damn thing */
if (exerr)
bferr(exerr);
bferr("Command not found");
}
/*
* Execute command f, arg list t.
* Record error message if not found.
* Also do shell scripts here.
*/
void
texec(struct command *cmd, tchar *f, tchar **t)
{
struct varent *v;
tchar **vp;
tchar *lastsh[2];
#ifdef TRACE
tprintf("TRACE- texec()\n");
#endif
/* convert cfname and cargs from tchar to char */
tconvert(cmd, f, t);
execv(cmd->cfname, cmd->cargs);
/*
* exec returned, free up allocations from above
* tconvert(), zero cfname and cargs to prevent
* duplicate free() in freesyn()
*/
xfree(cmd->cfname);
chr_blkfree(cmd->cargs);
cmd->cfname = (char *)0;
cmd->cargs = (char **)0;
switch (errno) {
case ENOEXEC:
/* check that this is not a binary file */
{
int ff = open_(f, 0);
tchar ch[MB_LEN_MAX];
if (ff != -1 && read_(ff, ch, 1) == 1 &&
!isprint(ch[0]) && !isspace(ch[0])) {
printf("Cannot execute binary file.\n");
Perror(f);
(void) close(ff);
unsetfd(ff);
return;
}
(void) close(ff);
unsetfd(ff);
}
/*
* If there is an alias for shell, then
* put the words of the alias in front of the
* argument list replacing the command name.
* Note no interpretation of the words at this point.
*/
v = adrof1(S_shell /* "shell" */, &aliases);
if (v == 0) {
#ifdef OTHERSH
int ff = open_(f, 0);
tchar ch[MB_LEN_MAX];
#endif
vp = lastsh;
vp[0] = adrof(S_shell /* "shell" */) ?
value(S_shell /* "shell" */) :
S_SHELLPATH /* SHELLPATH */;
vp[1] = (tchar *) NULL;
#ifdef OTHERSH
if (ff != -1 && read_(ff, ch, 1) == 1 && ch[0] != '#')
vp[0] = S_OTHERSH /* OTHERSH */;
(void) close(ff);
unsetfd(ff);
#endif
} else
vp = v->vec;
t[0] = f;
t = blkspl(vp, t); /* Splice up the new arglst */
f = *t;
tconvert(cmd, f, t); /* convert tchar to char */
/*
* now done with tchar arg list t,
* free the space calloc'd by above blkspl()
*/
xfree((char *)t);
execv(cmd->cfname, cmd->cargs); /* exec the command */
/* exec returned, same free'ing as above */
xfree(cmd->cfname);
chr_blkfree(cmd->cargs);
cmd->cfname = (char *)0;
cmd->cargs = (char **)0;
/* The sky is falling, the sky is falling! */
case ENOMEM:
Perror(f);
case ENOENT:
break;
default:
if (exerr == 0) {
exerr = strerror(errno);
setname(f);
}
}
}
static void
tconvert(struct command *cmd, tchar *fname, tchar **list)
{
char **rc;
int len;
cmd->cfname = tstostr(NULL, fname);
len = blklen(list);
rc = cmd->cargs = (char **)
xcalloc((uint_t)(len + 1), sizeof (char **));
while (len--)
*rc++ = tstostr(NULL, *list++);
*rc = NULL;
}
/*ARGSUSED*/
void
execash(tchar **t, struct command *kp)
{
#ifdef TRACE
tprintf("TRACE- execash()\n");
#endif
rechist();
(void) signal(SIGINT, parintr);
(void) signal(SIGQUIT, parintr);
(void) signal(SIGTERM, parterm); /* if doexec loses, screw */
lshift(kp->t_dcom, 1);
exiterr++;
doexec(kp);
/*NOTREACHED*/
}
void
xechoit(tchar **t)
{
#ifdef TRACE
tprintf("TRACE- xechoit()\n");
#endif
if (adrof(S_echo /* "echo" */)) {
flush();
haderr = 1;
blkpr(t), Putchar('\n');
haderr = 0;
}
}
/*
* This routine called when user enters "rehash".
* Both the path and cdpath caching arrays will
* be rehashed, via calling dohash. If either
* variable is not set with a value, then dohash
* just exits.
*/
void
dorehash(void)
{
dohash(xhash);
dohash(xhash2);
}
/*
* Fill up caching arrays for path and cdpath
*/
void
dohash(char cachearray[])
{
struct stat stb;
DIR *dirp;
struct dirent *dp;
int cnt;
int i = 0;
struct varent *v;
tchar **pv;
int hashval;
tchar curdir_[MAXNAMLEN+1];
#ifdef TRACE
tprintf("TRACE- dohash()\n");
#endif
/* Caching $path */
if (cachearray == xhash) {
havhash = 1;
v = adrof(S_path /* "path" */);
} else { /* Caching $cdpath */
havhash2 = 1;
v = adrof(S_cdpath /* "cdpath" */);
}
for (cnt = 0; cnt < (HSHSIZ / 8); cnt++)
cachearray[cnt] = 0;
if (v == 0)
return;
for (pv = v->vec; *pv; pv++, i++) {
if (pv[0][0] != '/')
continue;
dirp = opendir_(*pv);
if (dirp == NULL)
continue;
if (fstat(dirp->dd_fd, &stb) < 0 || !isdir(stb)) {
unsetfd(dirp->dd_fd);
closedir_(dirp);
continue;
}
while ((dp = readdir(dirp)) != NULL) {
if (dp->d_ino == 0)
continue;
if (dp->d_name[0] == '.' &&
(dp->d_name[1] == '\0' ||
dp->d_name[1] == '.' && dp->d_name[2] == '\0'))
continue;
hashval = hash(hashname(strtots(curdir_, dp->d_name)),
i);
bis(cachearray, hashval);
}
unsetfd(dirp->dd_fd);
closedir_(dirp);
}
}
void
dounhash(void)
{
#ifdef TRACE
tprintf("TRACE- dounhash()\n");
#endif
havhash = 0;
havhash2 = 0;
}
#ifdef VFORK
void
hashstat(void)
{
#ifdef TRACE
tprintf("TRACE- hashstat_()\n");
#endif
if (hits+misses)
printf("%d hits, %d misses, %d%%\n",
hits, misses, 100 * hits / (hits + misses));
}
#endif
/*
* Hash a command name.
*/
int
hashname(tchar *cp)
{
long h = 0;
#ifdef TRACE
tprintf("TRACE- hashname()\n");
#endif
while (*cp)
h = hash(h, *cp++);
return ((int)h);
}
|