Import NetBSD which and whereis commands.

Change-Id: Ide31b4cc52c64cbd424437f649674a460fb0a754
This commit is contained in:
stux 2014-07-11 10:59:23 -05:00 committed by Lionel Sambuc
parent fc05cedbdd
commit d24fd34cdf
13 changed files with 335 additions and 182 deletions

View File

@ -29,7 +29,7 @@ SUBDIR= add_route arp ash at backup btrace \
telnetd term termcap tget time \
truncate udpstat umount \
unstack update uud uue version vol \
whereis which writeisofs fetch \
writeisofs fetch \
zdump zmodem pkgin_cd pkgin_all \
worldstone updateboot update_bootcfg \
atnormalize dosread fdisk loadfont \

View File

@ -1,4 +0,0 @@
SCRIPTS= whereis.sh
MAN=
.include <bsd.prog.mk>

View File

@ -1,23 +0,0 @@
#!/bin/sh
: List all system directories containing the argument
: Author: Terrence W. Holm
if test $# -ne 1; then
echo "Usage: whereis name"
exit 1
fi
path="/bin /lib /etc\
/usr/bin /usr/lib\
/usr/include /usr/include/sys"
for dir in $path; do
for file in $dir/$1 $dir/$1.*; do
if test -f $file; then
echo $file
elif test -d $file; then
echo $file/
fi
done
done
exit 0

View File

@ -1,4 +0,0 @@
PROG= which
MAN=
.include <bsd.prog.mk>

View File

@ -1,86 +0,0 @@
/* which - search paths for executable */
#define DELIMITER ':'
#include <sys/types.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <stdio.h>
int main(int argc, char **argv);
int main(ac, av)
int ac;
char **av;
{
char *path, *cp;
char buf[400];
char prog[400];
char patbuf[512];
int quit, none;
int excode = 0;
if (ac < 2) {
fprintf(stderr, "Usage: %s cmd [cmd, ..]\n", *av);
exit(1);
}
av[ac] = 0;
for (av++; *av; av++) {
quit = 0;
none = 1;
if ((path = getenv("PATH")) == NULL) {
fprintf(stderr, "Null path.\n");
exit(0);
}
strcpy(patbuf, path);
path = patbuf;
cp = path;
while (1) {
cp = strchr(path, DELIMITER);
if (cp == NULL)
quit++;
else
*cp = '\0';
if (strcmp(path, "") == 0 && quit == 0) {
sprintf(buf, "%s./%s", path, *av);
} else
sprintf(buf, "%s/%s", path, *av);
/* Fprintf(stderr,"Trying %s, path %s\n",buf,path); */
path = ++cp;
if (access(buf, 1) == 0) {
printf("%s\n", buf);
none = 0;
}
sprintf(prog, "%s.%s", buf, "prg");
if (access(prog, 1) == 0) {
printf("%s\n", prog);
none = 0;
}
sprintf(prog, "%s.%s", buf, "ttp");
if (access(prog, 1) == 0) {
printf("%s\n", prog);
none = 0;
}
sprintf(prog, "%s.%s", buf, "tos");
if (access(prog, 1) == 0) {
printf("%s\n", prog);
none = 0;
}
if (quit) {
if (none) {
fprintf(stderr, "No %s in %s\n", *av, getenv("PATH"));
excode = 1;
}
break;
}
}
}
return(excode);
}

View File

@ -19,7 +19,7 @@ MAN= ash.1 at.1 \
synctree.1 sysenv.1 sz.1 telnet.1 template.1 \
term.1 termcap.1 tget.1 time.1 \
truncate.1 umount.1 \
uud.1 uue.1 vol.1 whereis.1 which.1 \
uud.1 uue.1 vol.1 \
yap.1 linkfarm.1 pkg_view.1
MLINKS += ash.1 sh.1

View File

@ -1,28 +0,0 @@
.TH WHEREIS 1
.SH NAME
whereis \- examine system directories for a given file
.SH SYNOPSIS
\fBwhereis \fIfile\fR
.br
.de FL
.TP
\\fB\\$1\\fR
\\$2
..
.de EX
.TP 20
\\fB\\$1\\fR
# \\$2
..
.SH EXAMPLES
.TP 20
.B whereis stat.h
# Prints: \fI/usr/include/sys/stat.h\fR
.SH DESCRIPTION
.PP
\fIWhereis\fR searches a fixed set of system
directories, \fI/bin\fR, \fI/lib\fR, \fI/usr/bin\fR,
and others, and prints all occurrences of the argument name in any of them.
.SH "SEE ALSO"
.BR man (1),
.BR which (1).

