📄 exec.c
字号:
int my_reenter; int odidfds; sig_t osigint, osigquit, osigterm; if (chkstop == 0 && setintr) panystop(0); /* * Hmm, we don't really want to do that now because we might * fail, but what is the choice */ rechist(); osigint = signal(SIGINT, parintr); osigquit = signal(SIGQUIT, parintr); osigterm = signal(SIGTERM, parterm); odidfds = didfds; oSHIN = SHIN; oSHOUT = SHOUT; oSHERR = SHERR; oOLDSTD = OLDSTD; saveIN = dcopy(SHIN, -1); saveOUT = dcopy(SHOUT, -1); saveDIAG = dcopy(SHERR, -1); saveSTD = dcopy(OLDSTD, -1); lshift(kp->t_dcom, 1); getexit(osetexit); if ((my_reenter = setexit()) == 0) { SHIN = dcopy(0, -1); SHOUT = dcopy(1, -1); SHERR = dcopy(2, -1); didfds = 0; doexec(t, kp); } (void) signal(SIGINT, osigint); (void) signal(SIGQUIT, osigquit); (void) signal(SIGTERM, osigterm); doneinp = 0; didfds = odidfds; (void) close(SHIN); (void) close(SHOUT); (void) close(SHERR); (void) close(OLDSTD); SHIN = dmove(saveIN, oSHIN); SHOUT = dmove(saveOUT, oSHOUT); SHERR = dmove(saveDIAG, oSHERR); OLDSTD = dmove(saveSTD, oOLDSTD); resexit(osetexit); if (my_reenter) stderror(ERR_SILENT);}voidxechoit(t) Char **t;{ if (adrof(STRecho)) { (void) fflush(csherr); blkpr(csherr, t); (void) fputc('\n', csherr); }}void/*ARGSUSED*/dohash(v, t) Char **v; struct command *t;{ DIR *dirp; register struct dirent *dp; register int cnt; int i = 0; struct varent *pathv = adrof(STRpath); Char **pv; int hashval; havhash = 1; for (cnt = 0; cnt < sizeof xhash; cnt++) xhash[cnt] = 0; if (pathv == 0) return; for (pv = pathv->vec; *pv; pv++, i++) { if (pv[0][0] != '/') continue; dirp = opendir(short2str(*pv)); if (dirp == NULL) 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(str2short(dp->d_name)), i); bis(xhash, hashval); /* tw_add_comm_name (dp->d_name); */ } (void) closedir(dirp); }}void/*ARGSUSED*/dounhash(v, t) Char **v; struct command *t;{ havhash = 0;}void/*ARGSUSED*/hashstat(v, t) Char **v; struct command *t;{ if (hits + misses) (void) fprintf(cshout, "%d hits, %d misses, %d%%\n", hits, misses, 100 * hits / (hits + misses));}/* * Hash a command name. */static inthashname(cp) register Char *cp;{ register long h = 0; while (*cp) h = hash(h, *cp++); return ((int) h);}static intiscommand(name) Char *name;{ register Char **pv; register Char *sav; register struct varent *v; register bool slash = any(short2str(name), '/'); register int hashval = 0, hashval1, i; v = adrof(STRpath); if (v == 0 || v->vec[0] == 0 || slash) pv = justabs; else pv = v->vec; sav = Strspl(STRslash, name); /* / command name for postpending */ if (havhash) hashval = hashname(name); i = 0; do { if (!slash && pv[0][0] == '/' && havhash) { hashval1 = hash(hashval, i); if (!bit(xhash, hashval1)) goto cont; } if (pv[0][0] == 0 || eq(pv[0], STRdot)) { /* don't make ./xxx */ if (executable(NULL, name, 0)) { xfree((ptr_t) sav); return i + 1; } } else { if (executable(*pv, sav, 0)) { xfree((ptr_t) sav); return i + 1; } }cont: pv++; i++; } while (*pv); xfree((ptr_t) sav); return 0;}/* Also by: * Andreas Luik <luik@isaak.isa.de> * I S A GmbH - Informationssysteme fuer computerintegrierte Automatisierung * Azenberstr. 35 * D-7000 Stuttgart 1 * West-Germany * is the executable() routine below and changes to iscommand(). * Thanks again!! *//* * executable() examines the pathname obtained by concatenating dir and name * (dir may be NULL), and returns 1 either if it is executable by us, or * if dir_ok is set and the pathname refers to a directory. * This is a bit kludgy, but in the name of optimization... */static intexecutable(dir, name, dir_ok) Char *dir, *name; bool dir_ok;{ struct stat stbuf; Char path[MAXPATHLEN + 1], *dp, *sp; char *strname; if (dir && *dir) { for (dp = path, sp = dir; *sp; *dp++ = *sp++) if (dp == &path[MAXPATHLEN + 1]) { *--dp = '\0'; break; } for (sp = name; *sp; *dp++ = *sp++) if (dp == &path[MAXPATHLEN + 1]) { *--dp = '\0'; break; } *dp = '\0'; strname = short2str(path); } else strname = short2str(name); return (stat(strname, &stbuf) != -1 && ((S_ISREG(stbuf.st_mode) && /* save time by not calling access() in the hopeless case */ (stbuf.st_mode & (S_IXOTH | S_IXGRP | S_IXUSR)) && access(strname, X_OK) == 0) || (dir_ok && S_ISDIR(stbuf.st_mode))));}/* The dowhich() is by: * Andreas Luik <luik@isaak.isa.de> * I S A GmbH - Informationssysteme fuer computerintegrierte Automatisierung * Azenberstr. 35 * D-7000 Stuttgart 1 * West-Germany * Thanks!! *//*ARGSUSED*/voiddowhich(v, c) register Char **v; struct command *c;{ struct wordent lex[3]; struct varent *vp; lex[0].next = &lex[1]; lex[1].next = &lex[2]; lex[2].next = &lex[0]; lex[0].prev = &lex[2]; lex[1].prev = &lex[0]; lex[2].prev = &lex[1]; lex[0].word = STRNULL; lex[2].word = STRret; while (*++v) { if ((vp = adrof1(*v, &aliases)) != NULL) { (void) fprintf(cshout, "%s: \t aliased to ", vis_str(*v)); blkpr(cshout, vp->vec); (void) fputc('\n', cshout); } else { lex[1].word = *v; tellmewhat(lex); } }}static voidtellmewhat(lex) struct wordent *lex;{ register int i; register struct biltins *bptr; register struct wordent *sp = lex->next; bool aliased = 0; Char *s0, *s1, *s2; Char qc; if (adrof1(sp->word, &aliases)) { alias(lex); sp = lex->next; aliased = 1; } s0 = sp->word; /* to get the memory freeing right... */ /* handle quoted alias hack */ if ((*(sp->word) & (QUOTE | TRIM)) == QUOTE) (sp->word)++; /* do quoting, if it hasn't been done */ s1 = s2 = sp->word; while (*s2) switch (*s2) { case '\'': case '"': qc = *s2++; while (*s2 && *s2 != qc) *s1++ = *s2++ | QUOTE; if (*s2) s2++; break; case '\\': if (*++s2) *s1++ = *s2++ | QUOTE; break; default: *s1++ = *s2++; } *s1 = '\0'; for (bptr = bfunc; bptr < &bfunc[nbfunc]; bptr++) { if (eq(sp->word, str2short(bptr->bname))) { if (aliased) prlex(cshout, lex); (void) fprintf(cshout, "%s: shell built-in command.\n", vis_str(sp->word)); sp->word = s0; /* we save and then restore this */ return; } } if ((i = iscommand(strip(sp->word))) != 0) { register Char **pv; register struct varent *v; bool slash = any(short2str(sp->word), '/'); v = adrof(STRpath); if (v == 0 || v->vec[0] == 0 || slash) pv = justabs; else pv = v->vec; while (--i) pv++; if (pv[0][0] == 0 || eq(pv[0], STRdot)) { sp->word = Strspl(STRdotsl, sp->word); prlex(cshout, lex); xfree((ptr_t) sp->word); sp->word = s0; /* we save and then restore this */ return; } s1 = Strspl(*pv, STRslash); sp->word = Strspl(s1, sp->word); xfree((ptr_t) s1); prlex(cshout, lex); xfree((ptr_t) sp->word); } else { if (aliased) prlex(cshout, lex); (void) fprintf(csherr, "%s: Command not found.\n", vis_str(sp->word)); } sp->word = s0; /* we save and then restore this */}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -