[net.sources] New version of install

silvert@dalcs.UUCP (Bill Silvert) (12/30/86)

Here is a new version of the install utility for copying a file into the
appropriate directory.  It uses a modified version of the which(1)
utility which was posted a while back.  That version of which is very
fast, and so is this version of install.

The options are completely different from those in the old version, so
check the documention in the shell script before using.  I haven't
bothered with the chmod commands (other than +x) since I don't use them,
but they are trivial to add.

Contents: install script and which.c


#! /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:
#	install
#	which.c
# This archive created: Mon Dec 29 18:37:05 1986
# By:	Bill Silvert (Marine Ecology Lab.)
export PATH; PATH=/bin:/usr/bin:$PATH
if test -f 'install'
then
	echo shar: "will not over-write existing file 'install'"
else
cat << \SHAR_EOF > 'install'
:
# install files on existing paths
USAGE="Usage: `basename $0` [-mrsx] [-d dir] [-f file] filename"
ACT=cp
REMOVE=:
STRIP=:

# parse options, -s for strip, -m for move, -r for remove, -x for chmod +x
# -d forces installation in directory, -f gives new file name
set -- `getopt mrsxd:f: $*`
if [ $? != 0 ]
then echo $USAGE; exit 2
fi
for i
do	case $i in
	-m)	ACT=mv; shift;;
	-r)	REMOVE=rm; shift;;
	-s)	STRIP=strip; shift;;
	-x)	STRIP='chmod +x'; shift;;
	-d)	DIR=$2; shift 2;;
	-f)	OLD=$2; shift 2;;
	--)	shift; break;;
	esac
done

for i
do
	if [ -f $i ]
	then
		$STRIP $i

		if [ $DIR ]
		then
			if [ -d $DIR ]
			then
				OLD=$DIR/`basename $i`
			else
				echo "$DIR is not a directory"
				exit 2
			fi
		else
			if [ $OLD ]
			then
				if [ $# -ne 1 ]
				then
					echo "-f option only installs one file"
					exit 2
				fi
			else
				OLD=`which -oq $i`
				if [ $OLD ]
				then
					:
				else
					echo "old version of $i not found"
					exit 3
				fi
			fi
		fi

		$ACT $i $OLD
		$REMOVE $i
		echo "$i installed as $OLD"
	else
		echo "file $i not found"
		exit 4
	fi
done
SHAR_EOF
chmod +x 'install'
fi
if test -f 'which.c'
then
	echo shar: "will not over-write existing file 'which.c'"
else
cat << \SHAR_EOF > 'which.c'
#include <stdio.h>
static char SCCSID[] = "@(#)which.c	Ver. 1.1, 86/12/29 13:46:31";
char *progname;
int all = 0;		/* find all occurrences */
int cwd = 1;		/* check cwd */
int warn = 1;		/* give a warning if not found */

main(argc,argv)
int argc;
char *argv[];
{
	char	*getenv(), *path = getenv("PATH");
	int c, getopt();
	extern int optind;
	extern char *optarg;

	progname = *argv;

	while((c = getopt(argc, argv, "aohq")) != EOF)
		switch(c) {
		case 'a':
			all++;
			break;
		case 'o':
			cwd = 0;
			break;
		case 'q':
			warn = 0;
			break;
		case 'h':
		default:
			help();
			exit(1);
		}
	switch(argc - optind) {
	case 0:
		help();
		break;
	default:
		for(; optind<argc; optind++) {
			if(cwd) putchar('\n');
			which(argv[optind], path);
		}
	}
	if(cwd) putchar('\n');
	exit(0);
}

help()
{
	fprintf(stderr, "Usage: %s [-ao] file [...]\n", progname);
	fprintf(stderr, "\t\tOptions are:\n");
	fprintf(stderr, "\t-a\tfind all occurrences in path\n");
	fprintf(stderr, "\t-o\tfind only first occurrence outside cwd\n");
}

/* which - C version of the unix/csh 'which' command
 * vix 23jul86 [written]
 * vix 24jul86 [don't use dynamic memory]
 */

which(name, path)
char	*name, *path;
{
	char	test[1000], *pc, *malloc(), save;
	int	len, namelen = strlen(name), found =0;
	int	count = 0;

	pc = path;
	if(*path == ':' && cwd && access(name, 01) == 0) { /* in cwd! */
		printf("./%s\n", name); /* go on to find other location */
		count++;
	}
	while (*pc != '\0' && (found == 0 || all) )
	{
		len = 0;
		while (*pc != ':' && *pc != '\0')
		{
			len++;
			pc++;
		}

		save = *pc;
		*pc = '\0';
		sprintf(test, "%s/%s", pc-len, name);
		*pc = save;
		if (*pc)
			pc++;

		found = (0 == access(test, 01));	/* executable */
		if (found) {
			puts(test);
			count++;
		}
	}
	if (count == 0 && warn)
	{
		printf("No %s (%s)\n", name, path);
	}
	return(count);
}
SHAR_EOF
fi
exit 0
#	End of shell archive