Builtin 'which' command for csh

David G Cantor dgc at ulysses.cs.ucla.edu
Sat Dec 13 07:26:00 AEST 1986


Summary:

Expires:

Sender:

Followup-To:

Distribution:


In article <1991 at tektools.UUCP> tonyb at 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 at 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 <stdio.h>
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);}



More information about the Comp.bugs.4bsd.ucb-fixes mailing list