On Tue, Feb 15, 2011 at 05:30:11PM +0000, Jason McIntyre wrote: > On Mon, Feb 14, 2011 at 11:31:18AM +0000, David Julio wrote: > > Is the exit status of which(1)/whereis(1) correct? > > > > $ which a b c > > which: a: Command not found > > which: b: Command not found > > which: c: Command not found > > > > $ echo $? > > 2 > > > > $ which -a a b c > > which: a: Command not found > > which: b: Command not found > > which: c: Command not found > > > > $ echo $? > > 1 > > If it is incorrect, below is my attempt to contribute. > this command is not covered by posix, so there's no reference there. > neither free nor netbsd document exit status for this command either. > > i have no access to such systems, but maybe someone who does can tell us > how other bsd behave? SunOS 5.10 doesn't document the exit status, doesn't have a -a switch. Simple check show that there exit status is 1 if noone are found, 0 if all/some are found.
Linux behaves the same way, but has an exit code of 2 if there where invalid arguments passed. DragonflyBSD (And FreeBSD) behave the same, but return 1 if there where invalid arguments. No access to a NetBSD box, and there man page doesn't document the exit status. > if it's a doc bug, it can be fixed easy enough. that would seem strange > behaviour though. if it's a software bug, any developer want to look at > this? What's correct there, I've no idea. At the very least we should be consistent with our own man page so, the diff might be the right solution. kv, thib > > Index: which.c > > =================================================================== > > RCS file: /cvs/src/usr.bin/which/which.c,v > > retrieving revision 1.16 > > diff -u -r1.16 which.c > > --- which.c 31 May 2010 14:01:49 -0000 1.16 > > +++ which.c 14 Feb 2011 11:02:10 -0000 > > @@ -55,11 +55,7 @@ > > > > (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, "a")) != -1) { > > + while ((ch = getopt(argc, argv, "a")) != -1) > > switch (ch) { > > case 'a': > > allmatches = 1; > > @@ -67,7 +63,11 @@ > > default: > > usage(); > > } > > - } > > + argc -= optind; > > + argv += optind; > > + > > + if (argc == 0) > > + usage(); > > > > /* > > * which(1) uses user's $PATH. > > @@ -98,11 +98,11 @@ > > if (setuid(geteuid())) > > err(1, "Can't set uid to %u", geteuid()); > > > > - for (n = optind; n < argc; n++) > > + for (n = 0; n < argc; n++) > > if (findprog(argv[n], path, progmode, allmatches) == 0) > > notfound++; > > > > - exit((notfound == 0) ? 0 : ((notfound == argc - 1) ? 2 : 1)); > > + exit((notfound == 0) ? 0 : ((notfound == argc) ? 2 : 1)); > > } > > > > int