2 # $MirOS: contrib/code/jupp/install-sh,v 1.8 2008/05/13 16:13:39 tg Exp $
3 # $miros: contrib/gnu/aux/install-sh,v 1.13 2008/05/02 23:01:38 tg Exp $
5 # install - install a program, script, or datafile
7 scriptversion=2006-12-25.00
9 # This originates from X11R5 (mit/util/scripts/install.sh), which was
10 # later released in X11R6 (xc/config/util/install.sh) with the
11 # following copyright and license.
13 # Copyright (C) 1994 X Consortium
15 # Permission is hereby granted, free of charge, to any person obtaining a copy
16 # of this software and associated documentation files (the "Software"), to
17 # deal in the Software without restriction, including without limitation the
18 # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
19 # sell copies of the Software, and to permit persons to whom the Software is
20 # furnished to do so, subject to the following conditions:
22 # The above copyright notice and this permission notice shall be included in
23 # all copies or substantial portions of the Software.
25 # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
26 # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
27 # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
28 # X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
29 # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
30 # TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 # Except as contained in this notice, the name of the X Consortium shall not
33 # be used in advertising or otherwise to promote the sale, use or other deal-
34 # ings in this Software without prior written authorization from the X Consor-
38 # FSF changes to this file are in the public domain.
40 # Calling this script install-sh is preferred over install.sh, to prevent
41 # `make' implicit rules from creating a file called install from it
42 # when there is no Makefile.
44 # This script is compatible with the BSD install script, but was written
51 # set DOITPROG to echo to test this script
53 # Don't use :- since 4.3BSD and earlier shells don't like it.
55 if test -z "$doit"; then
61 # Put in absolute file names if you don't have them in your path;
62 # or use environment vars.
64 chgrpprog=${CHGRPPROG-chgrp}
65 chmodprog=${CHMODPROG-chmod}
66 chownprog=${CHOWNPROG-chown}
67 cmpprog=${CMPPROG-cmp}
69 mkdirprog=${MKDIRPROG-mkdir}
72 stripprog=${STRIPPROG-strip}
75 initialize_posix_glob='
76 test "$posix_glob" != "?" || {
77 if (set -f) 2>/dev/null; then
87 # Desired mode of installed file.
106 Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
107 or: $0 [OPTION]... SRCFILES... DIRECTORY
108 or: $0 [OPTION]... -t DIRECTORY SRCFILES...
109 or: $0 [OPTION]... -d DIRECTORIES...
111 In the 1st form, copy SRCFILE to DSTFILE.
112 In the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
113 In the 4th, create DIRECTORIES.
116 --help display this help and exit.
117 --version display version info and exit.
120 -C install only if different (preserve the last data modification time)
121 -d create directories instead of installing files.
122 -g GROUP $chgrpprog installed files to GROUP.
123 -m MODE $chmodprog installed files to MODE.
124 -o USER $chownprog installed files to USER.
125 -s $stripprog installed files.
126 -t DIRECTORY install into DIRECTORY.
127 -T report an error if DSTFILE is a directory.
129 Environment variables override the default commands:
130 CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
134 while test $# -ne 0; do
138 -C) copy_on_change=true;;
142 -g) chgrpcmd="$chgrpprog $2"
145 --help) echo "$usage"; exit $?;;
150 '* | *'*'* | *'?'* | *'['*)
151 echo "$0: invalid mode: $mode" >&2
156 -o) chowncmd="$chownprog $2"
159 -s) stripcmd=$stripprog;;
164 -T) no_target_directory=true;;
166 --version) echo "$0 $scriptversion"; exit $?;;
171 -*) echo "$0: invalid option: $1" >&2
179 if test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
180 # When -d is used, all remaining arguments are directories to create.
181 # When -t is used, the destination is already specified.
182 # Otherwise, the last argument is the destination. Remove it from $@.
185 if test -n "$dst_arg"; then
186 # $@ is not empty: it contains at least $arg.
187 set fnord "$@" "$dst_arg"
195 if test $# -eq 0; then
196 if test -z "$dir_arg"; then
197 echo "$0: no input file specified." >&2
200 # It's OK to call `install-sh -d' without argument.
201 # This can happen when creating conditional directories.
205 if test -z "$dir_arg"; then
206 trap '(exit $?); exit' 1 2 13 15
208 # Set umask so as not to create temps with too-generous modes.
209 # However, 'strip' requires both read and write access to temps.
211 # Optimize common cases.
216 if test -z "$stripcmd"; then
221 cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
223 if test -z "$stripcmd"; then
228 cp_umask=$mode$u_plus_rw;;
234 # Protect names starting with `-'.
239 if test -n "$dir_arg"; then
246 # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
247 # might cause directories to be created, which would be especially bad
248 # if $src (and thus $dsttmp) contains '*'.
249 if test ! -f "$src" && test ! -d "$src"; then
250 echo "$0: $src does not exist." >&2
254 if test -z "$dst_arg"; then
255 echo "$0: no destination specified." >&2
260 # Protect names starting with `-'.
265 # If destination is a directory, append the input filename; won't work
266 # if double slashes aren't ignored.
267 if test -d "$dst"; then
268 if test -n "$no_target_directory"; then
269 echo "$0: $dst_arg: Is a directory" >&2
273 dst=$dstdir/`basename "$src"`
276 # Prefer dirname, but fall back on a substitute if dirname fails.
278 (dirname "$dst") 2>/dev/null ||
279 expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
280 X"$dst" : 'X\(//\)[^/]' \| \
281 X"$dst" : 'X\(//\)$' \| \
282 X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
284 sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
308 obsolete_mkdir_used=false
310 if test $dstdir_status != 0; then
313 # Create intermediate dirs using mode 755 as modified by the umask.
314 # This is like FreeBSD 'install' as of 1997-10-28.
316 case $stripcmd.$umask in
317 # Optimize common cases.
318 *[2367][2367]) mkdir_umask=$umask;;
319 .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
322 mkdir_umask=`expr $umask + 22 \
323 - $umask % 100 % 40 + $umask % 20 \
324 - $umask % 10 % 4 + $umask % 2
326 *) mkdir_umask=$umask,go-w;;
329 # With -d, create the new directory with the user-specified mode.
330 # Otherwise, rely on $mkdir_umask.
331 if test -n "$dir_arg"; then
340 # POSIX mkdir -p sets u+wx bits regardless of umask, which
341 # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
344 tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
345 trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
347 if (umask $mkdir_umask &&
348 exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
350 if test -z "$dir_arg" || {
351 # Check for POSIX incompatibilities with -m.
352 # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
353 # other-writeable bit of parent directory when it shouldn't.
354 # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
355 ls_ld_tmpdir=`ls -ld "$tmpdir"`
356 case $ls_ld_tmpdir in
357 d????-?r-*) different_mode=700;;
358 d????-?--*) different_mode=755;;
361 $mkdirprog -m$different_mode -p -- "$tmpdir" && {
362 ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
363 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
368 rmdir "$tmpdir/d" "$tmpdir"
370 # Remove any dirs left behind by ancient mkdir implementations.
371 rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
379 umask $mkdir_umask &&
380 $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
385 # The umask is ridiculous, or mkdir does not conform to POSIX,
386 # or it failed possibly due to a race condition. Create the
387 # directory the slow way, step by step, checking for races as we go.
395 eval "$initialize_posix_glob"
409 test -z "$d" && continue
412 if test -d "$prefix"; then
415 if $posix_mkdir; then
416 (umask=$mkdir_umask &&
417 $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
418 # Don't fail if two instances are running concurrently.
419 test -d "$prefix" || exit 1
422 *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
425 prefixes="$prefixes '$qprefix'"
431 if test -n "$prefixes"; then
432 # Don't fail if two instances are running concurrently.
433 (umask $mkdir_umask &&
434 eval "\$doit_exec \$mkdirprog $prefixes") ||
435 test -d "$dstdir" || exit 1
436 obsolete_mkdir_used=true
441 if test -n "$dir_arg"; then
442 { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
443 { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
444 { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
445 test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
448 # Make a couple of temp file names in the proper directory.
449 dsttmp=$dstdir/_inst.$$_
450 rmtmp=$dstdir/_rm.$$_
452 # Trap to clean up those temp files at exit.
453 trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
455 # Copy the file name to the temp name.
456 (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
458 # and set any options; do chmod last to preserve setuid bits.
460 # If any of these fail, we abort the whole thing. If we want to
461 # ignore errors from any of these, just make sure not to ignore
462 # errors from the above "$doit $cpprog $src $dsttmp" command.
464 { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
465 { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
466 { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
467 { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
469 # If -C, don't bother to copy if it wouldn't change the file.
470 if $copy_on_change &&
471 old=`LC_ALL=C ls -dlL "$dst" 2>/dev/null` &&
472 new=`LC_ALL=C ls -dlL "$dsttmp" 2>/dev/null` &&
474 eval "$initialize_posix_glob" &&
475 $posix_glob set -f &&
476 set X $old && old=:$2:$4:$5:$6 &&
477 set X $new && new=:$2:$4:$5:$6 &&
478 $posix_glob set +f &&
480 test "$old" = "$new" &&
481 $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
485 # Rename the file to the real destination.
486 $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
488 # The rename failed, perhaps because mv can't rename something else
489 # to itself, or perhaps because mv is so ancient that it does not
492 # Now remove or move aside any old file at destination location.
493 # We try this two ways since rm can't unlink itself on some
494 # systems and the destination file might be busy for other
495 # reasons. In this case, the final cleanup might fail but the new
496 # file should still install successfully.
499 $doit $rmcmd -f "$dst" 2>/dev/null ||
500 { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
501 { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
503 { echo "$0: cannot unlink or rename $dst" >&2
508 # Now rename the file to the real destination.
509 $doit $mvcmd "$dsttmp" "$dst"
518 # eval: (add-hook 'write-file-hooks 'time-stamp)
519 # time-stamp-start: "scriptversion="
520 # time-stamp-format: "%:y-%02m-%02d.%02H"
521 # time-stamp-end: "$"