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 -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

Reply via email to