char *Find_Executable (char *fn) { char *path, *dir, *getenv(); static char buf[1025]; /* Can't use Path_Max or Safe_Malloc here */ register char *p; for (p = fn; *p; p++) { if (*p == '/') { if (Executable (fn)) return fn; else Fatal_Error ("%s is not executable", fn); } } if ((path = getenv ("PATH")) == 0) path = ":/usr/ucb:/bin:/usr/bin"; dir = path; do { p = buf; while (*dir && *dir != ':') *p++ = *dir++; if (*dir) ++dir; if (p > buf) *p++ = '/'; strcpy (p, fn); if (Executable (buf)) return buf; } while (*dir); if (dir > path && dir[-1] == ':' && Executable (fn)) return fn; Fatal_Error ("cannot find pathname of %s", fn); /*NOTREACHED*/ }
void execwhatis(void){ /* mildly wrong -- should fork before writing */ word *a, *b, *path; var *v; struct builtin *bp; char file[512]; struct io out[1]; int found, sep; a = runq->argv->words->next; if(a==0){ Xerror1("Usage: whatis name ..."); return; } setstatus(""); out->fd = mapfd(1); out->bufp = out->buf; out->ebuf = &out->buf[NBUF]; out->strp = 0; for(;a;a = a->next){ v = vlook(a->word); if(v->val){ pfmt(out, "%s=", a->word); if(v->val->next==0) pfmt(out, "%q\n", v->val->word); else{ sep='('; for(b = v->val;b && b->word;b = b->next){ pfmt(out, "%c%q", sep, b->word); sep=' '; } pfmt(out, ")\n"); } found = 1; } else found = 0; v = gvlook(a->word); if(v->fn) pfmt(out, "fn %s %s\n", v->name, v->fn[v->pc-1].s); else{ for(bp = Builtin;bp->name;bp++) if(strcmp(a->word, bp->name)==0){ pfmt(out, "builtin %s\n", a->word); break; } if(!bp->name){ for(path = searchpath(a->word);path;path = path->next){ strcpy(file, path->word); if(file[0]) strcat(file, "/"); strcat(file, a->word); if(Executable(file)){ pfmt(out, "%s\n", file); break; } } if(!path && !found){ pfmt(err, "%s: not found\n", a->word); setstatus("not found"); } } } } poplist(); flush(err); }