install-sh 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296
  1. #!/bin/sh
  2. # install - install a program, script, or datafile
  3. scriptversion=2003-09-24.23
  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. It can only install one file at a time, a restriction
  41. # shared with many OS's install programs.
  42. # set DOITPROG to echo to test this script
  43. # Don't use :- since 4.3BSD and earlier shells don't like it.
  44. doit="${DOITPROG-}"
  45. # put in absolute paths if you don't have them in your path; or use env. vars.
  46. mvprog="${MVPROG-mv}"
  47. cpprog="${CPPROG-cp}"
  48. chmodprog="${CHMODPROG-chmod}"
  49. chownprog="${CHOWNPROG-chown}"
  50. chgrpprog="${CHGRPPROG-chgrp}"
  51. stripprog="${STRIPPROG-strip}"
  52. rmprog="${RMPROG-rm}"
  53. mkdirprog="${MKDIRPROG-mkdir}"
  54. transformbasename=
  55. transform_arg=
  56. instcmd="$mvprog"
  57. chmodcmd="$chmodprog 0755"
  58. chowncmd=
  59. chgrpcmd=
  60. stripcmd=
  61. rmcmd="$rmprog -f"
  62. mvcmd="$mvprog"
  63. src=
  64. dst=
  65. dir_arg=
  66. usage="Usage: $0 [OPTION]... SRCFILE DSTFILE
  67. or: $0 -d DIR1 DIR2...
  68. In the first form, install SRCFILE to DSTFILE, removing SRCFILE by default.
  69. In the second, create the directory path DIR.
  70. Options:
  71. -b=TRANSFORMBASENAME
  72. -c copy source (using $cpprog) instead of moving (using $mvprog).
  73. -d create directories instead of installing files.
  74. -g GROUP $chgrp installed files to GROUP.
  75. -m MODE $chmod installed files to MODE.
  76. -o USER $chown installed files to USER.
  77. -s strip installed files (using $stripprog).
  78. -t=TRANSFORM
  79. --help display this help and exit.
  80. --version display version info and exit.
  81. Environment variables override the default commands:
  82. CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG
  83. "
  84. while test -n "$1"; do
  85. case $1 in
  86. -b=*) transformbasename=`echo $1 | sed 's/-b=//'`
  87. shift
  88. continue;;
  89. -c) instcmd=$cpprog
  90. shift
  91. continue;;
  92. -d) dir_arg=true
  93. shift
  94. continue;;
  95. -g) chgrpcmd="$chgrpprog $2"
  96. shift
  97. shift
  98. continue;;
  99. --help) echo "$usage"; exit 0;;
  100. -m) chmodcmd="$chmodprog $2"
  101. shift
  102. shift
  103. continue;;
  104. -o) chowncmd="$chownprog $2"
  105. shift
  106. shift
  107. continue;;
  108. -s) stripcmd=$stripprog
  109. shift
  110. continue;;
  111. -t=*) transformarg=`echo $1 | sed 's/-t=//'`
  112. shift
  113. continue;;
  114. --version) echo "$0 $scriptversion"; exit 0;;
  115. *) if test -z "$src"; then
  116. src=$1
  117. else
  118. # this colon is to work around a 386BSD /bin/sh bug
  119. :
  120. dst=$1
  121. fi
  122. shift
  123. continue;;
  124. esac
  125. done
  126. if test -z "$src"; then
  127. echo "$0: no input file specified." >&2
  128. exit 1
  129. fi
  130. # Protect names starting with `-'.
  131. case $src in
  132. -*) src=./$src ;;
  133. esac
  134. if test -n "$dir_arg"; then
  135. dst=$src
  136. src=
  137. if test -d "$dst"; then
  138. instcmd=:
  139. chmodcmd=
  140. else
  141. instcmd=$mkdirprog
  142. fi
  143. else
  144. # Waiting for this to be detected by the "$instcmd $src $dsttmp" command
  145. # might cause directories to be created, which would be especially bad
  146. # if $src (and thus $dsttmp) contains '*'.
  147. if test ! -f "$src" && test ! -d "$src"; then
  148. echo "$0: $src does not exist." >&2
  149. exit 1
  150. fi
  151. if test -z "$dst"; then
  152. echo "$0: no destination specified." >&2
  153. exit 1
  154. fi
  155. # Protect names starting with `-'.
  156. case $dst in
  157. -*) dst=./$dst ;;
  158. esac
  159. # If destination is a directory, append the input filename; won't work
  160. # if double slashes aren't ignored.
  161. if test -d "$dst"; then
  162. dst=$dst/`basename "$src"`
  163. fi
  164. fi
  165. # This sed command emulates the dirname command.
  166. dstdir=`echo "$dst" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
  167. # Make sure that the destination directory exists.
  168. # Skip lots of stat calls in the usual case.
  169. if test ! -d "$dstdir"; then
  170. defaultIFS='
  171. '
  172. IFS="${IFS-$defaultIFS}"
  173. oIFS=$IFS
  174. # Some sh's can't handle IFS=/ for some reason.
  175. IFS='%'
  176. set - `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
  177. IFS=$oIFS
  178. pathcomp=
  179. while test $# -ne 0 ; do
  180. pathcomp=$pathcomp$1
  181. shift
  182. test -d "$pathcomp" || $mkdirprog "$pathcomp"
  183. pathcomp=$pathcomp/
  184. done
  185. fi
  186. if test -n "$dir_arg"; then
  187. $doit $instcmd "$dst" \
  188. && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \
  189. && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \
  190. && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \
  191. && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; }
  192. else
  193. # If we're going to rename the final executable, determine the name now.
  194. if test -z "$transformarg"; then
  195. dstfile=`basename "$dst"`
  196. else
  197. dstfile=`basename "$dst" $transformbasename \
  198. | sed $transformarg`$transformbasename
  199. fi
  200. # don't allow the sed command to completely eliminate the filename.
  201. test -z "$dstfile" && dstfile=`basename "$dst"`
  202. # Make a couple of temp file names in the proper directory.
  203. dsttmp=$dstdir/_inst.$$_
  204. rmtmp=$dstdir/_rm.$$_
  205. # Trap to clean up those temp files at exit.
  206. trap 'status=$?; rm -f "$dsttmp" "$rmtmp" && exit $status' 0
  207. trap '(exit $?); exit' 1 2 13 15
  208. # Move or copy the file name to the temp name
  209. $doit $instcmd "$src" "$dsttmp" &&
  210. # and set any options; do chmod last to preserve setuid bits.
  211. #
  212. # If any of these fail, we abort the whole thing. If we want to
  213. # ignore errors from any of these, just make sure not to ignore
  214. # errors from the above "$doit $instcmd $src $dsttmp" command.
  215. #
  216. { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \
  217. && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \
  218. && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \
  219. && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } &&
  220. # Now remove or move aside any old file at destination location. We
  221. # try this two ways since rm can't unlink itself on some systems and
  222. # the destination file might be busy for other reasons. In this case,
  223. # the final cleanup might fail but the new file should still install
  224. # successfully.
  225. {
  226. if test -f "$dstdir/$dstfile"; then
  227. $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \
  228. || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \
  229. || {
  230. echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
  231. (exit 1); exit
  232. }
  233. else
  234. :
  235. fi
  236. } &&
  237. # Now rename the file to the real destination.
  238. $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
  239. fi &&
  240. # The final little trick to "correctly" pass the exit status to the exit trap.
  241. {
  242. (exit 0); exit
  243. }
  244. # Local variables:
  245. # eval: (add-hook 'write-file-hooks 'time-stamp)
  246. # time-stamp-start: "scriptversion="
  247. # time-stamp-format: "%:y-%02m-%02d.%02H"
  248. # time-stamp-end: "$"
  249. # End: