xref: /aosp_15_r20/external/libpcap/install-sh (revision 8b26181f966a6af5cf6981a6f474313de533bb28)
1*8b26181fSAndroid Build Coastguard Worker#! /bin/sh
2*8b26181fSAndroid Build Coastguard Worker#
3*8b26181fSAndroid Build Coastguard Worker# install - install a program, script, or datafile
4*8b26181fSAndroid Build Coastguard Worker# This comes from X11R5 (mit/util/scripts/install.sh).
5*8b26181fSAndroid Build Coastguard Worker#
6*8b26181fSAndroid Build Coastguard Worker# Copyright 1991 by the Massachusetts Institute of Technology
7*8b26181fSAndroid Build Coastguard Worker#
8*8b26181fSAndroid Build Coastguard Worker# Permission to use, copy, modify, distribute, and sell this software and its
9*8b26181fSAndroid Build Coastguard Worker# documentation for any purpose is hereby granted without fee, provided that
10*8b26181fSAndroid Build Coastguard Worker# the above copyright notice appear in all copies and that both that
11*8b26181fSAndroid Build Coastguard Worker# copyright notice and this permission notice appear in supporting
12*8b26181fSAndroid Build Coastguard Worker# documentation, and that the name of M.I.T. not be used in advertising or
13*8b26181fSAndroid Build Coastguard Worker# publicity pertaining to distribution of the software without specific,
14*8b26181fSAndroid Build Coastguard Worker# written prior permission.  M.I.T. makes no representations about the
15*8b26181fSAndroid Build Coastguard Worker# suitability of this software for any purpose.  It is provided "as is"
16*8b26181fSAndroid Build Coastguard Worker# without express or implied warranty.
17*8b26181fSAndroid Build Coastguard Worker#
18*8b26181fSAndroid Build Coastguard Worker# Calling this script install-sh is preferred over install.sh, to prevent
19*8b26181fSAndroid Build Coastguard Worker# `make' implicit rules from creating a file called install from it
20*8b26181fSAndroid Build Coastguard Worker# when there is no Makefile.
21*8b26181fSAndroid Build Coastguard Worker#
22*8b26181fSAndroid Build Coastguard Worker# This script is compatible with the BSD install script, but was written
23*8b26181fSAndroid Build Coastguard Worker# from scratch.  It can only install one file at a time, a restriction
24*8b26181fSAndroid Build Coastguard Worker# shared with many OS's install programs.
25*8b26181fSAndroid Build Coastguard Worker
26*8b26181fSAndroid Build Coastguard Worker
27*8b26181fSAndroid Build Coastguard Worker# set DOITPROG to echo to test this script
28*8b26181fSAndroid Build Coastguard Worker
29*8b26181fSAndroid Build Coastguard Worker# Don't use :- since 4.3BSD and earlier shells don't like it.
30*8b26181fSAndroid Build Coastguard Workerdoit="${DOITPROG-}"
31*8b26181fSAndroid Build Coastguard Worker
32*8b26181fSAndroid Build Coastguard Worker
33*8b26181fSAndroid Build Coastguard Worker# put in absolute paths if you don't have them in your path; or use env. vars.
34*8b26181fSAndroid Build Coastguard Worker
35*8b26181fSAndroid Build Coastguard Workermvprog="${MVPROG-mv}"
36*8b26181fSAndroid Build Coastguard Workercpprog="${CPPROG-cp}"
37*8b26181fSAndroid Build Coastguard Workerchmodprog="${CHMODPROG-chmod}"
38*8b26181fSAndroid Build Coastguard Workerchownprog="${CHOWNPROG-chown}"
39*8b26181fSAndroid Build Coastguard Workerchgrpprog="${CHGRPPROG-chgrp}"
40*8b26181fSAndroid Build Coastguard Workerstripprog="${STRIPPROG-strip}"
41*8b26181fSAndroid Build Coastguard Workerrmprog="${RMPROG-rm}"
42*8b26181fSAndroid Build Coastguard Workermkdirprog="${MKDIRPROG-mkdir}"
43*8b26181fSAndroid Build Coastguard Worker
44*8b26181fSAndroid Build Coastguard Workertransformbasename=""
45*8b26181fSAndroid Build Coastguard Workertransform_arg=""
46*8b26181fSAndroid Build Coastguard Workerinstcmd="$mvprog"
47*8b26181fSAndroid Build Coastguard Workerchmodcmd="$chmodprog 0755"
48*8b26181fSAndroid Build Coastguard Workerchowncmd=""
49*8b26181fSAndroid Build Coastguard Workerchgrpcmd=""
50*8b26181fSAndroid Build Coastguard Workerstripcmd=""
51*8b26181fSAndroid Build Coastguard Workerrmcmd="$rmprog -f"
52*8b26181fSAndroid Build Coastguard Workermvcmd="$mvprog"
53*8b26181fSAndroid Build Coastguard Workersrc=""
54*8b26181fSAndroid Build Coastguard Workerdst=""
55*8b26181fSAndroid Build Coastguard Workerdir_arg=""
56*8b26181fSAndroid Build Coastguard Worker
57*8b26181fSAndroid Build Coastguard Workerwhile [ x"$1" != x ]; do
58*8b26181fSAndroid Build Coastguard Worker    case $1 in
59*8b26181fSAndroid Build Coastguard Worker	-c) instcmd="$cpprog"
60*8b26181fSAndroid Build Coastguard Worker	    shift
61*8b26181fSAndroid Build Coastguard Worker	    continue;;
62*8b26181fSAndroid Build Coastguard Worker
63*8b26181fSAndroid Build Coastguard Worker	-d) dir_arg=true
64*8b26181fSAndroid Build Coastguard Worker	    shift
65*8b26181fSAndroid Build Coastguard Worker	    continue;;
66*8b26181fSAndroid Build Coastguard Worker
67*8b26181fSAndroid Build Coastguard Worker	-m) chmodcmd="$chmodprog $2"
68*8b26181fSAndroid Build Coastguard Worker	    shift
69*8b26181fSAndroid Build Coastguard Worker	    shift
70*8b26181fSAndroid Build Coastguard Worker	    continue;;
71*8b26181fSAndroid Build Coastguard Worker
72*8b26181fSAndroid Build Coastguard Worker	-o) chowncmd="$chownprog $2"
73*8b26181fSAndroid Build Coastguard Worker	    shift
74*8b26181fSAndroid Build Coastguard Worker	    shift
75*8b26181fSAndroid Build Coastguard Worker	    continue;;
76*8b26181fSAndroid Build Coastguard Worker
77*8b26181fSAndroid Build Coastguard Worker	-g) chgrpcmd="$chgrpprog $2"
78*8b26181fSAndroid Build Coastguard Worker	    shift
79*8b26181fSAndroid Build Coastguard Worker	    shift
80*8b26181fSAndroid Build Coastguard Worker	    continue;;
81*8b26181fSAndroid Build Coastguard Worker
82*8b26181fSAndroid Build Coastguard Worker	-s) stripcmd="$stripprog"
83*8b26181fSAndroid Build Coastguard Worker	    shift
84*8b26181fSAndroid Build Coastguard Worker	    continue;;
85*8b26181fSAndroid Build Coastguard Worker
86*8b26181fSAndroid Build Coastguard Worker	-t=*) transformarg=`echo $1 | sed 's/-t=//'`
87*8b26181fSAndroid Build Coastguard Worker	    shift
88*8b26181fSAndroid Build Coastguard Worker	    continue;;
89*8b26181fSAndroid Build Coastguard Worker
90*8b26181fSAndroid Build Coastguard Worker	-b=*) transformbasename=`echo $1 | sed 's/-b=//'`
91*8b26181fSAndroid Build Coastguard Worker	    shift
92*8b26181fSAndroid Build Coastguard Worker	    continue;;
93*8b26181fSAndroid Build Coastguard Worker
94*8b26181fSAndroid Build Coastguard Worker	*)  if [ x"$src" = x ]
95*8b26181fSAndroid Build Coastguard Worker	    then
96*8b26181fSAndroid Build Coastguard Worker		src=$1
97*8b26181fSAndroid Build Coastguard Worker	    else
98*8b26181fSAndroid Build Coastguard Worker		# this colon is to work around a 386BSD /bin/sh bug
99*8b26181fSAndroid Build Coastguard Worker		:
100*8b26181fSAndroid Build Coastguard Worker		dst=$1
101*8b26181fSAndroid Build Coastguard Worker	    fi
102*8b26181fSAndroid Build Coastguard Worker	    shift
103*8b26181fSAndroid Build Coastguard Worker	    continue;;
104*8b26181fSAndroid Build Coastguard Worker    esac
105*8b26181fSAndroid Build Coastguard Workerdone
106*8b26181fSAndroid Build Coastguard Worker
107*8b26181fSAndroid Build Coastguard Workerif [ x"$src" = x ]
108*8b26181fSAndroid Build Coastguard Workerthen
109*8b26181fSAndroid Build Coastguard Worker	echo "install:	no input file specified"
110*8b26181fSAndroid Build Coastguard Worker	exit 1
111*8b26181fSAndroid Build Coastguard Workerelse
112*8b26181fSAndroid Build Coastguard Worker	true
113*8b26181fSAndroid Build Coastguard Workerfi
114*8b26181fSAndroid Build Coastguard Worker
115*8b26181fSAndroid Build Coastguard Workerif [ x"$dir_arg" != x ]; then
116*8b26181fSAndroid Build Coastguard Worker	dst=$src
117*8b26181fSAndroid Build Coastguard Worker	src=""
118*8b26181fSAndroid Build Coastguard Worker
119*8b26181fSAndroid Build Coastguard Worker	if [ -d $dst ]; then
120*8b26181fSAndroid Build Coastguard Worker		instcmd=:
121*8b26181fSAndroid Build Coastguard Worker	else
122*8b26181fSAndroid Build Coastguard Worker		instcmd=mkdir
123*8b26181fSAndroid Build Coastguard Worker	fi
124*8b26181fSAndroid Build Coastguard Workerelse
125*8b26181fSAndroid Build Coastguard Worker
126*8b26181fSAndroid Build Coastguard Worker# Waiting for this to be detected by the "$instcmd $src $dsttmp" command
127*8b26181fSAndroid Build Coastguard Worker# might cause directories to be created, which would be especially bad
128*8b26181fSAndroid Build Coastguard Worker# if $src (and thus $dsttmp) contains '*'.
129*8b26181fSAndroid Build Coastguard Worker
130*8b26181fSAndroid Build Coastguard Worker	if [ -f $src -o -d $src ]
131*8b26181fSAndroid Build Coastguard Worker	then
132*8b26181fSAndroid Build Coastguard Worker		true
133*8b26181fSAndroid Build Coastguard Worker	else
134*8b26181fSAndroid Build Coastguard Worker		echo "install:  $src does not exist"
135*8b26181fSAndroid Build Coastguard Worker		exit 1
136*8b26181fSAndroid Build Coastguard Worker	fi
137*8b26181fSAndroid Build Coastguard Worker
138*8b26181fSAndroid Build Coastguard Worker	if [ x"$dst" = x ]
139*8b26181fSAndroid Build Coastguard Worker	then
140*8b26181fSAndroid Build Coastguard Worker		echo "install:	no destination specified"
141*8b26181fSAndroid Build Coastguard Worker		exit 1
142*8b26181fSAndroid Build Coastguard Worker	else
143*8b26181fSAndroid Build Coastguard Worker		true
144*8b26181fSAndroid Build Coastguard Worker	fi
145*8b26181fSAndroid Build Coastguard Worker
146*8b26181fSAndroid Build Coastguard Worker# If destination is a directory, append the input filename; if your system
147*8b26181fSAndroid Build Coastguard Worker# does not like double slashes in filenames, you may need to add some logic
148*8b26181fSAndroid Build Coastguard Worker
149*8b26181fSAndroid Build Coastguard Worker	if [ -d $dst ]
150*8b26181fSAndroid Build Coastguard Worker	then
151*8b26181fSAndroid Build Coastguard Worker		dst="$dst"/`basename $src`
152*8b26181fSAndroid Build Coastguard Worker	else
153*8b26181fSAndroid Build Coastguard Worker		true
154*8b26181fSAndroid Build Coastguard Worker	fi
155*8b26181fSAndroid Build Coastguard Workerfi
156*8b26181fSAndroid Build Coastguard Worker
157*8b26181fSAndroid Build Coastguard Worker## this sed command emulates the dirname command
158*8b26181fSAndroid Build Coastguard Workerdstdir=`echo $dst | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
159*8b26181fSAndroid Build Coastguard Worker
160*8b26181fSAndroid Build Coastguard Worker# Make sure that the destination directory exists.
161*8b26181fSAndroid Build Coastguard Worker#  this part is taken from Noah Friedman's mkinstalldirs script
162*8b26181fSAndroid Build Coastguard Worker
163*8b26181fSAndroid Build Coastguard Worker# Skip lots of stat calls in the usual case.
164*8b26181fSAndroid Build Coastguard Workerif [ ! -d "$dstdir" ]; then
165*8b26181fSAndroid Build Coastguard WorkerdefaultIFS='
166*8b26181fSAndroid Build Coastguard Worker'
167*8b26181fSAndroid Build Coastguard WorkerIFS="${IFS-${defaultIFS}}"
168*8b26181fSAndroid Build Coastguard Worker
169*8b26181fSAndroid Build Coastguard WorkeroIFS="${IFS}"
170*8b26181fSAndroid Build Coastguard Worker# Some sh's can't handle IFS=/ for some reason.
171*8b26181fSAndroid Build Coastguard WorkerIFS='%'
172*8b26181fSAndroid Build Coastguard Workerset - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
173*8b26181fSAndroid Build Coastguard WorkerIFS="${oIFS}"
174*8b26181fSAndroid Build Coastguard Worker
175*8b26181fSAndroid Build Coastguard Workerpathcomp=''
176*8b26181fSAndroid Build Coastguard Worker
177*8b26181fSAndroid Build Coastguard Workerwhile [ $# -ne 0 ] ; do
178*8b26181fSAndroid Build Coastguard Worker	pathcomp="${pathcomp}${1}"
179*8b26181fSAndroid Build Coastguard Worker	shift
180*8b26181fSAndroid Build Coastguard Worker
181*8b26181fSAndroid Build Coastguard Worker	if [ ! -d "${pathcomp}" ] ;
182*8b26181fSAndroid Build Coastguard Worker        then
183*8b26181fSAndroid Build Coastguard Worker		$mkdirprog "${pathcomp}"
184*8b26181fSAndroid Build Coastguard Worker	else
185*8b26181fSAndroid Build Coastguard Worker		true
186*8b26181fSAndroid Build Coastguard Worker	fi
187*8b26181fSAndroid Build Coastguard Worker
188*8b26181fSAndroid Build Coastguard Worker	pathcomp="${pathcomp}/"
189*8b26181fSAndroid Build Coastguard Workerdone
190*8b26181fSAndroid Build Coastguard Workerfi
191*8b26181fSAndroid Build Coastguard Worker
192*8b26181fSAndroid Build Coastguard Workerif [ x"$dir_arg" != x ]
193*8b26181fSAndroid Build Coastguard Workerthen
194*8b26181fSAndroid Build Coastguard Worker	$doit $instcmd $dst &&
195*8b26181fSAndroid Build Coastguard Worker
196*8b26181fSAndroid Build Coastguard Worker	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dst; else true ; fi &&
197*8b26181fSAndroid Build Coastguard Worker	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dst; else true ; fi &&
198*8b26181fSAndroid Build Coastguard Worker	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dst; else true ; fi &&
199*8b26181fSAndroid Build Coastguard Worker	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dst; else true ; fi
200*8b26181fSAndroid Build Coastguard Workerelse
201*8b26181fSAndroid Build Coastguard Worker
202*8b26181fSAndroid Build Coastguard Worker# If we're going to rename the final executable, determine the name now.
203*8b26181fSAndroid Build Coastguard Worker
204*8b26181fSAndroid Build Coastguard Worker	if [ x"$transformarg" = x ]
205*8b26181fSAndroid Build Coastguard Worker	then
206*8b26181fSAndroid Build Coastguard Worker		dstfile=`basename $dst`
207*8b26181fSAndroid Build Coastguard Worker	else
208*8b26181fSAndroid Build Coastguard Worker		dstfile=`basename $dst $transformbasename |
209*8b26181fSAndroid Build Coastguard Worker			sed $transformarg`$transformbasename
210*8b26181fSAndroid Build Coastguard Worker	fi
211*8b26181fSAndroid Build Coastguard Worker
212*8b26181fSAndroid Build Coastguard Worker# don't allow the sed command to completely eliminate the filename
213*8b26181fSAndroid Build Coastguard Worker
214*8b26181fSAndroid Build Coastguard Worker	if [ x"$dstfile" = x ]
215*8b26181fSAndroid Build Coastguard Worker	then
216*8b26181fSAndroid Build Coastguard Worker		dstfile=`basename $dst`
217*8b26181fSAndroid Build Coastguard Worker	else
218*8b26181fSAndroid Build Coastguard Worker		true
219*8b26181fSAndroid Build Coastguard Worker	fi
220*8b26181fSAndroid Build Coastguard Worker
221*8b26181fSAndroid Build Coastguard Worker# Make a temp file name in the proper directory.
222*8b26181fSAndroid Build Coastguard Worker
223*8b26181fSAndroid Build Coastguard Worker	dsttmp=$dstdir/#inst.$$#
224*8b26181fSAndroid Build Coastguard Worker
225*8b26181fSAndroid Build Coastguard Worker# Move or copy the file name to the temp name
226*8b26181fSAndroid Build Coastguard Worker
227*8b26181fSAndroid Build Coastguard Worker	$doit $instcmd $src $dsttmp &&
228*8b26181fSAndroid Build Coastguard Worker
229*8b26181fSAndroid Build Coastguard Worker	trap "rm -f ${dsttmp}" 0 &&
230*8b26181fSAndroid Build Coastguard Worker
231*8b26181fSAndroid Build Coastguard Worker# and set any options; do chmod last to preserve setuid bits
232*8b26181fSAndroid Build Coastguard Worker
233*8b26181fSAndroid Build Coastguard Worker# If any of these fail, we abort the whole thing.  If we want to
234*8b26181fSAndroid Build Coastguard Worker# ignore errors from any of these, just make sure not to ignore
235*8b26181fSAndroid Build Coastguard Worker# errors from the above "$doit $instcmd $src $dsttmp" command.
236*8b26181fSAndroid Build Coastguard Worker
237*8b26181fSAndroid Build Coastguard Worker	if [ x"$chowncmd" != x ]; then $doit $chowncmd $dsttmp; else true;fi &&
238*8b26181fSAndroid Build Coastguard Worker	if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd $dsttmp; else true;fi &&
239*8b26181fSAndroid Build Coastguard Worker	if [ x"$stripcmd" != x ]; then $doit $stripcmd $dsttmp; else true;fi &&
240*8b26181fSAndroid Build Coastguard Worker	if [ x"$chmodcmd" != x ]; then $doit $chmodcmd $dsttmp; else true;fi &&
241*8b26181fSAndroid Build Coastguard Worker
242*8b26181fSAndroid Build Coastguard Worker# Now rename the file to the real destination.
243*8b26181fSAndroid Build Coastguard Worker
244*8b26181fSAndroid Build Coastguard Worker	$doit $rmcmd -f $dstdir/$dstfile &&
245*8b26181fSAndroid Build Coastguard Worker	$doit $mvcmd $dsttmp $dstdir/$dstfile
246*8b26181fSAndroid Build Coastguard Worker
247*8b26181fSAndroid Build Coastguard Workerfi &&
248*8b26181fSAndroid Build Coastguard Worker
249*8b26181fSAndroid Build Coastguard Worker
250*8b26181fSAndroid Build Coastguard Workerexit 0
251