[mod.sources] v06i105: A "which" for non-BSD systems

sources-request@mirror.UUCP (08/13/86)

Submitted by: Paul Vixie <pyrnj!pyramid!hplabs!hpsemc!vix>
Mod.sources: Volume 6, Issue 105
Archive-name: att_which

[  I threw together a manpage.  This program doesn't handle aliases,
   but if you had aliases you wouldn't need this program, would you?
   --r$  ]


#!/bin/sh
# This is a shell archive.  Remove anything before this line,
# then unpack it by saving it in a file and typing "sh file".
# Contents:  README which.1 Makefile which.c
 
echo x - README
if test -f README ; then
    echo README exists, putting output in $$README
    OUT=$$README
else
    OUT=README
fi
sed 's/^XX//' > $OUT <<'@//E*O*F README//'
XX26-July-1985
XXVersion 1.0

XXThis is another C implementation of the 'which' command, written because
XX(a) is was easy, (b) I didn't know how to find the original, and (c) HP-UX
XXon the 9000 series 300 and 500 doesn't have it.

XXTo install, unshar into an empty directory, edit the Makefile to change the
XXDESTDIR (probably to /usr/local, /usr/local/bin or /usr/ucb), then su and
XXtype 'make install'.  If all goes well type 'make clean' or even 'rm *' if
XXyou saved the distribution kit.

XXQuestions, problems, flames, to:

XX	Paul Vixie
XX	ucbvax!dual!qantel!vixie!paul
@//E*O*F README//
chmod u=rw,g=rw,o= README
 
echo x - which.1
if test -f which.1 ; then
    echo which.1 exists, putting output in $$which.1
    OUT=$$which.1
else
    OUT=which.1
fi
sed 's/^XX//' > $OUT <<'@//E*O*F which.1//'
XX.TH WHICH 1 LOCAL
XX.SH NAME
XXwhich \- show full path of commands
XX.SH SYNOPSIS
XX.B which
XXprogname ...
XX.SH DESCRIPTION
XX.I Which
XXtakes a series of program names, and prints
XXout the full pathname of the program that the shell would call to
XXexecute it.
XXIt does this by simulating the shells searching of the
XX.B $PATH
XXenvironment variable.
XX.SH "SEE ALSO"
XXThe exec(2,3) family.
@//E*O*F which.1//
chmod u=rw,g=rw,o=rw which.1
 
echo x - Makefile
if test -f Makefile ; then
    echo Makefile exists, putting output in $$Makefile
    OUT=$$Makefile
else
    OUT=Makefile
fi
sed 's/^XX//' > $OUT <<'@//E*O*F Makefile//'
XX# makefile for 'which'
XX# vix 25jul86 [written]

XXCFLAGS		=	-O
XXDESTDIR		=	/mnt/local/bin

XX.c.o		:
XX			cc $(CFLAGS) -c $<

XXall		:	which

XXwhich		:	which.o
XX			cc -o which which.o

XXwhich.o		:	which.c

XXinstall		:	all
XX			mv which $(DESTDIR)
XX			chmod 775 $(DESTDIR)/which

XXclean		:
XX			rm -f which.o

XXkit		:	clean
XX			shar -bcsv README Makefile which.c > which.shar
@//E*O*F Makefile//
chmod u=rw,g=rw,o= Makefile
 
echo x - which.c
if test -f which.c ; then
    echo which.c exists, putting output in $$which.c
    OUT=$$which.c
else
    OUT=which.c
fi
sed 's/^XX//' > $OUT <<'@//E*O*F which.c//'
XX/* which - C version of the unix/csh 'which' command
XX * vix 23jul86 [written]
XX * vix 24jul86 [don't use dynamic memory]
XX */

XX#include <stdio.h>

XXstatic	char	*myname;

XXmain(argc, argv)
XXint	argc;
XXchar	*argv[];
XX{
XX	char	*getenv(), *path = getenv("PATH");

XX	myname = argv[0];
XX	for (argc--, argv++;  argc;  argc--, argv++)
XX		if (0 != which(*argv, path))
XX			exit(1);
XX	exit(0);
XX}

XXstatic which(name, path)
XXchar	*name, *path;
XX{
XX	char	test[1000], *pc, *malloc(), save;
XX	int	len, namelen = strlen(name), found;

XX	pc = path;
XX	found = 0;
XX	while (*pc != '\0' && found == 0)
XX	{
XX		len = 0;
XX		while (*pc != ':' && *pc != '\0')
XX		{
XX			len++;
XX			pc++;
XX		}

XX		save = *pc;
XX		*pc = '\0';
XX		sprintf(test, "%s/%s", pc-len, name);
XX		*pc = save;
XX		if (*pc)
XX			pc++;

XX		found = (0 == access(test, 01));	/* executable */
XX		if (found)
XX			puts(test);
XX	}
XX	if (found == 0)
XX	{
XX		printf("%s: no %s in (%s)\n", myname, name, path);
XX		return 1;
XX	}
XX	return 0;
XX}
@//E*O*F which.c//
chmod u=rw,g=rw,o=rw which.c
 
echo Inspecting for damage in transit...
temp=/tmp/sharin$$; dtemp=/tmp/sharout$$
trap "rm -f $temp $dtemp; exit" 0 1 2 3 15
cat > $temp <<\!!!
      16      90     558 README
      16      66     368 which.1
      25      57     354 Makefile
      57     152     925 which.c
     114     365    2205 total
!!!
wc  README which.1 Makefile which.c | sed 's=[^ ]*/==' | diff -b $temp - >$dtemp
if test -s $dtemp
then echo "Ouch [diff of wc output]:" ; cat $dtemp
else echo "No problems found."
fi
exit 0