install-sh 13 KB

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