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