Relay-Version: version B 2.10 5/3/83; site utzoo.UUCP
Path: utzoo!mnetor!seismo!lll-crg!mordor!sri-spam!ames!ucla-cs!ulysses!dgc
From: dgc@ulysses.cs.ucla.edu (David G Cantor)
Newsgroups: comp.bugs.4bsd
Subject: Re: Builtin 'which' command for csh
Message-ID: <3409@curly.ucla-cs.UCLA.EDU>
Date: Fri, 12-Dec-86 16:26:00 EST
Article-I.D.: curly.3409
Posted: Fri Dec 12 16:26:00 1986
Date-Received: Mon, 15-Dec-86 07:04:02 EST
References: <1991@tektools.UUCP>
Reply-To: dgc@ulysses (David G Cantor)
Organization: UCLA Computer Science Department
Lines: 56
Keywords: /usr/ucb/which too slow, builtin which for csh

Summary:

Expires:

Sender:

Followup-To:

Distribution:


In article <1991@tektools.UUCP> tonyb@tektools.UUCP (Tony Birnseth)
gives a mod to the csh which supports builtin commands and aliases.

Here is a simplified which which does support aliases for the csh
(correctly) and is compatible with the Bourne shell.  It requires a one
line alias in the .cshrc file to handle aliases.  If not present it
functions as the Berkeley which.  It can be put in a users bin or in a
local bin (i.e. no mods to the "system" are required).

dgc

David G. Cantor
Internet:  dgc@cs.ucla.edu
UUCP:      ...!{ihnp4, randvax, sdcrdcf, ucbvax}!ucla-cs!dgc

/*
 *	Does a quick "which".
 *
 *  To get aliases when using the cshell, put the following
 *	in your ".cshrc":
 *
 *        alias which "this-function" whichalias \!^ \`alias \!^\` 
 *
 *	where "this-function" is replaced by the (completely-qualified)
 *  name of this function.
 *
 */

#include 
main(argc, argv) int argc; char **argv; {
  register char *path, *tp;
  char *getenv(), tmp[4096], file[4096];
  int	end = 0;
  if (argc < 2) exit(0);
  path = getenv("PATH");
  if (argc > 3) {
		if (strcmp(*++argv, "whichalias") || *argv[2] <= 32) exit(0);
		sprintf(file, "%s is an alias: ", *++argv);
		while (*++argv) {strcat(file, " "); strcat(file, *argv);}
		strcat(file, "\n");
		printf(file);
		exit(0);}
  if (argc == 3 && strcmp(*++argv, "whichalias")) exit(0);
  argv++;
  while(1) {
		tp = tmp;
		while(1) {
		  *tp++ = *path++;
			if (*(path-1) == ':') {*--tp = '\0'; break;}
			else if (*(path-1) == '\0') {end++; break;} }
		if (!access(tmp, 4)) {
	    sprintf(file, "%s/%s", tmp, *argv);
	    if (!access(file, 1)) printf("%s\n", file), exit(0);}
	  if (end) break;}
  printf("%s not found\n", *argv);
  exit(0);}