install-sh 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  1. #!/bin/sh
  2. # install - install a program, script, or datafile
  3. scriptversion=2011-01-19.21; # 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. tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
  301. trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
  302. if (umask $mkdir_umask &&
  303. exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
  304. then
  305. if test -z "$dir_arg" || {
  306. # Check for POSIX incompatibilities with -m.
  307. # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
  308. # other-writeable bit of parent directory when it shouldn't.
  309. # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
  310. ls_ld_tmpdir=`ls -ld "$tmpdir"`
  311. case $ls_ld_tmpdir in
  312. d????-?r-*) different_mode=700;;
  313. d????-?--*) different_mode=755;;
  314. *) false;;
  315. esac &&
  316. $mkdirprog -m$different_mode -p -- "$tmpdir" && {
  317. ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
  318. test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
  319. }
  320. }
  321. then posix_mkdir=:
  322. fi
  323. rmdir "$tmpdir/d" "$tmpdir"
  324. else
  325. # Remove any dirs left behind by ancient mkdir implementations.
  326. rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
  327. fi
  328. trap '' 0;;
  329. esac;;
  330. esac
  331. if
  332. $posix_mkdir && (
  333. umask $mkdir_umask &&
  334. $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
  335. )
  336. then :
  337. else
  338. # The umask is ridiculous, or mkdir does not conform to POSIX,
  339. # or it failed possibly due to a race condition. Create the
  340. # directory the slow way, step by step, checking for races as we go.
  341. case $dstdir in
  342. /*) prefix='/';;
  343. [-=\(\)!]*) prefix='./';;
  344. *) prefix='';;
  345. esac
  346. eval "$initialize_posix_glob"
  347. oIFS=$IFS
  348. IFS=/
  349. $posix_glob set -f
  350. set fnord $dstdir
  351. shift
  352. $posix_glob set +f
  353. IFS=$oIFS
  354. prefixes=
  355. for d
  356. do
  357. test X"$d" = X && continue
  358. prefix=$prefix$d
  359. if test -d "$prefix"; then
  360. prefixes=
  361. else
  362. if $posix_mkdir; then
  363. (umask=$mkdir_umask &&
  364. $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
  365. # Don't fail if two instances are running concurrently.
  366. test -d "$prefix" || exit 1
  367. else
  368. case $prefix in
  369. *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
  370. *) qprefix=$prefix;;
  371. esac
  372. prefixes="$prefixes '$qprefix'"
  373. fi
  374. fi
  375. prefix=$prefix/
  376. done
  377. if test -n "$prefixes"; then
  378. # Don't fail if two instances are running concurrently.
  379. (umask $mkdir_umask &&
  380. eval "\$doit_exec \$mkdirprog $prefixes") ||
  381. test -d "$dstdir" || exit 1
  382. obsolete_mkdir_used=true
  383. fi
  384. fi
  385. fi
  386. if test -n "$dir_arg"; then
  387. { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
  388. { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
  389. { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
  390. test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
  391. else
  392. # Make a couple of temp file names in the proper directory.
  393. dsttmp=$dstdir/_inst.$$_
  394. rmtmp=$dstdir/_rm.$$_
  395. # Trap to clean up those temp files at exit.
  396. trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
  397. # Copy the file name to the temp name.
  398. (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
  399. # and set any options; do chmod last to preserve setuid bits.
  400. #
  401. # If any of these fail, we abort the whole thing. If we want to
  402. # ignore errors from any of these, just make sure not to ignore
  403. # errors from the above "$doit $cpprog $src $dsttmp" command.
  404. #
  405. { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
  406. { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
  407. { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
  408. { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
  409. # If -C, don't bother to copy if it wouldn't change the file.
  410. if $copy_on_change &&
  411. old=`LC_ALL=C ls -dlL "$dst" 2>/dev/null` &&
  412. new=`LC_ALL=C ls -dlL "$dsttmp" 2>/dev/null` &&
  413. eval "$initialize_posix_glob" &&
  414. $posix_glob set -f &&
  415. set X $old && old=:$2:$4:$5:$6 &&
  416. set X $new && new=:$2:$4:$5:$6 &&
  417. $posix_glob set +f &&
  418. test "$old" = "$new" &&
  419. $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
  420. then
  421. rm -f "$dsttmp"
  422. else
  423. # Rename the file to the real destination.
  424. $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
  425. # The rename failed, perhaps because mv can't rename something else
  426. # to itself, or perhaps because mv is so ancient that it does not
  427. # support -f.
  428. {
  429. # Now remove or move aside any old file at destination location.
  430. # We try this two ways since rm can't unlink itself on some
  431. # systems and the destination file might be busy for other
  432. # reasons. In this case, the final cleanup might fail but the new
  433. # file should still install successfully.
  434. {
  435. test ! -f "$dst" ||
  436. $doit $rmcmd -f "$dst" 2>/dev/null ||
  437. { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
  438. { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
  439. } ||
  440. { echo "$0: cannot unlink or rename $dst" >&2
  441. (exit 1); exit 1
  442. }
  443. } &&
  444. # Now rename the file to the real destination.
  445. $doit $mvcmd "$dsttmp" "$dst"
  446. }
  447. fi || exit 1
  448. trap '' 0
  449. fi
  450. done
  451. # Local variables:
  452. # eval: (add-hook 'write-file-hooks 'time-stamp)
  453. # time-stamp-start: "scriptversion="
  454. # time-stamp-format: "%:y-%02m-%02d.%02H"
  455. # time-stamp-time-zone: "UTC"
  456. # time-stamp-end: "; # UTC"
  457. # End: