+ register Char *cp;
+{
+ register long h = 0;
+
+ while (*cp)
+ h = hash(h, *cp++);
+ return ((int) h);
+}
+
+static int
+iscommand(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 int
+executable(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*/
+void
+dowhich(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 void
+tellmewhat(lex)
+ struct wordent *lex;