1
0

install-sh 14 KB

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