[comp.sources.atari.st] v02i039: which -- Find programs using PATH env. variable

koreth@ssyx.ucsc.edu (Steven Grimm) (05/16/89)

Submitted-by: ravi@mcnc.org (Ravi Subrahmanyan)
Posting-number: Volume 2, Issue 39
Archive-name: which

Here's a trivial utility for the ST: basically the same as "which"
under unix.

#!/bin/sh
# shar:	Shell Archiver  (v1.22)
#
#	Run the following text with /bin/sh to create:
#	  README
#	  WHICH.C
#
sed 's/^X//' << 'SHAR_EOF' > README &&
XThis is an ST adaptation of a "which" program somone posted to 
Xnet.sources many years ago to replace the agonisingly slow
XBSD "which" script with a program.  I've used  it on our un*x
Xsystem for years, and finally decided I couldn't do without
Xit on the ST any longer..  Anyway, here it is (with source,
Xits trivial enough.)  To use it just say
X
X% which haha
X
Xand it'll add in the ".prg" or ".tos" or ".ttp" while checking for the file.  
X(it'll look for all three.)  It works with the Mark Williams C "msh" PATH,
Xbut you can easily modify it for your favourite shell, just change the comma
Xin the #define for DELIMETERat the top to whatever the delimeter is for
Xthe PATH in your shell.
X
X-ravi
X(ravi@mcnc.org)
SHAR_EOF
chmod 0600 README || echo "restore of README fails"
sed 's/^X//' << 'SHAR_EOF' > WHICH.C &&
X#define DELIMETER ','
X
X#include <stdio.h>
X 
Xchar *getenv();
Xchar *index();
X
Xint
Xmain(ac,av)
Xchar **av;
X{
X    char *path, *cp;
X    char buf[200];
X    char prog[200];
X    char patbuf[512];
X    int quit, none;
X
X    if (ac < 2) {
X        fprintf(stderr, "Usage: %s cmd [cmd, ..]\n", *av);
X        exit(1);
X    }
X    av[ac] = 0;
X    for(av++ ; *av; av++) {
X
X        quit = 0;
X        none = 1;
X	if ((path = getenv("PATH")) == NULL) {
X	    fprintf(stderr, "Null path.\n");
X	    exit(0);
X	}
X        strcpy(patbuf, path);
X        path = patbuf;
X        cp = path;
X
X        while(1) {
X            cp = index(path, DELIMETER);
X            if (cp == NULL) 
X                quit++;
X            else
X                *cp = '\0';
X
X	    if (strcmp(path,"") == (char *)NULL && quit == 0) {
X		sprintf(buf, "%s.\\%s", path, *av);
X	    } else 
X		sprintf(buf, "%s\\%s", path, *av);
X	
X/* fprintf(stderr,"Trying %s, path %s\n",buf,path); */
X
X            path = ++cp;
X
X            if (access(buf, 1) == 0) {
X                printf("%s\n", buf);
X                none = 0;
X            }
X	    
X            sprintf(prog, "%s.%s", buf, "prg");
X            if (access(prog, 1) == 0) {
X                printf("%s\n", prog);
X                none = 0;
X            }
X	    
X            sprintf(prog, "%s.%s", buf, "ttp");
X            if (access(prog, 1) == 0) {
X                printf("%s\n", prog);
X                none = 0;
X            }
X	    
X            sprintf(prog, "%s.%s", buf, "tos");
X            if (access(prog, 1) == 0) {
X                printf("%s\n", prog);
X                none = 0;
X            }
X            if (quit) {
X                if (none)
X                    printf("No %s in %s\n", *av, getenv("PATH"));
X                break;
X            }
X        }
X    }
X    exit(0);
X}
X
X
SHAR_EOF
chmod 0600 WHICH.C || echo "restore of WHICH.C fails"
exit 0