View File

@ -1,34 +0,0 @@
.TH WHICH 1
.SH NAME
which \- examine $PATH to see which file will be executed
.SH SYNOPSIS
\fBwhich \fIname\fR
.br
.de FL
.TP
\\fB\\$1\\fR
\\$2
..
.de EX
.TP 20
\\fB\\$1\\fR
# \\$2
..
.SH EXAMPLES
.TP 20
.B which a.out
# Tells which \fIa.out\fR will be executed
.SH DESCRIPTION
.PP
The $PATH shell variable controls the
\s-1MINIX 3\s-1
search rules.
If a command \fIa.out\fR is given, the shell first tries to find an
executable file in the working directory.
If that fails, it looks in various system directories, such as
\fI/bin\fR and \fI/usr/bin\fR.
The\fR which\fR command makes the same search and gives the absolute
path of the program that will be chosen, followed by other occurrences
of the file name along the path.
.SH "SEE ALSO"
.BR man (1).

View File

@ -30,7 +30,7 @@ SUBDIR= asa \
uniq units unvis unzip users \
uuidgen vis \
\
wall wc what who whois \
wall wc what whereis who whois \
write xargs xinstall xstr yes
.if !defined(__MINIX)

8
usr.bin/whereis/Makefile Normal file
View File

@ -0,0 +1,8 @@
# $NetBSD: Makefile,v 1.6 2009/04/14 22:15:28 lukem Exp $
# from: @(#)Makefile 8.1 (Berkeley) 6/6/93
PROG= whereis
MAN= whereis.1 which.1
LINKS= ${BINDIR}/whereis ${BINDIR}/which
.include <bsd.prog.mk>

77
usr.bin/whereis/whereis.1 Normal file
View File

@ -0,0 +1,77 @@
.\" $NetBSD: whereis.1,v 1.14 2003/08/07 11:17:17 agc Exp $
.\"
.\" Copyright (c) 1993
.\" 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. 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.
.\"
.\" @(#)whereis.1 8.3 (Berkeley) 4/27/95
.\"
.Dd April 3, 2003
.Dt WHEREIS 1
.Os
.Sh NAME
.Nm whereis
.Nd locate programs
.Sh SYNOPSIS
.Nm
.Op Fl p
.Ar program
.Op Ar program ...
.Sh DESCRIPTION
The
.Nm
utility checks the standard binary directories for the specified programs,
printing out the paths of any it finds.
.Pp
The default path searched is the string returned by the
.Xr sysctl 8
utility for the
.Dq user.cs_path
string.
If the
.Op Fl p
option is specified, then the value of the environment
variable
.Ev PATH
is used instead.
.Sh EXIT STATUS
The
.Nm
utility exits 0 on success, 1 on general error, 2 if only some
programs were located and 3 if none were.
.Sh SEE ALSO
.Xr whatis 1 ,
.Xr which 1 ,
.Xr sysctl 8
.Sh COMPATIBILITY
The historic flags and arguments for the
.Nm
utility are no longer available in this version.
.Sh HISTORY
The
.Nm
command appeared in
.Bx 3.0 .

180
usr.bin/whereis/whereis.c Normal file
View File

