Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Posting-Version: version B 2.10.2 9/18/84; site tikal.UUCP
Path: utzoo!watmath!clyde!burl!ulysses!mhuxr!mhuxt!houxm!vax135!cornell!uw-beaver!tikal!larry
From: larry@tikal.UUCP (Larry J. Barello)
Newsgroups: net.sources
Subject: C version of "which"
Message-ID: <218@tikal.UUCP>
Date: Wed, 14-Aug-85 17:57:59 EDT
Article-I.D.: tikal.218
Posted: Wed Aug 14 17:57:59 1985
Date-Received: Sun, 18-Aug-85 01:37:36 EDT
Distribution: net
Organization: Teltone Corp., Kirkland, WA
Lines: 68

I suppose this has been done a million times before...

I was hacked off at the sluggishness of the "which" utility, so I
whipped up this C version while waiting for the shell version to
finish.  It produces identical output as the /usr/ucb version and
doesn't attempt to expand aliases (which is bogus unless one sets
their aliases in the .cshrc file).  It also issues a usage message if
one doesn't give it a list of files.

For those of you who don't have a bezerkly style unix, this program
should still work out Ok.

	Larry Barello

	..!uw-beaver!teltone!larry

---------------- Cut here, DON'T run through anything. -------

#include 
 
char *getenv();
char *index();

int
main(ac,av)
char **av;
{
    char *path, *cp;
    char buf[200];
    char patbuf[512];
    int quit, none;

    if (ac < 2) {
	fprintf(stderr, "Usage: %s cmd [cmd, ..]\n", *av);
	exit(1);
    }
    av[ac] = 0;
    for(av++ ; *av; av++) {

	quit = 0;
	none = 1;
	strcpy(patbuf, getenv("PATH"));
	path = patbuf;
	cp = path;

	while(1) {
	    cp = index(path, ':');
	    if (cp == NULL) 
		quit++;
	    else
		*cp = '\0';

	    sprintf(buf, "%s/%s", path, *av);
	    path = ++cp;

	    if (access(buf, 1) == 0) {
		printf("%s\n", buf);
		none = 0;
	    }
	    if (quit) {
		if (none)
		    printf("No %s in %s\n", *av, getenv("PATH"));
		break;
	    }
	}
    }
    exit(0);
}