install-sh revision 1.1.1.1 1 1.1 haad #!/bin/sh
2 1.1 haad # install - install a program, script, or datafile
3 1.1 haad
4 1.1 haad scriptversion=2006-10-14.15
5 1.1 haad
6 1.1 haad # This originates from X11R5 (mit/util/scripts/install.sh), which was
7 1.1 haad # later released in X11R6 (xc/config/util/install.sh) with the
8 1.1 haad # following copyright and license.
9 1.1 haad #
10 1.1 haad # Copyright (C) 1994 X Consortium
11 1.1 haad #
12 1.1 haad # Permission is hereby granted, free of charge, to any person obtaining a copy
13 1.1 haad # of this software and associated documentation files (the "Software"), to
14 1.1 haad # deal in the Software without restriction, including without limitation the
15 1.1 haad # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16 1.1 haad # sell copies of the Software, and to permit persons to whom the Software is
17 1.1 haad # furnished to do so, subject to the following conditions:
18 1.1 haad #
19 1.1 haad # The above copyright notice and this permission notice shall be included in
20 1.1 haad # all copies or substantial portions of the Software.
21 1.1 haad #
22 1.1 haad # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23 1.1 haad # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24 1.1 haad # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25 1.1 haad # X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26 1.1 haad # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27 1.1 haad # TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 1.1 haad #
29 1.1 haad # Except as contained in this notice, the name of the X Consortium shall not
30 1.1 haad # be used in advertising or otherwise to promote the sale, use or other deal-
31 1.1 haad # ings in this Software without prior written authorization from the X Consor-
32 1.1 haad # tium.
33 1.1 haad #
34 1.1 haad #
35 1.1 haad # FSF changes to this file are in the public domain.
36 1.1 haad #
37 1.1 haad # Calling this script install-sh is preferred over install.sh, to prevent
38 1.1 haad # `make' implicit rules from creating a file called install from it
39 1.1 haad # when there is no Makefile.
40 1.1 haad #
41 1.1 haad # This script is compatible with the BSD install script, but was written
42 1.1 haad # from scratch.
43 1.1 haad
44 1.1 haad nl='
45 1.1 haad '
46 1.1 haad IFS=" "" $nl"
47 1.1 haad
48 1.1 haad # set DOITPROG to echo to test this script
49 1.1 haad
50 1.1 haad # Don't use :- since 4.3BSD and earlier shells don't like it.
51 1.1 haad doit="${DOITPROG-}"
52 1.1 haad if test -z "$doit"; then
53 1.1 haad doit_exec=exec
54 1.1 haad else
55 1.1 haad doit_exec=$doit
56 1.1 haad fi
57 1.1 haad
58 1.1 haad # Put in absolute file names if you don't have them in your path;
59 1.1 haad # or use environment vars.
60 1.1 haad
61 1.1 haad mvprog="${MVPROG-mv}"
62 1.1 haad cpprog="${CPPROG-cp}"
63 1.1 haad chmodprog="${CHMODPROG-chmod}"
64 1.1 haad chownprog="${CHOWNPROG-chown}"
65 1.1 haad chgrpprog="${CHGRPPROG-chgrp}"
66 1.1 haad stripprog="${STRIPPROG-strip}"
67 1.1 haad rmprog="${RMPROG-rm}"
68 1.1 haad mkdirprog="${MKDIRPROG-mkdir}"
69 1.1 haad
70 1.1 haad posix_glob=
71 1.1 haad posix_mkdir=
72 1.1 haad
73 1.1 haad # Desired mode of installed file.
74 1.1 haad mode=0755
75 1.1 haad
76 1.1 haad chmodcmd=$chmodprog
77 1.1 haad chowncmd=
78 1.1 haad chgrpcmd=
79 1.1 haad stripcmd=
80 1.1 haad rmcmd="$rmprog -f"
81 1.1 haad mvcmd="$mvprog"
82 1.1 haad src=
83 1.1 haad dst=
84 1.1 haad dir_arg=
85 1.1 haad dstarg=
86 1.1 haad no_target_directory=
87 1.1 haad
88 1.1 haad usage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89 1.1 haad or: $0 [OPTION]... SRCFILES... DIRECTORY
90 1.1 haad or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91 1.1 haad or: $0 [OPTION]... -d DIRECTORIES...
92 1.1 haad
93 1.1 haad In the 1st form, copy SRCFILE to DSTFILE.
94 1.1 haad In the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95 1.1 haad In the 4th, create DIRECTORIES.
96 1.1 haad
97 1.1 haad Options:
98 1.1 haad -c (ignored)
99 1.1 haad -d create directories instead of installing files.
100 1.1 haad -g GROUP $chgrpprog installed files to GROUP.
101 1.1 haad -m MODE $chmodprog installed files to MODE.
102 1.1 haad -o USER $chownprog installed files to USER.
103 1.1 haad -s $stripprog installed files.
104 1.1 haad -t DIRECTORY install into DIRECTORY.
105 1.1 haad -T report an error if DSTFILE is a directory.
106 1.1 haad --help display this help and exit.
107 1.1 haad --version display version info and exit.
108 1.1 haad
109 1.1 haad Environment variables override the default commands:
110 1.1 haad CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
111 1.1 haad "
112 1.1 haad
113 1.1 haad while test $# -ne 0; do
114 1.1 haad case $1 in
115 1.1 haad -c) shift
116 1.1 haad continue;;
117 1.1 haad
118 1.1 haad -d) dir_arg=true
119 1.1 haad shift
120 1.1 haad continue;;
121 1.1 haad
122 1.1 haad -g) chgrpcmd="$chgrpprog $2"
123 1.1 haad shift
124 1.1 haad shift
125 1.1 haad continue;;
126 1.1 haad
127 1.1 haad --help) echo "$usage"; exit $?;;
128 1.1 haad
129 1.1 haad -m) mode=$2
130 1.1 haad shift
131 1.1 haad shift
132 1.1 haad case $mode in
133 1.1 haad *' '* | *' '* | *'
134 1.1 haad '* | *'*'* | *'?'* | *'['*)
135 1.1 haad echo "$0: invalid mode: $mode" >&2
136 1.1 haad exit 1;;
137 1.1 haad esac
138 1.1 haad continue;;
139 1.1 haad
140 1.1 haad -o) chowncmd="$chownprog $2"
141 1.1 haad shift
142 1.1 haad shift
143 1.1 haad continue;;
144 1.1 haad
145 1.1 haad -s) stripcmd=$stripprog
146 1.1 haad shift
147 1.1 haad continue;;
148 1.1 haad
149 1.1 haad -t) dstarg=$2
150 1.1 haad shift
151 1.1 haad shift
152 1.1 haad continue;;
153 1.1 haad
154 1.1 haad -T) no_target_directory=true
155 1.1 haad shift
156 1.1 haad continue;;
157 1.1 haad
158 1.1 haad --version) echo "$0 $scriptversion"; exit $?;;
159 1.1 haad
160 1.1 haad --) shift
161 1.1 haad break;;
162 1.1 haad
163 1.1 haad -*) echo "$0: invalid option: $1" >&2
164 1.1 haad exit 1;;
165 1.1 haad
166 1.1 haad *) break;;
167 1.1 haad esac
168 1.1 haad done
169 1.1 haad
170 1.1 haad if test $# -ne 0 && test -z "$dir_arg$dstarg"; then
171 1.1 haad # When -d is used, all remaining arguments are directories to create.
172 1.1 haad # When -t is used, the destination is already specified.
173 1.1 haad # Otherwise, the last argument is the destination. Remove it from $@.
174 1.1 haad for arg
175 1.1 haad do
176 1.1 haad if test -n "$dstarg"; then
177 1.1 haad # $@ is not empty: it contains at least $arg.
178 1.1 haad set fnord "$@" "$dstarg"
179 1.1 haad shift # fnord
180 1.1 haad fi
181 1.1 haad shift # arg
182 1.1 haad dstarg=$arg
183 1.1 haad done
184 1.1 haad fi
185 1.1 haad
186 1.1 haad if test $# -eq 0; then
187 1.1 haad if test -z "$dir_arg"; then
188 1.1 haad echo "$0: no input file specified." >&2
189 1.1 haad exit 1
190 1.1 haad fi
191 1.1 haad # It's OK to call `install-sh -d' without argument.
192 1.1 haad # This can happen when creating conditional directories.
193 1.1 haad exit 0
194 1.1 haad fi
195 1.1 haad
196 1.1 haad if test -z "$dir_arg"; then
197 1.1 haad trap '(exit $?); exit' 1 2 13 15
198 1.1 haad
199 1.1 haad # Set umask so as not to create temps with too-generous modes.
200 1.1 haad # However, 'strip' requires both read and write access to temps.
201 1.1 haad case $mode in
202 1.1 haad # Optimize common cases.
203 1.1 haad *644) cp_umask=133;;
204 1.1 haad *755) cp_umask=22;;
205 1.1 haad
206 1.1 haad *[0-7])
207 1.1 haad if test -z "$stripcmd"; then
208 1.1 haad u_plus_rw=
209 1.1 haad else
210 1.1 haad u_plus_rw='% 200'
211 1.1 haad fi
212 1.1 haad cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
213 1.1 haad *)
214 1.1 haad if test -z "$stripcmd"; then
215 1.1 haad u_plus_rw=
216 1.1 haad else
217 1.1 haad u_plus_rw=,u+rw
218 1.1 haad fi
219 1.1 haad cp_umask=$mode$u_plus_rw;;
220 1.1 haad esac
221 1.1 haad fi
222 1.1 haad
223 1.1 haad for src
224 1.1 haad do
225 1.1 haad # Protect names starting with `-'.
226 1.1 haad case $src in
227 1.1 haad -*) src=./$src ;;
228 1.1 haad esac
229 1.1 haad
230 1.1 haad if test -n "$dir_arg"; then
231 1.1 haad dst=$src
232 1.1 haad dstdir=$dst
233 1.1 haad test -d "$dstdir"
234 1.1 haad dstdir_status=$?
235 1.1 haad else
236 1.1 haad
237 1.1 haad # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
238 1.1 haad # might cause directories to be created, which would be especially bad
239 1.1 haad # if $src (and thus $dsttmp) contains '*'.
240 1.1 haad if test ! -f "$src" && test ! -d "$src"; then
241 1.1 haad echo "$0: $src does not exist." >&2
242 1.1 haad exit 1
243 1.1 haad fi
244 1.1 haad
245 1.1 haad if test -z "$dstarg"; then
246 1.1 haad echo "$0: no destination specified." >&2
247 1.1 haad exit 1
248 1.1 haad fi
249 1.1 haad
250 1.1 haad dst=$dstarg
251 1.1 haad # Protect names starting with `-'.
252 1.1 haad case $dst in
253 1.1 haad -*) dst=./$dst ;;
254 1.1 haad esac
255 1.1 haad
256 1.1 haad # If destination is a directory, append the input filename; won't work
257 1.1 haad # if double slashes aren't ignored.
258 1.1 haad if test -d "$dst"; then
259 1.1 haad if test -n "$no_target_directory"; then
260 1.1 haad echo "$0: $dstarg: Is a directory" >&2
261 1.1 haad exit 1
262 1.1 haad fi
263 1.1 haad dstdir=$dst
264 1.1 haad dst=$dstdir/`basename "$src"`
265 1.1 haad dstdir_status=0
266 1.1 haad else
267 1.1 haad # Prefer dirname, but fall back on a substitute if dirname fails.
268 1.1 haad dstdir=`
269 1.1 haad (dirname "$dst") 2>/dev/null ||
270 1.1 haad expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
271 1.1 haad X"$dst" : 'X\(//\)[^/]' \| \
272 1.1 haad X"$dst" : 'X\(//\)$' \| \
273 1.1 haad X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
274 1.1 haad echo X"$dst" |
275 1.1 haad sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
276 1.1 haad s//\1/
277 1.1 haad q
278 1.1 haad }
279 1.1 haad /^X\(\/\/\)[^/].*/{
280 1.1 haad s//\1/
281 1.1 haad q
282 1.1 haad }
283 1.1 haad /^X\(\/\/\)$/{
284 1.1 haad s//\1/
285 1.1 haad q
286 1.1 haad }
287 1.1 haad /^X\(\/\).*/{
288 1.1 haad s//\1/
289 1.1 haad q
290 1.1 haad }
291 1.1 haad s/.*/./; q'
292 1.1 haad `
293 1.1 haad
294 1.1 haad test -d "$dstdir"
295 1.1 haad dstdir_status=$?
296 1.1 haad fi
297 1.1 haad fi
298 1.1 haad
299 1.1 haad obsolete_mkdir_used=false
300 1.1 haad
301 1.1 haad if test $dstdir_status != 0; then
302 1.1 haad case $posix_mkdir in
303 1.1 haad '')
304 1.1 haad # Create intermediate dirs using mode 755 as modified by the umask.
305 1.1 haad # This is like FreeBSD 'install' as of 1997-10-28.
306 1.1 haad umask=`umask`
307 1.1 haad case $stripcmd.$umask in
308 1.1 haad # Optimize common cases.
309 1.1 haad *[2367][2367]) mkdir_umask=$umask;;
310 1.1 haad .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
311 1.1 haad
312 1.1 haad *[0-7])
313 1.1 haad mkdir_umask=`expr $umask + 22 \
314 1.1 haad - $umask % 100 % 40 + $umask % 20 \
315 1.1 haad - $umask % 10 % 4 + $umask % 2
316 1.1 haad `;;
317 1.1 haad *) mkdir_umask=$umask,go-w;;
318 1.1 haad esac
319 1.1 haad
320 1.1 haad # With -d, create the new directory with the user-specified mode.
321 1.1 haad # Otherwise, rely on $mkdir_umask.
322 1.1 haad if test -n "$dir_arg"; then
323 1.1 haad mkdir_mode=-m$mode
324 1.1 haad else
325 1.1 haad mkdir_mode=
326 1.1 haad fi
327 1.1 haad
328 1.1 haad posix_mkdir=false
329 1.1 haad case $umask in
330 1.1 haad *[123567][0-7][0-7])
331 1.1 haad # POSIX mkdir -p sets u+wx bits regardless of umask, which
332 1.1 haad # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
333 1.1 haad ;;
334 1.1 haad *)
335 1.1 haad tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
336 1.1 haad trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
337 1.1 haad
338 1.1 haad if (umask $mkdir_umask &&
339 1.1 haad exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
340 1.1 haad then
341 1.1 haad if test -z "$dir_arg" || {
342 1.1 haad # Check for POSIX incompatibilities with -m.
343 1.1 haad # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
344 1.1 haad # other-writeable bit of parent directory when it shouldn't.
345 1.1 haad # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
346 1.1 haad ls_ld_tmpdir=`ls -ld "$tmpdir"`
347 1.1 haad case $ls_ld_tmpdir in
348 1.1 haad d????-?r-*) different_mode=700;;
349 1.1 haad d????-?--*) different_mode=755;;
350 1.1 haad *) false;;
351 1.1 haad esac &&
352 1.1 haad $mkdirprog -m$different_mode -p -- "$tmpdir" && {
353 1.1 haad ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
354 1.1 haad test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
355 1.1 haad }
356 1.1 haad }
357 1.1 haad then posix_mkdir=:
358 1.1 haad fi
359 1.1 haad rmdir "$tmpdir/d" "$tmpdir"
360 1.1 haad else
361 1.1 haad # Remove any dirs left behind by ancient mkdir implementations.
362 1.1 haad rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
363 1.1 haad fi
364 1.1 haad trap '' 0;;
365 1.1 haad esac;;
366 1.1 haad esac
367 1.1 haad
368 1.1 haad if
369 1.1 haad $posix_mkdir && (
370 1.1 haad umask $mkdir_umask &&
371 1.1 haad $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
372 1.1 haad )
373 1.1 haad then :
374 1.1 haad else
375 1.1 haad
376 1.1 haad # The umask is ridiculous, or mkdir does not conform to POSIX,
377 1.1 haad # or it failed possibly due to a race condition. Create the
378 1.1 haad # directory the slow way, step by step, checking for races as we go.
379 1.1 haad
380 1.1 haad case $dstdir in
381 1.1 haad /*) prefix=/ ;;
382 1.1 haad -*) prefix=./ ;;
383 1.1 haad *) prefix= ;;
384 1.1 haad esac
385 1.1 haad
386 1.1 haad case $posix_glob in
387 1.1 haad '')
388 1.1 haad if (set -f) 2>/dev/null; then
389 1.1 haad posix_glob=true
390 1.1 haad else
391 1.1 haad posix_glob=false
392 1.1 haad fi ;;
393 1.1 haad esac
394 1.1 haad
395 1.1 haad oIFS=$IFS
396 1.1 haad IFS=/
397 1.1 haad $posix_glob && set -f
398 1.1 haad set fnord $dstdir
399 1.1 haad shift
400 1.1 haad $posix_glob && set +f
401 1.1 haad IFS=$oIFS
402 1.1 haad
403 1.1 haad prefixes=
404 1.1 haad
405 1.1 haad for d
406 1.1 haad do
407 1.1 haad test -z "$d" && continue
408 1.1 haad
409 1.1 haad prefix=$prefix$d
410 1.1 haad if test -d "$prefix"; then
411 1.1 haad prefixes=
412 1.1 haad else
413 1.1 haad if $posix_mkdir; then
414 1.1 haad (umask=$mkdir_umask &&
415 1.1 haad $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
416 1.1 haad # Don't fail if two instances are running concurrently.
417 1.1 haad test -d "$prefix" || exit 1
418 1.1 haad else
419 1.1 haad case $prefix in
420 1.1 haad *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
421 1.1 haad *) qprefix=$prefix;;
422 1.1 haad esac
423 1.1 haad prefixes="$prefixes '$qprefix'"
424 1.1 haad fi
425 1.1 haad fi
426 1.1 haad prefix=$prefix/
427 1.1 haad done
428 1.1 haad
429 1.1 haad if test -n "$prefixes"; then
430 1.1 haad # Don't fail if two instances are running concurrently.
431 1.1 haad (umask $mkdir_umask &&
432 1.1 haad eval "\$doit_exec \$mkdirprog $prefixes") ||
433 1.1 haad test -d "$dstdir" || exit 1
434 1.1 haad obsolete_mkdir_used=true
435 1.1 haad fi
436 1.1 haad fi
437 1.1 haad fi
438 1.1 haad
439 1.1 haad if test -n "$dir_arg"; then
440 1.1 haad { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
441 1.1 haad { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
442 1.1 haad { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
443 1.1 haad test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
444 1.1 haad else
445 1.1 haad
446 1.1 haad # Make a couple of temp file names in the proper directory.
447 1.1 haad dsttmp=$dstdir/_inst.$$_
448 1.1 haad rmtmp=$dstdir/_rm.$$_
449 1.1 haad
450 1.1 haad # Trap to clean up those temp files at exit.
451 1.1 haad trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
452 1.1 haad
453 1.1 haad # Copy the file name to the temp name.
454 1.1 haad (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
455 1.1 haad
456 1.1 haad # and set any options; do chmod last to preserve setuid bits.
457 1.1 haad #
458 1.1 haad # If any of these fail, we abort the whole thing. If we want to
459 1.1 haad # ignore errors from any of these, just make sure not to ignore
460 1.1 haad # errors from the above "$doit $cpprog $src $dsttmp" command.
461 1.1 haad #
462 1.1 haad { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
463 1.1 haad && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
464 1.1 haad && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
465 1.1 haad && { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
466 1.1 haad
467 1.1 haad # Now rename the file to the real destination.
468 1.1 haad { $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null \
469 1.1 haad || {
470 1.1 haad # The rename failed, perhaps because mv can't rename something else
471 1.1 haad # to itself, or perhaps because mv is so ancient that it does not
472 1.1 haad # support -f.
473 1.1 haad
474 1.1 haad # Now remove or move aside any old file at destination location.
475 1.1 haad # We try this two ways since rm can't unlink itself on some
476 1.1 haad # systems and the destination file might be busy for other
477 1.1 haad # reasons. In this case, the final cleanup might fail but the new
478 1.1 haad # file should still install successfully.
479 1.1 haad {
480 1.1 haad if test -f "$dst"; then
481 1.1 haad $doit $rmcmd -f "$dst" 2>/dev/null \
482 1.1 haad || { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null \
483 1.1 haad && { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }; }\
484 1.1 haad || {
485 1.1 haad echo "$0: cannot unlink or rename $dst" >&2
486 1.1 haad (exit 1); exit 1
487 1.1 haad }
488 1.1 haad else
489 1.1 haad :
490 1.1 haad fi
491 1.1 haad } &&
492 1.1 haad
493 1.1 haad # Now rename the file to the real destination.
494 1.1 haad $doit $mvcmd "$dsttmp" "$dst"
495 1.1 haad }
496 1.1 haad } || exit 1
497 1.1 haad
498 1.1 haad trap '' 0
499 1.1 haad fi
500 1.1 haad done
501 1.1 haad
502 1.1 haad # Local variables:
503 1.1 haad # eval: (add-hook 'write-file-hooks 'time-stamp)
504 1.1 haad # time-stamp-start: "scriptversion="
505 1.1 haad # time-stamp-format: "%:y-%02m-%02d.%02H"
506 1.1 haad # time-stamp-end: "$"
507 1.1 haad # End:
508