@ -0,0 +1,180 @@
/* $NetBSD: whereis.c,v 1.21 2008/10/17 10:53:26 apb Exp $ */
/*-
* Copyright (c) 1993
* 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. 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.
*/
#include <sys/cdefs.h>
#ifndef lint
__COPYRIGHT("@(#) Copyright (c) 1993\
The Regents of the University of California. All rights reserved.");
#endif /* not lint */
#ifndef lint
#if 0
static char sccsid[] = "@(#)whereis.c 8.3 (Berkeley) 5/4/95";
#endif
__RCSID("$NetBSD: whereis.c,v 1.21 2008/10/17 10:53:26 apb Exp $");
#endif /* not lint */
#include <sys/param.h>
#include <sys/stat.h>
#include <sys/sysctl.h>
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
static void usage(void) __dead;
int
main(int argc, char *argv[])
{
struct stat sb;
size_t len;
#ifdef __minix
int ch;
#else
int ch, mib[2];
#endif /* def __minix */
char *p, *std, path[MAXPATHLEN];
const char *t;
int which = strcmp(getprogname(), "which") == 0;
int useenvpath = which, found = 0;
gid_t egid = getegid();
uid_t euid = geteuid();
/* To make access(2) do what we want */
if (setgid(egid) == -1)
err(1, "Can't set gid to %lu", (unsigned long)egid);
if (setuid(euid) == -1)
err(1, "Can't set uid to %lu", (unsigned long)euid);
while ((ch = getopt(argc, argv, "ap")) != -1)
switch (ch) {
case 'a':
which = 0;
break;
case 'p':
useenvpath = 1; /* use environment for PATH */
break;
case '?':
default:
usage();
}
argc -= optind;
argv += optind;
if (argc == 0)
usage();
if (useenvpath) {
if ((std = getenv("PATH")) == NULL)
errx(1, "PATH environment variable is not set");
} else {
/* Retrieve the standard path. */
#ifdef __minix
/*
Note: This path is currently defined here and should probably be defined
here, in "ash" or in "sh".
To minimize code changes, the path has been hard coded into this file.
However, if this path needs to be used in other ported programs, please
move this define to <minix/paths.h> and add the include to this file
and all files that use _PATH_USER_CS_PATH.
*/
#define _PATH_USER_CS_PATH "/usr/X11R7/sbin:/usr/local/sbin:/usr/pkg/sbin:/usr/sbin:/sbin:/usr/X11R7/bin:/usr/local/bin:/usr/pkg/bin:/usr/bin:/bin:/usr/games"
std = strdup(_PATH_USER_CS_PATH);
#else
mib[0] = CTL_USER;
mib[1] = USER_CS_PATH;
if (sysctl(mib, 2, NULL, &len, NULL, 0) == -1)
err(1, "sysctl: user.cs_path");
if (len == 0)
errx(1, "sysctl: user.cs_path (zero length)");
if ((std = malloc(len)) == NULL)
err(1, NULL);
if (sysctl(mib, 2, std, &len, NULL, 0) == -1)
err(1, "sysctl: user.cs_path");
#endif /* def __minix */
}
/* For each path, for each program... */
for (; *argv; ++argv) {
if (**argv == '/') {
if (stat(*argv, &sb) == -1)
continue; /* next argv */
if (!S_ISREG(sb.st_mode))
continue; /* next argv */
if (access(*argv, X_OK) == -1)
continue; /* next argv */
(void)printf("%s\n", *argv);
found++;
if (which)
continue; /* next argv */
} else for (p = std; p; ) {
t = p;
if ((p = strchr(p, ':')) != NULL) {
*p = '\0';
if (t == p)
t = ".";
} else
if (strlen(t) == 0)
t = ".";
(void)snprintf(path, sizeof(path), "%s/%s", t, *argv);
len = snprintf(path, sizeof(path), "%s/%s", t, *argv);
if (p)
*p++ = ':';
if (len >= sizeof(path))
continue; /* next p */
if (stat(path, &sb) == -1)
continue; /* next p */
if (!S_ISREG(sb.st_mode))
continue; /* next p */
if (access(path, X_OK) == -1)
continue; /* next p */
(void)printf("%s\n", path);
found++;
if (which)
break; /* next argv */
}
}
return ((found == 0) ? 3 : ((found >= argc) ? 0 : 2));
}
static void
usage(void)
{
(void)fprintf(stderr, "Usage: %s [-ap] program [...]\n", getprogname());
exit(1);
}

67
usr.bin/whereis/which.1 Normal file
View File

@ -0,0 +1,67 @@
.\" $NetBSD: which.1,v 1.2 2010/04/05 16:13:58 joerg Exp $
.\"
.\" Copyright (c) 1980, 1991 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. 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.
.\"
.\" from: @(#)which.1 6.3 (Berkeley) 4/23/91
.\" $NetBSD: which.1,v 1.2 2010/04/05 16:13:58 joerg Exp $
.\"
.Dd April 23, 1991
.Dt WHICH 1
.Os
.Sh NAME
.Nm which
.Nd "locate a program file in the users $PATH environment variable"
.Sh SYNOPSIS
.Nm
.Op Fl a
.Ar name
.Op Ar ...
.Sh DESCRIPTION
.Nm
takes a list of names and looks for the files which would be
executed had these names been given as commands.
Each argument is searched for along the user's
.Ev PATH .
.Pp
If the
.Fl a
flag is given,
.Nm
will continue to search the
.Ev PATH
until all instances of a program file are found.
.Sh HISTORY
The
.Nm
command appeared in
.Bx 3.0 .
.Sh BUGS
This implementation does not expand
.Xr csh 1
aliases, and is shell agnostic.
This is really a feature.