Path Program

Mark Keating markk at censor.UUCP
Fri Jun 16 06:20:20 AEST 1989



   After trying to work on another system I was slightly dissapointed
to find that there was no 'path' program to be found anywhere.
So I wrote one  -- hope you find it useful.

#! /bin/sh
# This is a shell archive, meaning:
# 1. Remove everything above the #! /bin/sh line.
# 2. Save the resulting text in a file.
# 3. Execute the file with /bin/sh (not csh) to create the files:
#	path.1
#	path.c
# This archive created: Thu Jun 15 16:04:06 1989
export PATH; PATH=/bin:$PATH
if test -f 'path.1'
then
	echo shar: will not over-write existing file "'path.1'"
else
cat << \SHAR_EOF > 'path.1'
.TH PATH 1
.SH NAME
path \- locate the executable for a command
.SH SYNOPSIS
/usr/local/bin/path [ -options ] command(s)
.I file
.SH DESCRIPTION
.I Path
will report on the location of
.I file,
searching every directory in the path (as defined in the
$PATH variable), much like the operating system does when searching
$PATH to execute a program.  It is handy for finding out where in your path
a command resides. Any options specified are passed with located names to the
.I ls
command.
.PP
.SH CREDITS
This utility was inspired by the
.I findpath
utility posted by Ed Carp,
and re-generated into
.I path
by Mark Keating.
SHAR_EOF
fi # end of overwriting check
if test -f 'path.c'
then
	echo shar: will not over-write existing file "'path.c'"
else
cat << \SHAR_EOF > 'path.c'
/* opt: -O -s -o path
 
 	path [ -options ] command [ command... ] 
 
 		find the path of a command
 		if options specified then they are passed through to `ls'


		path  Version 1.0   created by  Mark Keating - 1989
*/

#include <stdio.h>
#include <string.h>
#include <sys/types.h>
#include <dirent.h>

	DIR				*d_fp;
	struct dirent	*d_ent;
	char			*d_nam;
	char			*ls_opt, ls_buf[256];
	char			*path,   path_list[512];

	char *getenv();

main(argc, argv)
int  argc;
char 	  *argv[];
{
	register int	i, found;
	register char	*ptr;

	if (argc < 2) {
		fprintf(stderr, "Usage: path [-lsopt] command [...]\n");
		exit(1);
	}

	if ((path=getenv("PATH")) == NULL) {
		fprintf(stderr, "PATH not set!\n");
		exit(1);
	}

	for (i=1; i < argc; i++) {
		if (argv[i][0] == '-') {	/* ls option passed */
			ls_opt = argv[i]; continue;
		}

		strcpy(path_list, ".:");  strcat(path_list, path);
		for (ptr = path_list, found = 0;
				!found && (d_nam=strtok(ptr, ":")) != NULL; ptr = NULL) {
			if ((d_fp=opendir(d_nam)) == (DIR *)NULL) {
				perror(d_nam); continue;
			}
			while ((d_ent=readdir(d_fp)) != (struct dirent *)NULL) {
				if (strcmp(d_ent->d_name, argv[i]) == 0) {
					if (ls_opt == NULL) {
						printf("%s/%s\n", d_nam, argv[i]);
					}
					else {
						sprintf(ls_buf, "ls %s %s/%s\n",
											ls_opt, d_nam, argv[i]);
						system(ls_buf);
					}
					found = 1; break;
				}
			}
			closedir(d_fp);
		}
	}
}
SHAR_EOF
fi # end of overwriting check
#	End of shell archive
exit 0



More information about the Alt.sources mailing list