From: millert Date: Fri, 21 Feb 1997 18:34:57 +0000 (+0000) Subject: which(1) is now a binary that works in any shell since csh(1) has a X-Git-Url: http://artulab.com/gitweb/?a=commitdiff_plain;h=71e895c5a3b8db0057d632ea2e73163a98528980;p=openbsd which(1) is now a binary that works in any shell since csh(1) has a which built-in. --- diff --git a/usr.bin/which/Makefile b/usr.bin/which/Makefile index 0c1bd524cac..c6433a1986b 100644 --- a/usr.bin/which/Makefile +++ b/usr.bin/which/Makefile @@ -1,9 +1,5 @@ -# $OpenBSD: Makefile,v 1.3 1996/12/08 14:32:39 downsj Exp $ +# $OpenBSD: Makefile,v 1.4 1997/02/21 18:34:57 millert Exp $ -MAN= which.1 - -beforeinstall: - ${INSTALL} ${COPY} -o ${BINOWN} -g ${BINGRP} -m ${BINMODE} \ - ${.CURDIR}/which.csh ${DESTDIR}${BINDIR}/which +PROG= which .include diff --git a/usr.bin/which/which.1 b/usr.bin/which/which.1 index 16170693ddc..0091b272df2 100644 --- a/usr.bin/which/which.1 +++ b/usr.bin/which/which.1 @@ -1,4 +1,4 @@ -.\" $OpenBSD: which.1,v 1.3 1997/01/07 15:57:48 niklas Exp $ +.\" $OpenBSD: which.1,v 1.4 1997/02/21 18:34:59 millert Exp $ .\" Copyright (c) 1980, 1991 Regents of the University of California. .\" All rights reserved. .\" @@ -32,14 +32,12 @@ .\" .\" from: @(#)which.1 6.3 (Berkeley) 4/23/91 .\" -.Dd April 23, 1991 +.Dd February 21, 1997 .Dt WHICH 1 -.Os BSD 3 +.Os .Sh NAME .Nm which -.Nd "locate a program file including aliases and paths" -.Pq Xr csh 1 -only) +.Nd "locate a program file (or files) in the path" .Sh SYNOPSIS .Nm which .Op Ar name @@ -48,14 +46,10 @@ only) .Nm Which takes a list of names and looks for the files which would be executed had these names been given as commands. -Each argument is expanded if it is aliased, -and searched for along the user's path. -Both aliases and path are taken from the user's -.Pa \&.cshrc -file. -.Pp +Each argument is searched for along the user's path. +.Sh RETURN VALUES The -.Nm which +.Nm utility exits with one of the following values: .Bl -tag -width 4n .It 0 @@ -64,22 +58,44 @@ All names got successfully resolved. Some names got resolved but not all. .It 2 No names got resolved. -.El -.Sh FILES -.Bl -tag -width ~/\&.cshrc -.It Pa ~/\&.cshrc -source of aliases and path values +.It -1 +A system error occurred. .El .Sh DIAGNOSTICS -A diagnostic is given for names which are aliased to more than a single -word, -or if an executable file with the argument name was not found in the path. -.Sh BUGS -Must be executed by a +A diagnostic is given if an executable file with the argument +name was not found in the path. +.Sh ENVIRONMENT VARIABLES +.Bl -tag -width PATH +.It Ev PATH +.Nm +uses the environment variable +.Ev PATH +as a colon-separated list of directories in which to find executables. +If +.Ev PATH +is not set, and the given name is not a fully-qualified +or relative pathname, +.Nm +will fail. +.El +.Sh CAVEATS +The +.Nm +command formerly was a +.Xr csh 1 +script and could expand aliases. +.Xr csh 1 +now has a built-in +.Nm +comand so this version is intended for use +with other shells like +.Xr sh 1 . +.Sh SEE ALSO .Xr csh 1 , -or some other shell which knows about aliases. +.Xr sh 1 , +.Xr environ 7 .Sh HISTORY -The +A .Nm command appeared in .Bx 3.0 . diff --git a/usr.bin/which/which.c b/usr.bin/which/which.c new file mode 100644 index 00000000000..0f9c9265d2f --- /dev/null +++ b/usr.bin/which/which.c @@ -0,0 +1,149 @@ +/* $OpenBSD: which.c,v 1.1 1997/02/21 18:35:00 millert Exp $ */ + +/* + * Copyright (c) 1997 Todd C. Miller + * All rights reserved. + * + * 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 Todd C. Miller. + * 4. The name of the author may not be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED ``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 AUTHOR 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 rcsid[] = "$OpenBSD: which.c,v 1.1 1997/02/21 18:35:00 millert Exp $"; +#endif /* not lint */ + +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +extern char *__progname; + +int which __P((char *, char *)); +void usage __P((void)); + +/* + * which(1) -- find an executable(s) in the user's path + * + * Return values: + * 0 - all executables found + * 1 - some found, some not + * 2 - none found + */ + +int +main(argc, argv) + int argc; + char **argv; +{ + char *path; + int n, notfound = 0; + + (void)setlocale(LC_ALL, ""); + + if (argc == 1) + usage(); + + if ((path = getenv("PATH")) == NULL) + err(-1, "Can't get $PATH from environment"); + + /* To make access(2) do what we want */ + if (setgid(getegid())) + err(-1, "Can't set gid to %u", getegid()); + if (setuid(geteuid())) + err(-1, "Can't set uid to %u", geteuid()); + + for (n = 1; n < argc; n++) + if (which(argv[n], path) == 0) + notfound++; + + exit((notfound == 0) ? 0 : ((notfound == argc - 1) ? 2 : 1)); +} + +int +which(prog, path) + char *prog; + char *path; +{ + char *p, filename[MAXPATHLEN]; + int proglen, plen; + struct stat sbuf; + + /* Special case if prog contains '/' */ + if (strchr(prog, '/')) { + if ((stat(prog, &sbuf) == 0) && S_ISREG(sbuf.st_mode) && + access(prog, X_OK) == 0) { + (void)puts(prog); + return(1); + } else { + (void)printf("%s: Command not found.\n", prog); + return(0); + } + } + + if ((path = strdup(path)) == NULL) + errx(1, "Can't allocate memory."); + + proglen = strlen(prog); + while ((p = strsep(&path, ":")) != NULL) { + if (*p == '\0') + p = "."; + + plen = strlen(p); + while (p[plen-1] == '/') + p[--plen] = '\0'; /* strip trailing '/' */ + + if (plen + 1 + proglen >= sizeof(filename)) { + warnx("%s/%s: %s", p, prog, strerror(ENAMETOOLONG)); + return(0); + } + + (void)strcpy(filename, p); + filename[plen] = '/'; + (void)strcpy(filename + plen + 1, prog); + if ((stat(filename, &sbuf) == 0) && S_ISREG(sbuf.st_mode) && + access(filename, X_OK) == 0) { + (void)puts(filename); + return(1); + } + } + (void)free(path); + + (void)printf("%s: Command not found.\n", prog); + return(0); +} + +void +usage() +{ + (void) fprintf(stderr, "Usage: %s [name ...]\n", __progname); + exit(1); +} diff --git a/usr.bin/which/which.csh b/usr.bin/which/which.csh deleted file mode 100644 index fd9ffe6dd13..00000000000 --- a/usr.bin/which/which.csh +++ /dev/null @@ -1,88 +0,0 @@ -#!/bin/csh -# $OpenBSD: which.csh,v 1.3 1997/01/07 15:38:58 niklas Exp $ - -# -# DO NOT USE "csh -f" -# -# Copyright (c) 1983 The Regents of the University of California. -# All rights reserved. -# -# 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 University of -# California, Berkeley and its contributors. -# 4. Neither the name of the University 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 REGENTS 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. -# -# @(#)which.csh 5.5 (Berkeley) 4/18/91 -# - -# which : tells you which program you get -# -set prompt = "% " -set noglob -foreach arg ( $argv ) - set alius = `alias $arg` - switch ( $#alius ) - case 0 : - breaksw - case 1 : - set arg = $alius[1] - breaksw - default : - echo ${arg}: " " aliased to $alius - continue - endsw - unset found - if ( $arg:h != $arg:t ) then - if ( -e $arg ) then - echo $arg - else - echo $arg not found - endif - continue - else - foreach i ( $path ) - if ( -x $i/$arg && ! -d $i/$arg ) then - echo $i/$arg - set found - set one_found - break - endif - end - endif - if ( ! $?found ) then - echo no $arg in $path - set one_missed - endif -end -if ( $?one_missed ) then - if ( $?one_found ) then - exit 1 - else - exit 2 - endif -else - exit 0 -endif