install-sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501
  1. #!/bin/sh
  2. # install - install a program, script, or datafile
  3. scriptversion=2013-12-25.23; # 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. tab=' '
  42. nl='
  43. '
  44. IFS=" $tab$nl"
  45. # Set DOITPROG to "echo" to test this script.
  46. doit=${DOITPROG-}
  47. doit_exec=${doit:-exec}
  48. # Put in absolute file names if you don't have them in your path;
  49. # or use environment vars.
  50. chgrpprog=${CHGRPPROG-chgrp}
  51. chmodprog=${CHMODPROG-chmod}
  52. chownprog=${CHOWNPROG-chown}
  53. cmpprog=${CMPPROG-cmp}
  54. cpprog=${CPPROG-cp}
  55. mkdirprog=${MKDIRPROG-mkdir}
  56. mvprog=${MVPROG-mv}
  57. rmprog=${RMPROG-rm}
  58. stripprog=${STRIPPROG-strip}
  59. posix_mkdir=
  60. # Desired mode of installed file.
  61. mode=0755
  62. chgrpcmd=
  63. chmodcmd=$chmodprog
  64. chowncmd=
  65. mvcmd=$mvprog
  66. rmcmd="$rmprog -f"
  67. stripcmd=
  68. src=
  69. dst=
  70. dir_arg=
  71. dst_arg=
  72. copy_on_change=false
  73. is_target_a_directory=possibly
  74. usage="\
  75. Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE
  76. or: $0 [OPTION]... SRCFILES... DIRECTORY
  77. or: $0 [OPTION]... -t DIRECTORY SRCFILES...
  78. or: $0 [OPTION]... -d DIRECTORIES...
  79. In the 1st form, copy SRCFILE to DSTFILE.
  80. In the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
  81. In the 4th, create DIRECTORIES.
  82. Options:
  83. --help display this help and exit.
  84. --version display version info and exit.
  85. -c (ignored)
  86. -C install only if different (preserve the last data modification time)
  87. -d create directories instead of installing files.
  88. -g GROUP $chgrpprog installed files to GROUP.
  89. -m MODE $chmodprog installed files to MODE.
  90. -o USER $chownprog installed files to USER.
  91. -s $stripprog installed files.
  92. -t DIRECTORY install into DIRECTORY.
  93. -T report an error if DSTFILE is a directory.
  94. Environment variables override the default commands:
  95. CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
  96. RMPROG STRIPPROG
  97. "
  98. while test $# -ne 0; do
  99. case $1 in
  100. -c) ;;
  101. -C) copy_on_change=true;;
  102. -d) dir_arg=true;;
  103. -g) chgrpcmd="$chgrpprog $2"
  104. shift;;
  105. --help) echo "$usage"; exit $?;;
  106. -m) mode=$2
  107. case $mode in
  108. *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
  109. echo "$0: invalid mode: $mode" >&2
  110. exit 1;;
  111. esac
  112. shift;;
  113. -o) chowncmd="$chownprog $2"
  114. shift;;
  115. -s) stripcmd=$stripprog;;
  116. -t)
  117. is_target_a_directory=always
  118. dst_arg=$2
  119. # Protect names problematic for 'test' and other utilities.
  120. case $dst_arg in
  121. -* | [=\(\)!]) dst_arg=./$dst_arg;;
  122. esac
  123. shift;;
  124. -T) is_target_a_directory=never;;
  125. --version) echo "$0 $scriptversion"; exit $?;;
  126. --) shift
  127. break;;
  128. -*) echo "$0: invalid option: $1" >&2
  129. exit 1;;
  130. *) break;;
  131. esac
  132. shift
  133. done
  134. # We allow the use of options -d and -T together, by making -d
  135. # take the precedence; this is for compatibility with GNU install.
  136. if test -n "$dir_arg"; then
  137. if test -n "$dst_arg"; then
  138. echo "$0: target directory not allowed when installing a directory." >&2
  139. exit 1
  140. fi
  141. fi
  142. if test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
  143. # When -d is used, all remaining arguments are directories to create.
  144. # When -t is used, the destination is already specified.
  145. # Otherwise, the last argument is the destination. Remove it from $@.
  146. for arg
  147. do
  148. if test -n "$dst_arg"; then
  149. # $@ is not empty: it contains at least $arg.
  150. set fnord "$@" "$dst_arg"
  151. shift # fnord
  152. fi
  153. shift # arg
  154. dst_arg=$arg
  155. # Protect names problematic for 'test' and other utilities.
  156. case $dst_arg in
  157. -* | [=\(\)!]) dst_arg=./$dst_arg;;
  158. esac
  159. done
  160. fi
  161. if test $# -eq 0; then
  162. if test -z "$dir_arg"; then
  163. echo "$0: no input file specified." >&2
  164. exit 1
  165. fi
  166. # It's OK to call 'install-sh -d' without argument.
  167. # This can happen when creating conditional directories.
  168. exit 0
  169. fi
  170. if test -z "$dir_arg"; then
  171. if test $# -gt 1 || test "$is_target_a_directory" = always; then
  172. if test ! -d "$dst_arg"; then
  173. echo "$0: $dst_arg: Is not a directory." >&2
  174. exit 1
  175. fi
  176. fi
  177. fi
  178. if test -z "$dir_arg"; then
  179. do_exit='(exit $ret); exit $ret'
  180. trap "ret=129; $do_exit" 1
  181. trap "ret=130; $do_exit" 2
  182. trap "ret=141; $do_exit" 13
  183. trap "ret=143; $do_exit" 15
  184. # Set umask so as not to create temps with too-generous modes.
  185. # However, 'strip' requires both read and write access to temps.
  186. case $mode in
  187. # Optimize common cases.
  188. *644) cp_umask=133;;
  189. *755) cp_umask=22;;
  190. *[0-7])
  191. if test -z "$stripcmd"; then
  192. u_plus_rw=
  193. else
  194. u_plus_rw='% 200'
  195. fi
  196. cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
  197. *)
  198. if test -z "$stripcmd"; then
  199. u_plus_rw=
  200. else
  201. u_plus_rw=,u+rw
  202. fi
  203. cp_umask=$mode$u_plus_rw;;
  204. esac
  205. fi
  206. for src
  207. do
  208. # Protect names problematic for 'test' and other utilities.
  209. case $src in
  210. -* | [=\(\)!]) src=./$src;;
  211. esac
  212. if test -n "$dir_arg"; then
  213. dst=$src
  214. dstdir=$dst
  215. test -d "$dstdir"
  216. dstdir_status=$?
  217. else
  218. # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
  219. # might cause directories to be created, which would be especially bad
  220. # if $src (and thus $dsttmp) contains '*'.
  221. if test ! -f "$src" && test ! -d "$src"; then
  222. echo "$0: $src does not exist." >&2
  223. exit 1
  224. fi
  225. if test -z "$dst_arg"; then
  226. echo "$0: no destination specified." >&2
  227. exit 1
  228. fi
  229. dst=$dst_arg
  230. # If destination is a directory, append the input filename; won't work
  231. # if double slashes aren't ignored.
  232. if test -d "$dst"; then
  233. if test "$is_target_a_directory" = never; then
  234. echo "$0: $dst_arg: Is a directory" >&2
  235. exit 1
  236. fi
  237. dstdir=$dst
  238. dst=$dstdir/`basename "$src"`
  239. dstdir_status=0
  240. else
  241. dstdir=`dirname "$dst"`
  242. test -d "$dstdir"
  243. dstdir_status=$?
  244. fi
  245. fi
  246. obsolete_mkdir_used=false
  247. if test $dstdir_status != 0; then
  248. case $posix_mkdir in
  249. '')
  250. # Create intermediate dirs using mode 755 as modified by the umask.
  251. # This is like FreeBSD 'install' as of 1997-10-28.
  252. umask=`umask`
  253. case $stripcmd.$umask in
  254. # Optimize common cases.
  255. *[2367][2367]) mkdir_umask=$umask;;
  256. .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
  257. *[0-7])
  258. mkdir_umask=`expr $umask + 22 \
  259. - $umask % 100 % 40 + $umask % 20 \
  260. - $umask % 10 % 4 + $umask % 2
  261. `;;
  262. *) mkdir_umask=$umask,go-w;;
  263. esac
  264. # With -d, create the new directory with the user-specified mode.
  265. # Otherwise, rely on $mkdir_umask.
  266. if test -n "$dir_arg"; then
  267. mkdir_mode=-m$mode
  268. else
  269. mkdir_mode=
  270. fi
  271. posix_mkdir=false
  272. case $umask in
  273. *[123567][0-7][0-7])
  274. # POSIX mkdir -p sets u+wx bits regardless of umask, which
  275. # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
  276. ;;
  277. *)
  278. tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
  279. trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
  280. if (umask $mkdir_umask &&
  281. exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
  282. then
  283. if test -z "$dir_arg" || {
  284. # Check for POSIX incompatibilities with -m.
  285. # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
  286. # other-writable bit of parent directory when it shouldn't.
  287. # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
  288. ls_ld_tmpdir=`ls -ld "$tmpdir"`
  289. case $ls_ld_tmpdir in
  290. d????-?r-*) different_mode=700;;
  291. d????-?--*) different_mode=755;;
  292. *) false;;
  293. esac &&
  294. $mkdirprog -m$different_mode -p -- "$tmpdir" && {
  295. ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
  296. test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
  297. }
  298. }
  299. then posix_mkdir=:
  300. fi
  301. rmdir "$tmpdir/d" "$tmpdir"
  302. else
  303. # Remove any dirs left behind by ancient mkdir implementations.
  304. rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
  305. fi
  306. trap '' 0;;
  307. esac;;
  308. esac
  309. if
  310. $posix_mkdir && (
  311. umask $mkdir_umask &&
  312. $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
  313. )
  314. then :
  315. else
  316. # The umask is ridiculous, or mkdir does not conform to POSIX,
  317. # or it failed possibly due to a race condition. Create the
  318. # directory the slow way, step by step, checking for races as we go.
  319. case $dstdir in
  320. /*) prefix='/';;
  321. [-=\(\)!]*) prefix='./';;
  322. *) prefix='';;
  323. esac
  324. oIFS=$IFS
  325. IFS=/
  326. set -f
  327. set fnord $dstdir
  328. shift
  329. set +f
  330. IFS=$oIFS
  331. prefixes=
  332. for d
  333. do
  334. test X"$d" = X && continue
  335. prefix=$prefix$d
  336. if test -d "$prefix"; then
  337. prefixes=
  338. else
  339. if $posix_mkdir; then
  340. (umask=$mkdir_umask &&
  341. $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
  342. # Don't fail if two instances are running concurrently.
  343. test -d "$prefix" || exit 1
  344. else
  345. case $prefix in
  346. *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
  347. *) qprefix=$prefix;;
  348. esac
  349. prefixes="$prefixes '$qprefix'"
  350. fi
  351. fi
  352. prefix=$prefix/
  353. done
  354. if test -n "$prefixes"; then
  355. # Don't fail if two instances are running concurrently.
  356. (umask $mkdir_umask &&
  357. eval "\$doit_exec \$mkdirprog $prefixes") ||
  358. test -d "$dstdir" || exit 1
  359. obsolete_mkdir_used=true
  360. fi
  361. fi
  362. fi
  363. if test -n "$dir_arg"; then
  364. { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
  365. { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
  366. { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
  367. test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
  368. else
  369. # Make a couple of temp file names in the proper directory.
  370. dsttmp=$dstdir/_inst.$$_
  371. rmtmp=$dstdir/_rm.$$_
  372. # Trap to clean up those temp files at exit.
  373. trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
  374. # Copy the file name to the temp name.
  375. (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
  376. # and set any options; do chmod last to preserve setuid bits.
  377. #
  378. # If any of these fail, we abort the whole thing. If we want to
  379. # ignore errors from any of these, just make sure not to ignore
  380. # errors from the above "$doit $cpprog $src $dsttmp" command.
  381. #
  382. { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
  383. { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
  384. { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
  385. { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
  386. # If -C, don't bother to copy if it wouldn't change the file.
  387. if $copy_on_change &&
  388. old=`LC_ALL=C ls -dlL "$dst" 2>/dev/null` &&
  389. new=`LC_ALL=C ls -dlL "$dsttmp" 2>/dev/null` &&
  390. set -f &&
  391. set X $old && old=:$2:$4:$5:$6 &&
  392. set X $new && new=:$2:$4:$5:$6 &&
  393. set +f &&
  394. test "$old" = "$new" &&
  395. $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
  396. then
  397. rm -f "$dsttmp"
  398. else
  399. # Rename the file to the real destination.
  400. $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
  401. # The rename failed, perhaps because mv can't rename something else
  402. # to itself, or perhaps because mv is so ancient that it does not
  403. # support -f.
  404. {
  405. # Now remove or move aside any old file at destination location.
  406. # We try this two ways since rm can't unlink itself on some
  407. # systems and the destination file might be busy for other
  408. # reasons. In this case, the final cleanup might fail but the new
  409. # file should still install successfully.
  410. {
  411. test ! -f "$dst" ||
  412. $doit $rmcmd -f "$dst" 2>/dev/null ||
  413. { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
  414. { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
  415. } ||
  416. { echo "$0: cannot unlink or rename $dst" >&2
  417. (exit 1); exit 1
  418. }
  419. } &&
  420. # Now rename the file to the real destination.
  421. $doit $mvcmd "$dsttmp" "$dst"
  422. }
  423. fi || exit 1
  424. trap '' 0
  425. fi
  426. done
  427. # Local variables:
  428. # eval: (add-hook 'write-file-hooks 'time-stamp)
  429. # time-stamp-start: "scriptversion="
  430. # time-stamp-format: "%:y-%02m-%02d.%02H"
  431. # time-stamp-time-zone: "UTC"
  432. # time-stamp-end: "; # UTC"
  433. # End: