=================================================================== RCS file: /cvsrepo/anoncvs/cvs/src/usr.bin/which/which.c,v retrieving revision 1.1 retrieving revision 1.2 diff -c -r1.1 -r1.2 *** src/usr.bin/which/which.c 1997/02/21 18:35:00 1.1 --- src/usr.bin/which/which.c 1997/04/08 02:44:07 1.2 *************** *** 1,4 **** ! /* $OpenBSD: which.c,v 1.1 1997/02/21 18:35:00 millert Exp $ */ /* * Copyright (c) 1997 Todd C. Miller --- 1,4 ---- ! /* $OpenBSD: which.c,v 1.2 1997/04/08 02:44:07 millert Exp $ */ /* * Copyright (c) 1997 Todd C. Miller *************** *** 31,41 **** */ #ifndef lint ! static char rcsid[] = "$OpenBSD: which.c,v 1.1 1997/02/21 18:35:00 millert Exp $"; #endif /* not lint */ #include #include #include #include --- 31,42 ---- */ #ifndef lint ! static char rcsid[] = "$OpenBSD: which.c,v 1.2 1997/04/08 02:44:07 millert Exp $"; #endif /* not lint */ #include #include + #include #include #include *************** *** 45,57 **** #include #include extern char *__progname; ! int which __P((char *, char *)); void usage __P((void)); /* * which(1) -- find an executable(s) in the user's path * * Return values: * 0 - all executables found --- 46,62 ---- #include #include + #define PROG_WHICH 1 + #define PROG_WHEREIS 2 + extern char *__progname; ! int findprog __P((char *, char *, int)); void usage __P((void)); /* * which(1) -- find an executable(s) in the user's path + * whereis(1) -- find an executable(s) in the default user path * * Return values: * 0 - all executables found *************** *** 65,80 **** char **argv; { char *path; ! int n, notfound = 0; (void)setlocale(LC_ALL, ""); if (argc == 1) usage(); ! if ((path = getenv("PATH")) == NULL) ! err(-1, "Can't get $PATH from environment"); /* To make access(2) do what we want */ if (setgid(getegid())) err(-1, "Can't set gid to %u", getegid()); --- 70,114 ---- char **argv; { char *path; ! size_t n; ! int ch, notfound = 0, progmode = PROG_WHICH; (void)setlocale(LC_ALL, ""); if (argc == 1) usage(); ! /* Don't accept command args but check since old whereis(1) used to */ ! while ((ch = getopt(argc, argv, "")) != -1) { ! switch (ch) { ! default: ! usage(); ! } ! } + /* + * which(1) uses user's $PATH. + * whereis(1) uses user.cs_path from sysctl(3). + */ + if (strcmp(__progname, "whereis") == 0) { + int mib[2]; + + progmode = PROG_WHEREIS; + mib[0] = CTL_USER; + mib[1] = USER_CS_PATH; + if (sysctl(mib, 2, NULL, &n, NULL, 0) == -1) + err(-1, "unable to get length of user.cs_path"); + if (n == 0) + errx(-1, "user.cs_path was zero length!"); + if ((path = (char *)malloc(n)) == NULL) + errx(-1, "can't allocate memory."); + if (sysctl(mib, 2, path, &n, NULL, 0) == -1) + err(-1, "unable to get user.cs_path"); + } else { + if ((path = getenv("PATH")) == NULL) + err(-1, "can't get $PATH from environment"); + } + /* To make access(2) do what we want */ if (setgid(getegid())) err(-1, "Can't set gid to %u", getegid()); *************** *** 82,97 **** err(-1, "Can't set uid to %u", geteuid()); for (n = 1; n < argc; n++) ! if (which(argv[n], path) == 0) notfound++; exit((notfound == 0) ? 0 : ((notfound == argc - 1) ? 2 : 1)); } int ! which(prog, path) char *prog; char *path; { char *p, filename[MAXPATHLEN]; int proglen, plen; --- 116,132 ---- err(-1, "Can't set uid to %u", geteuid()); for (n = 1; n < argc; n++) ! if (findprog(argv[n], path, progmode) == 0) notfound++; exit((notfound == 0) ? 0 : ((notfound == argc - 1) ? 2 : 1)); } int ! findprog(prog, path, progmode) char *prog; char *path; + int progmode; { char *p, filename[MAXPATHLEN]; int proglen, plen; *************** *** 110,116 **** } if ((path = strdup(path)) == NULL) ! errx(1, "Can't allocate memory."); proglen = strlen(prog); while ((p = strsep(&path, ":")) != NULL) { --- 145,151 ---- } if ((path = strdup(path)) == NULL) ! errx(-1, "Can't allocate memory."); proglen = strlen(prog); while ((p = strsep(&path, ":")) != NULL) { *************** *** 137,149 **** } (void)free(path); ! (void)printf("%s: Command not found.\n", prog); return(0); } void usage() { ! (void) fprintf(stderr, "Usage: %s [name ...]\n", __progname); exit(1); } --- 172,186 ---- } (void)free(path); ! /* whereis(1) is silent on failure. */ ! if (progmode != PROG_WHEREIS) ! (void)printf("%s: Command not found.\n", prog); return(0); } void usage() { ! (void) fprintf(stderr, "Usage: %s name [...]\n", __progname); exit(1); }