diff --git a/pathtools/GNUmakefile b/pathtools/GNUmakefile new file mode 100644 index 0000000..0a2fbd4 --- /dev/null +++ b/pathtools/GNUmakefile @@ -0,0 +1,49 @@ +include GNUmakevars + +TARGETS = apppath preppath delpath apppath.1 preppath.1 delpath.1 +CONFDIR=../../configure +CONFDIR_exists=$(shell [ -d $(CONFDIR) ] && echo ok) + +all: configure $(TARGETS) + +clean: + rm -f $(TARGETS) *.bak core foo bar baz *.ps + +distclean: clean + rm -f customize + +install: $(BINDIR) \ + $(BINDIR)/apppath \ + $(BINDIR)/preppath \ + $(BINDIR)/delpath \ + $(MAN1DIR)/apppath.1 \ + $(MAN1DIR)/preppath.1 \ + $(MAN1DIR)/delpath.1 \ + + +%.1: %.pl + pod2man $< > $@ + +%: %.pl customize + sh ./customize < $< > $@ + chmod +x $@ + +%: %.sh customize + sh ./customize < $< > $@ + chmod +x $@ + +customize: configure + sh ./configure + +$(BINDIR): + mkdir -p $@ + +ifeq ($(CONFDIR_exists),ok) + +configure: $(CONFDIR)/start $(CONFDIR)/perl $(CONFDIR)/finish + cat $^ > $@ + +endif + + +include GNUmakerules diff --git a/pathtools/apppath.pl b/pathtools/apppath.pl new file mode 100755 index 0000000..261991d --- /dev/null +++ b/pathtools/apppath.pl @@ -0,0 +1,53 @@ +#!@@@perl@@@ -w + +=head1 NAME + +apppath - append directories to path + +=head1 SYNOPSIS + +apppath [-c] directory... + +=head1 DESCRIPTION + +apppath appends the directories given as arguments to the PATH and +prints the new path on stdout. + +=head1 OPTIONS + +=over 4 + +=item B<-c> + +Check whether the directories exist before adding them. Nonexistent +directories are silently ignored. + +=back + +=head1 AUTHOR + +Peter J. Holzer . + +=cut + +use strict; +my $path = $ENV{PATH}; +my @path = split(/:/, $path); +my $check; + +if ($ARGV[0] eq '-c') { + $check = 1; + shift; +} + +if ($#ARGV == 0 && $ARGV[0] =~ /:/) { + @ARGV = split(/:/, $ARGV[0]); +} + +nd: for my $nd (@ARGV) { + for my $od (@path) { + next nd if ($nd eq $od); + } + push @path, $nd if (!$check || -d $nd); +} +print join(':', @path), "\n"; diff --git a/pathtools/delpath.pl b/pathtools/delpath.pl new file mode 100755 index 0000000..dc73ea2 --- /dev/null +++ b/pathtools/delpath.pl @@ -0,0 +1,38 @@ +#!@@@perl@@@ -w + +=head1 NAME + +delpath - delete directories from path + +=head1 SYNOPSIS + +delpath directory... + +=head1 DESCRIPTION + +delpath deletes the directories given as arguments from the PATH and +prints the new path on stdout. + +=head1 AUTHOR + +Peter J. Holzer . + +=cut + +use strict; + +if ($#ARGV == 0 && $ARGV[0] =~ /:/) { + @ARGV = split(/:/, $ARGV[0]); +} + +my $path = $ENV{PATH}; +my @path = split(/:/, $path); + +my %del; +for (@del{@ARGV}) { + $_ = 1; +} + +@path = grep { !$del{$_} } @path; + +print join(':', @path), "\n"; diff --git a/pathtools/preppath.pl b/pathtools/preppath.pl new file mode 100755 index 0000000..9e3895f --- /dev/null +++ b/pathtools/preppath.pl @@ -0,0 +1,56 @@ +#!@@@perl@@@ -w + +=head1 NAME + +preppath - prepend directories to path + +=head1 SYNOPSIS + +preppath [-c] directory... + +=head1 DESCRIPTION + +apppath prepends the directories given as arguments to the PATH, +eliminates any duplicates prints the new path on stdout. + +=head1 OPTIONS + +=over 4 + +=item B<-c> + +Check whether the directories exist before adding them. Nonexistent +directories are silently ignored. + +=back + +=head1 AUTHOR + +Peter J. Holzer . + +=cut + +use strict; +my $path = $ENV{PATH}; +my @path = split(/:/, $path); +my $check; + +if ($ARGV[0] eq '-c') { + $check = 1; + shift; +} + +if ($#ARGV == 0 && $ARGV[0] =~ /:/) { + @ARGV = split(/:/, $ARGV[0]); +} + +my %seen; +my @newpath; + +for my $d (@ARGV, @path) { + if (!$seen{$d} && (!$check || -d $d)) { + push @newpath, $d; + $seen{$d} = 1; + } +} +print join(':', @newpath), "\n";