which(1) prints error messages on stdout, breaking shell scripts and
common usage patterns:

ldd `which foo`
ldd: foo:: No such file or directory
ldd: Command: No such file or directory
ldd: not: No such file or directory
ldd: found.: No such file or directory

X=$(which foo 2>/dev/null)
test -n "$X" || echo "foo not found"

This works contrary to the csh builtin it was modeled after and `which'
from coreutils.

The patch below fixes that. As a bonus, print "which: " in front of the
otherwise confusing error message.

While there, let err() do its job and print a standard error message for
strndup (replace with "malloc"|NULL if you like that better)

Index: which.c
===================================================================
RCS file: /vcs/cvs/openbsd/src/usr.bin/which/which.c,v
retrieving revision 1.15
diff -u -p -r1.15 which.c
--- which.c     27 Oct 2009 23:59:50 -0000      1.15
+++ which.c     27 May 2010 19:49:50 -0000
@@ -120,13 +120,13 @@ findprog(char *prog, char *path, int pro
                        (void)puts(prog);
                        return (1);
                } else {
-                       (void)printf("%s: Command not found.\n", prog);
+                       warnx("%s: Command not found.", prog);
                        return (0);
                }
        }
 
        if ((path = strdup(path)) == NULL)
-               errx(1, "Can't allocate memory.");
+               err(1, "strdup");
        pathcpy = path;
 
        proglen = strlen(prog);
@@ -159,7 +159,7 @@ findprog(char *prog, char *path, int pro
 
        /* whereis(1) is silent on failure. */
        if (!rval && progmode != PROG_WHEREIS)
-               (void)printf("%s: Command not found.\n", prog);
+               warnx("%s: Command not found.", prog);
        return (rval);
 }

Reply via email to