Subject: Re: bin/3138: [dM] mkdep(1) always uses /usr/bin/gcc
To: None <netbsd-bugs@NetBSD.ORG>
From: Arne H. Juul <arnej@pvv.ntnu.no>
List: netbsd-bugs
Date: 02/02/1997 21:51:36
On Fri, 31 Jan 1997, Chris G. Demetriou wrote:
> use of 'which' is a fatal flaw in this patch.
Todd Vierling writes:
> Funny; I don't see why. If you're worried about security, that's one thing
> (a root user should know his $PATH--this isn't setuid); if you're worried
> about compatibility, there should be no problem using which. The binary
> /usr/bin/which (which sh uses, instead of csh's builtin) works as this
> script uses it; if it finds cc/gcc, it prints only the first instance, and
> if it doesn't, it prints nothing.
On Sat, 1 Feb 1997, Christos Zoulas wrote:
> Which is a nasty csh program. It sources your .cshrc with $prompt defined,
> and checks each component of the path and aliases for the word(s) you pass
> it. If you are not using csh this is losing.
>
> A better fix is to add a whence builtin in the Bourne shell, but I still
> don't like this much.
Argh - I had hoped we would long ago have replaced that very nasty
hack. Even when I was a die-hard csh user I found the csh-script "which"
to be extremely unhelpful and wrong. And now csh actually has a built-in
which, so the script should at least be:
#!/bin/csh -f
which $*
Actually, since this came up, I found a strong urge to write a sane C
version of "which"; appended. Comments are welcome; send-pr will
follow later. If this goes in the original patch from der Mouse should
be OK too :-)
- Arne H. J.
/* $NetBSD$ */
/*-
* Copyright (c) 1996 The NetBSD Foundation, Inc.
* All rights reserved.
*
* This code is derived from software contributed to The NetBSD Foundation
* by Arne H. Juul.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
* 3. All advertising materials mentioning features or use of this software
* must display the following acknowledgement:
* This product includes software developed by the NetBSD
* Foundation, Inc. and its contributors.
* 4. Neither the name of The NetBSD Foundation nor the names of its
* contributors may be used to endorse or promote products derived
* from this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
* ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
* TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE
* LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
* CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
* SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
* INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
* CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
* ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
* POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef lint
static char copyright[] =
"@(#) Copyright (c) 1997\n\
The NetBSD Foundation. All rights reserved.\n";
static char rcsid[] = "$NetBSD$";
#endif /* not lint */
#include <sys/types.h>
#include <sys/stat.h>
#include <locale.h>
#include <err.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static void usage();
/*
* which --
* This which is different from historic which. But then historic
* which never was sane, even for csh users.
*/
int
main(argc, argv)
int argc;
char *argv[];
{
unsigned int len, maxlen, comlen, tplen;
struct stat s;
char *path, *p, *try, *trypath, *command;
setlocale(LC_ALL, "");
if (argc != 2) usage();
setuid(geteuid()); /* for sanity of access(2) */
command = argv[1];
comlen = strlen(command);
if (strchr(command, '/')) {
if ((stat(command, &s) == 0) &&
S_ISREG(s.st_mode) && !access(command, X_OK)) {
printf("%s\n", command);
exit(0);
} else {
fprintf(stderr, "%s not found\n", command);
exit(1);
}
}
path = getenv("PATH");
if (!path) {
fprintf(stderr, "which: no PATH in environment\n");
exit(1);
}
p = strdup(path);
maxlen = 0;
try = NULL;
while (p) {
trypath = p;
p = strchr(p, ':');
if (p)
*p++ = '\0' ;
if (!*trypath)
trypath = ".";
tplen = strlen(trypath);
len = tplen + 1 + comlen + 1;
if (len > maxlen) {
maxlen = len;
try = realloc(try, maxlen);
if (!try)
err(1, "malloc");
}
strcpy(try, trypath);
try[tplen] = '/';
strcpy(try+tplen+1, command);
if ((stat(try, &s) == 0) &&
S_ISREG(s.st_mode) && !access(try, X_OK)) {
printf("%s\n", try);
exit(0);
}
}
fprintf(stderr, "no %s in path\n", command);
exit(1);
/* NOTREACHED */
}
static void
usage()
{
fprintf(stderr, "usage: which command\n");
exit(1);
}