install-sh 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520
  1. #!/bin/sh
  2. # install - install a program, script, or datafile
  3. scriptversion=2006-12-25.00
  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. shift;;
  133. -T) no_target_directory=true;;
  134. --version) echo "$0 $scriptversion"; exit $?;;
  135. --) shift
  136. break;;
  137. -*) echo "$0: invalid option: $1" >&2
  138. exit 1;;
  139. *) break;;
  140. esac
  141. shift
  142. done
  143. if test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
  144. # When -d is used, all remaining arguments are directories to create.
  145. # When -t is used, the destination is already specified.
  146. # Otherwise, the last argument is the destination. Remove it from $@.
  147. for arg
  148. do
  149. if test -n "$dst_arg"; then
  150. # $@ is not empty: it contains at least $arg.
  151. set fnord "$@" "$dst_arg"
  152. shift # fnord
  153. fi
  154. shift # arg
  155. dst_arg=$arg
  156. done
  157. fi
  158. if test $# -eq 0; then
  159. if test -z "$dir_arg"; then
  160. echo "$0: no input file specified." >&2
  161. exit 1
  162. fi
  163. # It's OK to call `install-sh -d' without argument.
  164. # This can happen when creating conditional directories.
  165. exit 0
  166. fi
  167. if test -z "$dir_arg"; then
  168. trap '(exit $?); exit' 1 2 13 15
  169. # Set umask so as not to create temps with too-generous modes.
  170. # However, 'strip' requires both read and write access to temps.
  171. case $mode in
  172. # Optimize common cases.
  173. *644) cp_umask=133;;
  174. *755) cp_umask=22;;
  175. *[0-7])
  176. if test -z "$stripcmd"; then
  177. u_plus_rw=
  178. else
  179. u_plus_rw='% 200'
  180. fi
  181. cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
  182. *)
  183. if test -z "$stripcmd"; then
  184. u_plus_rw=
  185. else
  186. u_plus_rw=,u+rw
  187. fi
  188. cp_umask=$mode$u_plus_rw;;
  189. esac
  190. fi
  191. for src
  192. do
  193. # Protect names starting with `-'.
  194. case $src in
  195. -*) src=./$src;;
  196. esac
  197. if test -n "$dir_arg"; then
  198. dst=$src
  199. dstdir=$dst
  200. test -d "$dstdir"
  201. dstdir_status=$?
  202. else
  203. # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
  204. # might cause directories to be created, which would be especially bad
  205. # if $src (and thus $dsttmp) contains '*'.
  206. if test ! -f "$src" && test ! -d "$src"; then
  207. echo "$0: $src does not exist." >&2
  208. exit 1
  209. fi
  210. if test -z "$dst_arg"; then
  211. echo "$0: no destination specified." >&2
  212. exit 1
  213. fi
  214. dst=$dst_arg
  215. # Protect names starting with `-'.
  216. case $dst in
  217. -*) dst=./$dst;;
  218. esac
  219. # If destination is a directory, append the input filename; won't work
  220. # if double slashes aren't ignored.
  221. if test -d "$dst"; then
  222. if test -n "$no_target_directory"; then
  223. echo "$0: $dst_arg: Is a directory" >&2
  224. exit 1
  225. fi
  226. dstdir=$dst
  227. dst=$dstdir/`basename "$src"`
  228. dstdir_status=0
  229. else
  230. # Prefer dirname, but fall back on a substitute if dirname fails.
  231. dstdir=`
  232. (dirname "$dst") 2>/dev/null ||
  233. expr X"$dst" : 'X\(.*[^/]\)//*[^/][^/]*/*$' \| \
  234. X"$dst" : 'X\(//\)[^/]' \| \
  235. X"$dst" : 'X\(//\)$' \| \
  236. X"$dst" : 'X\(/\)' \| . 2>/dev/null ||
  237. echo X"$dst" |
  238. sed '/^X\(.*[^/]\)\/\/*[^/][^/]*\/*$/{
  239. s//\1/
  240. q
  241. }
  242. /^X\(\/\/\)[^/].*/{
  243. s//\1/
  244. q
  245. }
  246. /^X\(\/\/\)$/{
  247. s//\1/
  248. q
  249. }
  250. /^X\(\/\).*/{
  251. s//\1/
  252. q
  253. }
  254. s/.*/./; q'
  255. `
  256. test -d "$dstdir"
  257. dstdir_status=$?
  258. fi
  259. fi
  260. obsolete_mkdir_used=false
  261. if test $dstdir_status != 0; then
  262. case $posix_mkdir in
  263. '')
  264. # Create intermediate dirs using mode 755 as modified by the umask.
  265. # This is like FreeBSD 'install' as of 1997-10-28.
  266. umask=`umask`
  267. case $stripcmd.$umask in
  268. # Optimize common cases.
  269. *[2367][2367]) mkdir_umask=$umask;;
  270. .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
  271. *[0-7])
  272. mkdir_umask=`expr $umask + 22 \
  273. - $umask % 100 % 40 + $umask % 20 \
  274. - $umask % 10 % 4 + $umask % 2
  275. `;;
  276. *) mkdir_umask=$umask,go-w;;
  277. esac
  278. # With -d, create the new directory with the user-specified mode.
  279. # Otherwise, rely on $mkdir_umask.
  280. if test -n "$dir_arg"; then
  281. mkdir_mode=-m$mode
  282. else
  283. mkdir_mode=
  284. fi
  285. posix_mkdir=false
  286. case $umask in
  287. *[123567][0-7][0-7])
  288. # POSIX mkdir -p sets u+wx bits regardless of umask, which
  289. # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
  290. ;;
  291. *)
  292. tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
  293. trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
  294. if (umask $mkdir_umask &&
  295. exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
  296. then
  297. if test -z "$dir_arg" || {
  298. # Check for POSIX incompatibilities with -m.
  299. # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
  300. # other-writeable bit of parent directory when it shouldn't.
  301. # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
  302. ls_ld_tmpdir=`ls -ld "$tmpdir"`
  303. case $ls_ld_tmpdir in
  304. d????-?r-*) different_mode=700;;
  305. d????-?--*) different_mode=755;;
  306. *) false;;
  307. esac &&
  308. $mkdirprog -m$different_mode -p -- "$tmpdir" && {
  309. ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
  310. test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
  311. }
  312. }
  313. then posix_mkdir=:
  314. fi
  315. rmdir "$tmpdir/d" "$tmpdir"
  316. else
  317. # Remove any dirs left behind by ancient mkdir implementations.
  318. rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
  319. fi
  320. trap '' 0;;
  321. esac;;
  322. esac
  323. if
  324. $posix_mkdir && (
  325. umask $mkdir_umask &&
  326. $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
  327. )
  328. then :
  329. else
  330. # The umask is ridiculous, or mkdir does not conform to POSIX,
  331. # or it failed possibly due to a race condition. Create the
  332. # directory the slow way, step by step, checking for races as we go.
  333. case $dstdir in
  334. /*) prefix='/';;
  335. -*) prefix='./';;
  336. *) prefix='';;
  337. esac
  338. eval "$initialize_posix_glob"
  339. oIFS=$IFS
  340. IFS=/
  341. $posix_glob set -f
  342. set fnord $dstdir
  343. shift
  344. $posix_glob set +f
  345. IFS=$oIFS
  346. prefixes=
  347. for d
  348. do
  349. test -z "$d" && continue
  350. prefix=$prefix$d
  351. if test -d "$prefix"; then
  352. prefixes=
  353. else
  354. if $posix_mkdir; then
  355. (umask=$mkdir_umask &&
  356. $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
  357. # Don't fail if two instances are running concurrently.
  358. test -d "$prefix" || exit 1
  359. else
  360. case $prefix in
  361. *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
  362. *) qprefix=$prefix;;
  363. esac
  364. prefixes="$prefixes '$qprefix'"
  365. fi
  366. fi
  367. prefix=$prefix/
  368. done
  369. if test -n "$prefixes"; then
  370. # Don't fail if two instances are running concurrently.
  371. (umask $mkdir_umask &&
  372. eval "\$doit_exec \$mkdirprog $prefixes") ||
  373. test -d "$dstdir" || exit 1
  374. obsolete_mkdir_used=true
  375. fi
  376. fi
  377. fi
  378. if test -n "$dir_arg"; then
  379. { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
  380. { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
  381. { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
  382. test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
  383. else
  384. # Make a couple of temp file names in the proper directory.
  385. dsttmp=$dstdir/_inst.$$_
  386. rmtmp=$dstdir/_rm.$$_
  387. # Trap to clean up those temp files at exit.
  388. trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
  389. # Copy the file name to the temp name.
  390. (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
  391. # and set any options; do chmod last to preserve setuid bits.
  392. #
  393. # If any of these fail, we abort the whole thing. If we want to
  394. # ignore errors from any of these, just make sure not to ignore
  395. # errors from the above "$doit $cpprog $src $dsttmp" command.
  396. #
  397. { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
  398. { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
  399. { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
  400. { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
  401. # If -C, don't bother to copy if it wouldn't change the file.
  402. if $copy_on_change &&
  403. old=`LC_ALL=C ls -dlL "$dst" 2>/dev/null` &&
  404. new=`LC_ALL=C ls -dlL "$dsttmp" 2>/dev/null` &&
  405. eval "$initialize_posix_glob" &&
  406. $posix_glob set -f &&
  407. set X $old && old=:$2:$4:$5:$6 &&
  408. set X $new && new=:$2:$4:$5:$6 &&
  409. $posix_glob set +f &&
  410. test "$old" = "$new" &&
  411. $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
  412. then
  413. rm -f "$dsttmp"
  414. else
  415. # Rename the file to the real destination.
  416. $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
  417. # The rename failed, perhaps because mv can't rename something else
  418. # to itself, or perhaps because mv is so ancient that it does not
  419. # support -f.
  420. {
  421. # Now remove or move aside any old file at destination location.
  422. # We try this two ways since rm can't unlink itself on some
  423. # systems and the destination file might be busy for other
  424. # reasons. In this case, the final cleanup might fail but the new
  425. # file should still install successfully.
  426. {
  427. test ! -f "$dst" ||
  428. $doit $rmcmd -f "$dst" 2>/dev/null ||
  429. { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
  430. { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
  431. } ||
  432. { echo "$0: cannot unlink or rename $dst" >&2
  433. (exit 1); exit 1
  434. }
  435. } &&
  436. # Now rename the file to the real destination.
  437. $doit $mvcmd "$dsttmp" "$dst"
  438. }
  439. fi || exit 1
  440. trap '' 0
  441. fi
  442. done
  443. # Local variables:
  444. # eval: (add-hook 'write-file-hooks 'time-stamp)
  445. # time-stamp-start: "scriptversion="
  446. # time-stamp-format: "%:y-%02m-%02d.%02H"
  447. # time-stamp-end: "$"
  448. # End: