Incorrect exit status from which(1)/whereis(1)
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. Thank you for your time. 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 - 1.16 +++ which.c 14 Feb 2011 11:02:10 - @@ -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
Re: Incorrect exit status from which(1)/whereis(1)
On Mon, Feb 14, 2011 at 11:31:18AM +, David Julio wrote: > Is the exit status of which(1)/whereis(1) correct? > is any developer interested in looking at this? jmc > $ 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. > > Thank you for your time. > > 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 - 1.16 > +++ which.c 14 Feb 2011 11:02:10 - > @@ -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
Re: Incorrect exit status from which(1)/whereis(1)
On 3/1/11, Jason McIntyre wrote: > On Mon, Feb 14, 2011 at 11:31:18AM +, David Julio wrote: >> Is the exit status of which(1)/whereis(1) correct? > > is any developer interested in looking at this? I like the diff and will commit it in 24hrs if no objects. I'll note that the description of the diff could have been clearer, as what it *really* does is make the -a option not change the exit status. Philip Guenther
Re: Incorrect exit status from which(1)/whereis(1)
On Mon, Feb 14, 2011 at 3:31 AM, David Julio wrote: ... > 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 - 1.16 > +++ which.c 14 Feb 2011 11:02:10 - ... Committed, thanks! Philip Guenther
Re: Incorrect exit status from which(1)/whereis(1)
On Mon, Feb 14, 2011 at 11:31:18AM +, 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. > > Thank you for your time. > 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? 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? jmc > 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 - 1.16 > +++ which.c 14 Feb 2011 11:02:10 - > @@ -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
Re: Incorrect exit status from which(1)/whereis(1)
On Tue, Feb 15, 2011 at 05:30:11PM +, Jason McIntyre wrote: > On Mon, Feb 14, 2011 at 11:31:18AM +, 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 - 1.16 > > +++ which.c 14 Feb 2011 11:02:10 - > > @@ -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
Re: Incorrect exit status from which(1)/whereis(1)
On Tue, Feb 15, 2011 at 5:30 PM, Jason McIntyre wrote: > On Mon, Feb 14, 2011 at 11:31:18AM +, 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. >> >> Thank you for your time. >> > > i have no access to such systems, but maybe someone who does can tell us > how other bsd behave? > > jmc NetBSD's which(1)/whereis(1) return 0 if all names were successfully resolved, 1 for errors, 2 if some names were resolved and 3 if none were. The man page for which doesn't document the exit status but the man page for whereis does. The exit status is the same with and without -a.