install-sh 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301
  1. #!/bin/sh
  2. #
  3. # $NetBSD: install-sh.in,v 1.6 2012/01/11 13:07:31 hans Exp $
  4. # This script now also installs multiple files, but might choke on installing
  5. # multiple files with spaces in the file names.
  6. #
  7. # install - install a program, script, or datafile
  8. # This comes from X11R5 (mit/util/scripts/install.sh).
  9. #
  10. # Copyright 1991 by the Massachusetts Institute of Technology
  11. #
  12. # Permission to use, copy, modify, distribute, and sell this software and its
  13. # documentation for any purpose is hereby granted without fee, provided that
  14. # the above copyright notice appear in all copies and that both that
  15. # copyright notice and this permission notice appear in supporting
  16. # documentation, and that the name of M.I.T. not be used in advertising or
  17. # publicity pertaining to distribution of the software without specific,
  18. # written prior permission. M.I.T. makes no representations about the
  19. # suitability of this software for any purpose. It is provided "as is"
  20. # without express or implied warranty.
  21. #
  22. # Calling this script install-sh is preferred over install.sh, to prevent
  23. # `make' implicit rules from creating a file called install from it
  24. # when there is no Makefile.
  25. #
  26. # This script is compatible with the BSD install script, but was written
  27. # from scratch.
  28. # set DOITPROG to echo to test this script
  29. # Don't use :- since 4.3BSD and earlier shells don't like it.
  30. doit="${DOITPROG-}"
  31. # put in absolute paths if you don't have them in your path; or use env. vars.
  32. awkprog="${AWKPROG-awk}"
  33. mvprog="${MVPROG-mv}"
  34. cpprog="${CPPROG-cp}"
  35. chmodprog="${CHMODPROG-chmod}"
  36. chownprog="${CHOWNPROG-chown}"
  37. chgrpprog="${CHGRPPROG-chgrp}"
  38. stripprog="${STRIPPROG-strip}"
  39. rmprog="${RMPROG-rm}"
  40. mkdirprog="${MKDIRPROG-mkdir}"
  41. instcmd="$cpprog"
  42. instflags=""
  43. pathcompchmodcmd="$chmodprog 755"
  44. chmodcmd="$chmodprog 755"
  45. chowncmd=""
  46. chgrpcmd=""
  47. stripcmd=""
  48. stripflags=""
  49. rmcmd="$rmprog -f"
  50. mvcmd="$mvprog"
  51. src=""
  52. msrc=""
  53. dst=""
  54. dir_arg=""
  55. suffix=""
  56. suffixfmt=""
  57. while [ x"$1" != x ]; do
  58. case $1 in
  59. -b) suffix=".old"
  60. shift
  61. continue;;
  62. -B) suffixfmt="$2"
  63. shift
  64. shift
  65. continue;;
  66. -c) instcmd="$cpprog"
  67. shift
  68. continue;;
  69. -d) dir_arg=true
  70. shift
  71. continue;;
  72. -m) chmodcmd="$chmodprog $2"
  73. shift
  74. shift
  75. continue;;
  76. -m*)
  77. chmodcmd="$chmodprog ${1#-m}"
  78. shift
  79. continue;;
  80. -o) chowncmd="$chownprog $2"
  81. shift
  82. shift
  83. continue;;
  84. -g) chgrpcmd="$chgrpprog $2"
  85. shift
  86. shift
  87. continue;;
  88. -s) stripcmd="$stripprog"
  89. shift
  90. continue;;
  91. -S) stripcmd="$stripprog"
  92. stripflags="-S $2 $stripflags"
  93. shift
  94. shift
  95. continue;;
  96. -p) instflags="-p"
  97. shift
  98. continue;;
  99. *) if [ x"$msrc" = x ]
  100. then
  101. msrc="$dst"
  102. else
  103. msrc="$msrc $dst"
  104. fi
  105. src="$dst"
  106. dst="$1"
  107. shift
  108. continue;;
  109. esac
  110. done
  111. if [ x"$dir_arg" = x ]
  112. then
  113. dstisfile=""
  114. if [ ! -d "$dst" ]
  115. then
  116. if [ x"$msrc" = x"$src" ]
  117. then
  118. dstisfile=true
  119. else
  120. echo "install: destination is not a directory"
  121. exit 1
  122. fi
  123. fi
  124. else
  125. msrc="$msrc $dst"
  126. fi
  127. if [ x"$msrc" = x ]
  128. then
  129. echo "install: no destination specified"
  130. exit 1
  131. fi
  132. for srcarg in $msrc; do
  133. if [ x"$dir_arg" != x ]; then
  134. dstarg="$srcarg"
  135. else
  136. dstarg="$dst"
  137. # Waiting for this to be detected by the "$instcmd $srcarg $dsttmp" command
  138. # might cause directories to be created, which would be especially bad
  139. # if $src (and thus $dsttmp) contains '*'.
  140. if [ -f "$srcarg" ]
  141. then
  142. doinst="$instcmd $instflags"
  143. elif [ -d "$srcarg" ]
  144. then
  145. echo "install: $srcarg: not a regular file"
  146. exit 1
  147. elif [ "$srcarg" = "/dev/null" ]
  148. then
  149. doinst="$cpprog"
  150. else
  151. echo "install: $srcarg does not exist"
  152. exit 1
  153. fi
  154. # If destination is a directory, append the input filename; if your system
  155. # does not like double slashes in filenames, you may need to add some logic
  156. if [ -d "$dstarg" ]
  157. then
  158. dstarg="$dstarg"/`basename "$srcarg"`
  159. fi
  160. fi
  161. ## this sed command emulates the dirname command
  162. dstdir=`echo "$dstarg" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
  163. # Make sure that the destination directory exists.
  164. # this part is taken from Noah Friedman's mkinstalldirs script
  165. # Skip lots of stat calls in the usual case.
  166. if [ ! -d "$dstdir" ]; then
  167. defaultIFS='
  168. '
  169. IFS="${IFS-${defaultIFS}}"
  170. oIFS="${IFS}"
  171. # Some sh's can't handle IFS=/ for some reason.
  172. IFS='%'
  173. set - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
  174. IFS="${oIFS}"
  175. pathcomp=''
  176. while [ $# -ne 0 ] ; do
  177. pathcomp="${pathcomp}${1}"
  178. shift
  179. if [ ! -d "${pathcomp}" ] ;
  180. then
  181. $doit $mkdirprog "${pathcomp}"
  182. if [ x"$chowncmd" != x ]; then $doit $chowncmd "${pathcomp}"; else true ; fi &&
  183. if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "${pathcomp}"; else true ; fi &&
  184. if [ x"$pathcompchmodcmd" != x ]; then $doit $pathcompchmodcmd "${pathcomp}"; else true ; fi
  185. else
  186. true
  187. fi
  188. pathcomp="${pathcomp}/"
  189. done
  190. fi
  191. if [ x"$dir_arg" != x ]
  192. then
  193. if [ -d "$dstarg" ]; then
  194. true
  195. else
  196. $doit $mkdirprog "$dstarg" &&
  197. if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dstarg"; else true ; fi &&
  198. if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dstarg"; else true ; fi &&
  199. if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dstarg"; else true ; fi
  200. fi
  201. else
  202. if [ x"$dstisfile" = x ]
  203. then
  204. file=$srcarg
  205. else
  206. file=$dst
  207. fi
  208. dstfile=`basename "$file"`
  209. dstfinal="$dstdir/$dstfile"
  210. # Make a temp file name in the proper directory.
  211. dsttmp=$dstdir/#inst.$$#
  212. # Make a backup file name in the proper directory.
  213. case x$suffixfmt in
  214. *%*) suffix=`echo x |
  215. $awkprog -v bname="$dstfinal" -v fmt="$suffixfmt" '
  216. { cnt = 0;
  217. do {
  218. sfx = sprintf(fmt, cnt++);
  219. name = bname sfx;
  220. } while (system("test -f " name) == 0);
  221. print sfx; }' -`;;
  222. x) ;;
  223. *) suffix="$suffixfmt";;
  224. esac
  225. dstbackup="$dstfinal$suffix"
  226. # Move or copy the file name to the temp name
  227. $doit $doinst $srcarg "$dsttmp" &&
  228. trap "rm -f ${dsttmp}" 0 &&
  229. # and set any options; do chmod last to preserve setuid bits
  230. # If any of these fail, we abort the whole thing. If we want to
  231. # ignore errors from any of these, just make sure not to ignore
  232. # errors from the above "$doit $instcmd $src $dsttmp" command.
  233. if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp"; else true;fi &&
  234. if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp"; else true;fi &&
  235. if [ x"$stripcmd" != x ]; then $doit $stripcmd $stripflags "$dsttmp"; else true;fi &&
  236. if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; else true;fi &&
  237. # Now rename the file to the real destination.
  238. if [ x"$suffix" != x ] && [ -f "$dstfinal" ]
  239. then
  240. $doit $mvcmd "$dstfinal" "$dstbackup"
  241. else
  242. $doit $rmcmd -f "$dstfinal"
  243. fi &&
  244. $doit $mvcmd "$dsttmp" "$dstfinal"
  245. fi
  246. done &&
  247. exit 0