Debian Codegraph @

install-sh

raw symbols SCIP
1 #!/bin/sh
2 # $MirOS: contrib/code/jupp/install-sh,v 1.9 2018/11/11 18:45:41 tg Exp $
3 # $miros: contrib/gnu/aux/install-sh,v 1.14 2018/11/11 18:42:46 tg Exp $
4 #-
5 # install - install a program, script, or datafile
6
7 scriptversion=2018-03-11.20; # UTC
8
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.
12 #
13 # Copyright (C) 1994 X Consortium
14 #
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:
21 #
22 # The above copyright notice and this permission notice shall be included in
23 # all copies or substantial portions of the Software.
24 #
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.
31 #
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-
35 # tium.
36 #
37 #
38 # FSF changes to this file are in the public domain.
39 #
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.
43 #
44 # This script is compatible with the BSD install script, but was written
45 # from scratch.
46
47 tab=' '
48 nl='
49 '
50 IFS=" $tab$nl"
51
52 # Set DOITPROG to "echo" to test this script.
53
54 # Don't use :- since 4.3BSD and earlier shells don't like it.
55 doit=${DOITPROG-}
56 if test -z "$doit"; then
57 doit_exec=exec
58 else
59 doit_exec=$doit
60 fi
61
62 # Put in absolute file names if you don't have them in your path;
63 # or use environment vars.
64
65 chgrpprog=${CHGRPPROG-chgrp}
66 chmodprog=${CHMODPROG-chmod}
67 chownprog=${CHOWNPROG-chown}
68 cmpprog=${CMPPROG-cmp}
69 cpprog=${CPPROG-cp}
70 mkdirprog=${MKDIRPROG-mkdir}
71 mvprog=${MVPROG-mv}
72 rmprog=${RMPROG-rm}
73 stripprog=${STRIPPROG-strip}
74
75 posix_glob='?'
76 initialise_posix_glob='
77 test "$posix_glob" != "?" || {
78 if (set -f) 2>/dev/null; then
79 posix_glob=
80 else
81 posix_glob=:
82 fi
83 }
84 '
85
86 posix_mkdir=
87
88 # Desired mode of installed file.
89 mode=0755
90
91 chgrpcmd=
92 chmodcmd=$chmodprog
93 chowncmd=
94 mvcmd=$mvprog
95 rmcmd="$rmprog -f"
96 stripcmd=
97
98 src=
99 dst=
100 dir_arg=
101 dst_arg=
102
103 copy_on_change=false
104 is_target_a_directory=possibly
105
106 usage="\
107 Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
108 or: $0 [OPTION]... SRCFILES... DIRECTORY
109 or: $0 [OPTION]... -t DIRECTORY SRCFILES...
110 or: $0 [OPTION]... -d DIRECTORIES...
111
112 In the 1st form, copy SRCFILE to DSTFILE.
113 In the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
114 In the 4th, create DIRECTORIES.
115
116 Options:
117 --help display this help and exit.
118 --version display version info and exit.
119
120 -c (ignored)
121 -C install only if different (preserve the last data modification time)
122 -d create directories instead of installing files.
123 -g GROUP $chgrpprog installed files to GROUP.
124 -m MODE $chmodprog installed files to MODE.
125 -o USER $chownprog installed files to USER.
126 -s $stripprog installed files.
127 -t DIRECTORY install into DIRECTORY.
128 -T report an error if DSTFILE is a directory.
129
130 Environment variables override the default commands:
131 CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
132 RMPROG STRIPPROG
133 "
134
135 while test $# -ne 0; do
136 case $1 in
137 -c) ;;
138
139 -C) copy_on_change=true;;
140
141 -d) dir_arg=true;;
142
143 -g) chgrpcmd="$chgrpprog $2"
144 shift;;
145
146 --help) echo "$usage"; exit $?;;
147
148 -m) mode=$2
149 case $mode in
150 *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
151 echo "$0: invalid mode: $mode" >&2
152 exit 1;;
153 esac
154 shift;;
155
156 -o) chowncmd="$chownprog $2"
157 shift;;
158
159 -s) stripcmd=$stripprog;;
160
161 -t)
162 is_target_a_directory=always
163 dst_arg=$2
164 # Protect names problematic for 'test' and other utilities.
165 case $dst_arg in
166 -* | [=\(\)!]) dst_arg=./$dst_arg;;
167 esac
168 shift;;
169
170 -T) is_target_a_directory=never;;
171
172 --version) echo "$0 $scriptversion"; exit $?;;
173
174 --) shift
175 break;;
176
177 -*) echo "$0: invalid option: $1" >&2
178 exit 1;;
179
180 *) break;;
181 esac
182 shift
183 done
184
185 # We allow the use of options -d and -T together, by making -d
186 # take the precedence; this is for compatibility with GNU install.
187
188 if test -n "$dir_arg"; then
189 if test -n "$dst_arg"; then
190 echo "$0: target directory not allowed when installing a directory." >&2
191 exit 1
192 fi
193 fi
194
195 if test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
196 # When -d is used, all remaining arguments are directories to create.
197 # When -t is used, the destination is already specified.
198 # Otherwise, the last argument is the destination. Remove it from $@.
199 for arg
200 do
201 if test -n "$dst_arg"; then
202 # $@ is not empty: it contains at least $arg.
203 set fnord "$@" "$dst_arg"
204 shift # fnord
205 fi
206 shift # arg
207 dst_arg=$arg
208 # Protect names problematic for 'test' and other utilities.
209 case $dst_arg in
210 -* | [=\(\)!]) dst_arg=./$dst_arg;;
211 esac
212 done
213 fi
214
215 if test $# -eq 0; then
216 if test -z "$dir_arg"; then
217 echo "$0: no input file specified." >&2
218 exit 1
219 fi
220 # It's OK to call 'install-sh -d' without argument.
221 # This can happen when creating conditional directories.
222 exit 0
223 fi
224
225 if test -z "$dir_arg"; then
226 if test $# -gt 1 || test "$is_target_a_directory" = always; then
227 if test ! -d "$dst_arg"; then
228 echo "$0: $dst_arg: Is not a directory." >&2
229 exit 1
230 fi
231 fi
232 fi
233
234 if test -z "$dir_arg"; then
235 do_exit='(exit $ret); exit $ret'
236 trap "ret=129; $do_exit" 1
237 trap "ret=130; $do_exit" 2
238 trap "ret=141; $do_exit" 13
239 trap "ret=143; $do_exit" 15
240
241 # Set umask so as not to create temps with too-generous modes.
242 # However, 'strip' requires both read and write access to temps.
243 case $mode in
244 # Optimize common cases.
245 *644) cp_umask=133;;
246 *755) cp_umask=22;;
247
248 *[0-7])
249 if test -z "$stripcmd"; then
250 u_plus_rw=
251 else
252 u_plus_rw='% 200'
253 fi
254 cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
255 *)
256 if test -z "$stripcmd"; then
257 u_plus_rw=
258 else
259 u_plus_rw=,u+rw
260 fi
261 cp_umask=$mode$u_plus_rw;;
262 esac
263 fi
264
265 for src
266 do
267 # Protect names problematic for 'test' and other utilities.
268 case $src in
269 -* | [=\(\)!]) src=./$src;;
270 esac
271
272 if test -n "$dir_arg"; then
273 dst=$src
274 dstdir=$dst
275 test -d "$dstdir"
276 dstdir_status=$?
277 else
278
279 # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
280 # might cause directories to be created, which would be especially bad
281 # if $src (and thus $dsttmp) contains '*'.
282 if test ! -f "$src" && test ! -d "$src"; then
283 echo "$0: $src does not exist." >&2
284 exit 1
285 fi
286
287 if test -z "$dst_arg"; then
288 echo "$0: no destination specified." >&2
289 exit 1
290 fi
291 dst=$dst_arg
292
293 # If destination is a directory, append the input filename.
294 if test -d "$dst"; then
295 if test "$is_target_a_directory" = never; then
296 echo "$0: $dst_arg: Is a directory" >&2
297 exit 1
298 fi
299 dstdir=$dst
300 dstbase=${src##*/}
301 case $dst in
302 */) dst=$dst$dstbase;;
303 *) dst=$dst/$dstbase;;
304 esac
305 dstdir_status=0
306 else
307 # Prefer dirname, but fall back on a substitute if dirname fails.
308 dstdir=`
309 (dirname "$dst") 2>/dev/null ||
310 expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
311 X"$dst" : 'X\(//\)[^/]' \| \
312 X"$dst" : 'X\(//\)$' \| \
313 X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
314 echo X"$dst" |
315 sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
316 s//\1/
317 q
318 }
319 /^X\(\/\/\)[^/].*/{
320 s//\1/
321 q
322 }
323 /^X\(\/\/\)$/{
324 s//\1/
325 q
326 }
327 /^X\(\/\).*/{
328 s//\1/
329 q
330 }
331 s/.*/./; q'
332 `
333
334 test -d "$dstdir"
335 dstdir_status=$?
336 fi
337 fi
338
339 case $dstdir in
340 */) dstdirslash=$dstdir;;
341 *) dstdirslash=$dstdir/;;
342 esac
343
344 obsolete_mkdir_used=false
345
346 if test $dstdir_status != 0; then
347 case $posix_mkdir in
348 '')
349 # Create intermediate dirs using mode 755 as modified by the umask.
350 # This is like FreeBSD 'install' as of 1997-10-28.
351 umask=`umask`
352 case $stripcmd.$umask in
353 # Optimize common cases.
354 *[2367][2367]) mkdir_umask=$umask;;
355 .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
356
357 *[0-7])
358 mkdir_umask=`expr $umask + 22 \
359 - $umask % 100 % 40 + $umask % 20 \
360 - $umask % 10 % 4 + $umask % 2
361 `;;
362 *) mkdir_umask=$umask,go-w;;
363 esac
364
365 # With -d, create the new directory with the user-specified mode.
366 # Otherwise, rely on $mkdir_umask.
367 if test -n "$dir_arg"; then
368 mkdir_mode=-m$mode
369 else
370 mkdir_mode=
371 fi
372
373 posix_mkdir=false
374 case $umask in
375 *[123567][0-7][0-7])
376 # POSIX mkdir -p sets u+wx bits regardless of umask, which
377 # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
378 ;;
379 *)
380 # Note that $RANDOM variable is not portable (e.g. dash); Use it
381 # here however when possible just to lower collision chance.
382 tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
383
384 trap 'ret=$?; rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null; exit $ret' 0
385
386 # Because "mkdir -p" follows existing symlinks and we likely work
387 # directly in world-writable /tmp, make sure that the '$tmpdir'
388 # directory is successfully created first before we actually test
389 # 'mkdir -p' feature.
390 if (umask $mkdir_umask &&
391 $mkdirprog $mkdir_mode "$tmpdir" &&
392 exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
393 then
394 if test -z "$dir_arg" || {
395 # Check for POSIX incompatibilities with -m.
396 # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
397 # other-writable bit of parent directory when it shouldn't.
398 # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
399 test_tmpdir="$tmpdir/a"
400 ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
401 case $ls_ld_tmpdir in
402 d????-?r-*) different_mode=700;;
403 d????-?--*) different_mode=755;;
404 *) false;;
405 esac &&
406 $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
407 ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
408 test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
409 }
410 }
411 then posix_mkdir=:
412 fi
413 rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
414 else
415 # Remove any dirs left behind by ancient mkdir implementations.
416 rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
417 fi
418 trap '' 0;;
419 esac;;
420 esac
421
422 if
423 $posix_mkdir && (
424 umask $mkdir_umask &&
425 $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
426 )
427 then :
428 else
429
430 # The umask is ridiculous, or mkdir does not conform to POSIX,
431 # or it failed possibly due to a race condition. Create the
432 # directory the slow way, step by step, checking for races as we go.
433
434 case $dstdir in
435 /*) prefix='/';;
436 [-=\(\)!]*) prefix='./';;
437 *) prefix='';;
438 esac
439
440 eval "$initialise_posix_glob"
441
442 oIFS=$IFS
443 IFS=/
444 $posix_glob set -f
445 set fnord $dstdir
446 shift
447 $posix_glob set +f
448 IFS=$oIFS
449
450 prefixes=
451
452 for d
453 do
454 test X"$d" = X && continue
455
456 prefix=$prefix$d
457 if test -d "$prefix"; then
458 prefixes=
459 else
460 if $posix_mkdir; then
461 (umask=$mkdir_umask &&
462 $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
463 # Don't fail if two instances are running concurrently.
464 test -d "$prefix" || exit 1
465 else
466 case $prefix in
467 *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
468 *) qprefix=$prefix;;
469 esac
470 prefixes="$prefixes '$qprefix'"
471 fi
472 fi
473 prefix=$prefix/
474 done
475
476 if test -n "$prefixes"; then
477 # Don't fail if two instances are running concurrently.
478 (umask $mkdir_umask &&
479 eval "\$doit_exec \$mkdirprog $prefixes") ||
480 test -d "$dstdir" || exit 1
481 obsolete_mkdir_used=true
482 fi
483 fi
484 fi
485
486 if test -n "$dir_arg"; then
487 { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
488 { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
489 { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
490 test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
491 else
492
493 # Make a couple of temp file names in the proper directory.
494 dsttmp=${dstdirslash}_inst.$$_
495 rmtmp=${dstdirslash}_rm.$$_
496
497 # Trap to clean up those temp files at exit.
498 trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
499
500 # Copy the file name to the temp name.
501 (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
502
503 # and set any options; do chmod last to preserve setuid bits.
504 #
505 # If any of these fail, we abort the whole thing. If we want to
506 # ignore errors from any of these, just make sure not to ignore
507 # errors from the above "$doit $cpprog $src $dsttmp" command.
508 #
509 { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
510 { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
511 { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
512 { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
513
514 # If -C, don't bother to copy if it wouldn't change the file.
515 if $copy_on_change &&
516 old=`LC_ALL=C ls -dlL "$dst" 2>/dev/null` &&
517 new=`LC_ALL=C ls -dlL "$dsttmp" 2>/dev/null` &&
518
519 eval "$initialise_posix_glob" &&
520 $posix_glob set -f &&
521 set X $old && old=:$2:$4:$5:$6 &&
522 set X $new && new=:$2:$4:$5:$6 &&
523 $posix_glob set +f &&
524
525 test "$old" = "$new" &&
526 $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
527 then
528 rm -f "$dsttmp"
529 else
530 # Rename the file to the real destination.
531 $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
532
533 # The rename failed, perhaps because mv can't rename something else
534 # to itself, or perhaps because mv is so ancient that it does not
535 # support -f.
536 {
537 # Now remove or move aside any old file at destination location.
538 # We try this two ways since rm can't unlink itself on some
539 # systems and the destination file might be busy for other
540 # reasons. In this case, the final cleanup might fail but the new
541 # file should still install successfully.
542 {
543 test ! -f "$dst" ||
544 $doit $rmcmd -f "$dst" 2>/dev/null ||
545 { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
546 { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
547 } ||
548 { echo "$0: cannot unlink or rename $dst" >&2
549 (exit 1); exit 1
550 }
551 } &&
552
553 # Now rename the file to the real destination.
554 $doit $mvcmd "$dsttmp" "$dst"
555 }
556 fi || exit 1
557
558 trap '' 0
559 fi
560 done
561
562 # Local variables:
563 # eval: (add-hook 'before-save-hook 'time-stamp)
564 # time-stamp-start: "scriptversion="
565 # time-stamp-format: "%:y-%02m-%02d.%02H"
566 # time-stamp-time-zone: "UTC0"
567 # time-stamp-end: "; # UTC"
568 # End: