install-sh 7.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294
  1. #!/bin/sh
  2. #
  3. # install - install a program, script, or datafile
  4. #
  5. # This originates from X11R5 (mit/util/scripts/install.sh), which was
  6. # later released in X11R6 (xc/config/util/install.sh) with the
  7. # following copyright and license.
  8. #
  9. # Copyright (C) 1994 X Consortium
  10. #
  11. # Permission is hereby granted, free of charge, to any person obtaining a copy
  12. # of this software and associated documentation files (the "Software"), to
  13. # deal in the Software without restriction, including without limitation the
  14. # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
  15. # sell copies of the Software, and to permit persons to whom the Software is
  16. # furnished to do so, subject to the following conditions:
  17. #
  18. # The above copyright notice and this permission notice shall be included in
  19. # all copies or substantial portions of the Software.
  20. #
  21. # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  22. # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  23. # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  24. # X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
  25. # AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
  26. # TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
  27. #
  28. # Except as contained in this notice, the name of the X Consortium shall not
  29. # be used in advertising or otherwise to promote the sale, use or other deal-
  30. # ings in this Software without prior written authorization from the X Consor-
  31. # tium.
  32. #
  33. #
  34. # FSF changes to this file are in the public domain.
  35. #
  36. # Calling this script install-sh is preferred over install.sh, to prevent
  37. # `make' implicit rules from creating a file called install from it
  38. # when there is no Makefile.
  39. #
  40. # This script is compatible with the BSD install script, but was written
  41. # from scratch. It can only install one file at a time, a restriction
  42. # shared with many OS's install programs.
  43. # set DOITPROG to echo to test this script
  44. # Don't use :- since 4.3BSD and earlier shells don't like it.
  45. doit="${DOITPROG-}"
  46. # put in absolute paths if you don't have them in your path; or use env. vars.
  47. mvprog="${MVPROG-mv}"
  48. cpprog="${CPPROG-cp}"
  49. chmodprog="${CHMODPROG-chmod}"
  50. chownprog="${CHOWNPROG-chown}"
  51. chgrpprog="${CHGRPPROG-chgrp}"
  52. stripprog="${STRIPPROG-strip}"
  53. rmprog="${RMPROG-rm}"
  54. mkdirprog="${MKDIRPROG-mkdir}"
  55. transformbasename=""
  56. transform_arg=""
  57. instcmd="$mvprog"
  58. chmodcmd="$chmodprog 0755"
  59. chowncmd=""
  60. chgrpcmd=""
  61. stripcmd=""
  62. rmcmd="$rmprog -f"
  63. mvcmd="$mvprog"
  64. src=""
  65. dst=""
  66. dir_arg=""
  67. while [ x"$1" != x ]; do
  68. case $1 in
  69. -c) instcmd=$cpprog
  70. shift
  71. continue;;
  72. -d) dir_arg=true
  73. shift
  74. continue;;
  75. -m) chmodcmd="$chmodprog $2"
  76. shift
  77. shift
  78. continue;;
  79. -o) chowncmd="$chownprog $2"
  80. shift
  81. shift
  82. continue;;
  83. -g) chgrpcmd="$chgrpprog $2"
  84. shift
  85. shift
  86. continue;;
  87. -s) stripcmd=$stripprog
  88. shift
  89. continue;;
  90. -t=*) transformarg=`echo $1 | sed 's/-t=//'`
  91. shift
  92. continue;;
  93. -b=*) transformbasename=`echo $1 | sed 's/-b=//'`
  94. shift
  95. continue;;
  96. *) if [ x"$src" = x ]
  97. then
  98. src=$1
  99. else
  100. # this colon is to work around a 386BSD /bin/sh bug
  101. :
  102. dst=$1
  103. fi
  104. shift
  105. continue;;
  106. esac
  107. done
  108. if [ x"$src" = x ]
  109. then
  110. echo "$0: no input file specified" >&2
  111. exit 1
  112. else
  113. :
  114. fi
  115. if [ x"$dir_arg" != x ]; then
  116. dst=$src
  117. src=""
  118. if [ -d "$dst" ]; then
  119. instcmd=:
  120. chmodcmd=""
  121. else
  122. instcmd=$mkdirprog
  123. fi
  124. else
  125. # Waiting for this to be detected by the "$instcmd $src $dsttmp" command
  126. # might cause directories to be created, which would be especially bad
  127. # if $src (and thus $dsttmp) contains '*'.
  128. if [ -f "$src" ] || [ -d "$src" ]
  129. then
  130. :
  131. else
  132. echo "$0: $src does not exist" >&2
  133. exit 1
  134. fi
  135. if [ x"$dst" = x ]
  136. then
  137. echo "$0: no destination specified" >&2
  138. exit 1
  139. else
  140. :
  141. fi
  142. # If destination is a directory, append the input filename; if your system
  143. # does not like double slashes in filenames, you may need to add some logic
  144. if [ -d "$dst" ]
  145. then
  146. dst=$dst/`basename "$src"`
  147. else
  148. :
  149. fi
  150. fi
  151. ## this sed command emulates the dirname command
  152. dstdir=`echo "$dst" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
  153. # Make sure that the destination directory exists.
  154. # this part is taken from Noah Friedman's mkinstalldirs script
  155. # Skip lots of stat calls in the usual case.
  156. if [ ! -d "$dstdir" ]; then
  157. defaultIFS='
  158. '
  159. IFS="${IFS-$defaultIFS}"
  160. oIFS=$IFS
  161. # Some sh's can't handle IFS=/ for some reason.
  162. IFS='%'
  163. set - `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'`
  164. IFS=$oIFS
  165. pathcomp=''
  166. while [ $# -ne 0 ] ; do
  167. pathcomp=$pathcomp$1
  168. shift
  169. if [ ! -d "$pathcomp" ] ;
  170. then
  171. $mkdirprog "$pathcomp"
  172. else
  173. :
  174. fi
  175. pathcomp=$pathcomp/
  176. done
  177. fi
  178. if [ x"$dir_arg" != x ]
  179. then
  180. $doit $instcmd "$dst" &&
  181. if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dst"; else : ; fi &&
  182. if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dst"; else : ; fi &&
  183. if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dst"; else : ; fi &&
  184. if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dst"; else : ; fi
  185. else
  186. # If we're going to rename the final executable, determine the name now.
  187. if [ x"$transformarg" = x ]
  188. then
  189. dstfile=`basename "$dst"`
  190. else
  191. dstfile=`basename "$dst" $transformbasename |
  192. sed $transformarg`$transformbasename
  193. fi
  194. # don't allow the sed command to completely eliminate the filename
  195. if [ x"$dstfile" = x ]
  196. then
  197. dstfile=`basename "$dst"`
  198. else
  199. :
  200. fi
  201. # Make a couple of temp file names in the proper directory.
  202. dsttmp=$dstdir/_inst.$$_
  203. rmtmp=$dstdir/_rm.$$_
  204. # Trap to clean up temp files at exit.
  205. trap 'status=$?; rm -f "$dsttmp" "$rmtmp" && exit $status' 0
  206. trap '(exit $?); exit' 1 2 13 15
  207. # Move or copy the file name to the temp name
  208. $doit $instcmd "$src" "$dsttmp" &&
  209. # and set any options; do chmod last to preserve setuid bits
  210. # If any of these fail, we abort the whole thing. If we want to
  211. # ignore errors from any of these, just make sure not to ignore
  212. # errors from the above "$doit $instcmd $src $dsttmp" command.
  213. if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp"; else :;fi &&
  214. if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp"; else :;fi &&
  215. if [ x"$stripcmd" != x ]; then $doit $stripcmd "$dsttmp"; else :;fi &&
  216. if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; else :;fi &&
  217. # Now remove or move aside any old file at destination location. We try this
  218. # two ways since rm can't unlink itself on some systems and the destination
  219. # file might be busy for other reasons. In this case, the final cleanup
  220. # might fail but the new file should still install successfully.
  221. {
  222. if [ -f "$dstdir/$dstfile" ]
  223. then
  224. $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null ||
  225. $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null ||
  226. {
  227. echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2
  228. (exit 1); exit
  229. }
  230. else
  231. :
  232. fi
  233. } &&
  234. # Now rename the file to the real destination.
  235. $doit $mvcmd "$dsttmp" "$dstdir/$dstfile"
  236. fi &&
  237. # The final little trick to "correctly" pass the exit status to the exit trap.
  238. {
  239. (exit 0); exit
  240. }