install-sh 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291
  1. #!/bin/sh
  2. #
  3. # $NetBSD: install-sh.in,v 1.5 2010/10/08 19:57:05 tez 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. pathcompchmodcmd="$chmodprog 755"
  43. chmodcmd="$chmodprog 755"
  44. chowncmd=""
  45. chgrpcmd=""
  46. stripcmd=""
  47. stripflags=""
  48. rmcmd="$rmprog -f"
  49. mvcmd="$mvprog"
  50. src=""
  51. msrc=""
  52. dst=""
  53. dir_arg=""
  54. suffix=""
  55. suffixfmt=""
  56. while [ x"$1" != x ]; do
  57. case $1 in
  58. -b) suffix=".old"
  59. shift
  60. continue;;
  61. -B) suffixfmt="$2"
  62. shift
  63. shift
  64. continue;;
  65. -c) instcmd="$cpprog"
  66. shift
  67. continue;;
  68. -d) dir_arg=true
  69. shift
  70. continue;;
  71. -m) chmodcmd="$chmodprog $2"
  72. shift
  73. shift
  74. continue;;
  75. -o) chowncmd="$chownprog $2"
  76. shift
  77. shift
  78. continue;;
  79. -g) chgrpcmd="$chgrpprog $2"
  80. shift
  81. shift
  82. continue;;
  83. -s) stripcmd="$stripprog"
  84. shift
  85. continue;;
  86. -S) stripcmd="$stripprog"
  87. stripflags="-S $2 $stripflags"
  88. shift
  89. shift
  90. continue;;
  91. *) if [ x"$msrc" = x ]
  92. then
  93. msrc="$dst"
  94. else
  95. msrc="$msrc $dst"
  96. fi
  97. src="$dst"
  98. dst="$1"
  99. shift
  100. continue;;
  101. esac
  102. done
  103. if [ x"$dir_arg" = x ]
  104. then
  105. dstisfile=""
  106. if [ ! -d "$dst" ]
  107. then
  108. if [ x"$msrc" = x"$src" ]
  109. then
  110. dstisfile=true
  111. else
  112. echo "install: destination is not a directory"
  113. exit 1
  114. fi
  115. fi
  116. else
  117. msrc="$msrc $dst"
  118. fi
  119. if [ x"$msrc" = x ]
  120. then
  121. echo "install: no destination specified"
  122. exit 1
  123. fi
  124. for srcarg in $msrc; do
  125. if [ x"$dir_arg" != x ]; then
  126. dstarg="$srcarg"
  127. else
  128. dstarg="$dst"
  129. # Waiting for this to be detected by the "$instcmd $srcarg $dsttmp" command
  130. # might cause directories to be created, which would be especially bad
  131. # if $src (and thus $dsttmp) contains '*'.
  132. if [ -f "$srcarg" ]
  133. then
  134. doinst="$instcmd"
  135. elif [ -d "$srcarg" ]
  136. then
  137. echo "install: $srcarg: not a regular file"
  138. exit 1
  139. elif [ "$srcarg" = "/dev/null" ]
  140. then
  141. doinst="$cpprog"
  142. else
  143. echo "install: $srcarg does not exist"
  144. exit 1
  145. fi
  146. # If destination is a directory, append the input filename; if your system
  147. # does not like double slashes in filenames, you may need to add some logic
  148. if [ -d "$dstarg" ]
  149. then
  150. dstarg="$dstarg"/`basename "$srcarg"`
  151. fi
  152. fi
  153. ## this sed command emulates the dirname command
  154. dstdir=`echo "$dstarg" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'`
  155. # Make sure that the destination directory exists.
  156. # this part is taken from Noah Friedman's mkinstalldirs script
  157. # Skip lots of stat calls in the usual case.
  158. if [ ! -d "$dstdir" ]; then
  159. defaultIFS='
  160. '
  161. IFS="${IFS-${defaultIFS}}"
  162. oIFS="${IFS}"
  163. # Some sh's can't handle IFS=/ for some reason.
  164. IFS='%'
  165. set - `echo ${dstdir} | sed -e 's@/@%@g' -e 's@^%@/@'`
  166. IFS="${oIFS}"
  167. pathcomp=''
  168. while [ $# -ne 0 ] ; do
  169. pathcomp="${pathcomp}${1}"
  170. shift
  171. if [ ! -d "${pathcomp}" ] ;
  172. then
  173. $doit $mkdirprog "${pathcomp}"
  174. if [ x"$chowncmd" != x ]; then $doit $chowncmd "${pathcomp}"; else true ; fi &&
  175. if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "${pathcomp}"; else true ; fi &&
  176. if [ x"$pathcompchmodcmd" != x ]; then $doit $pathcompchmodcmd "${pathcomp}"; else true ; fi
  177. else
  178. true
  179. fi
  180. pathcomp="${pathcomp}/"
  181. done
  182. fi
  183. if [ x"$dir_arg" != x ]
  184. then
  185. if [ -d "$dstarg" ]; then
  186. true
  187. else
  188. $doit $mkdirprog "$dstarg" &&
  189. if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dstarg"; else true ; fi &&
  190. if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dstarg"; else true ; fi &&
  191. if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dstarg"; else true ; fi
  192. fi
  193. else
  194. if [ x"$dstisfile" = x ]
  195. then
  196. file=$srcarg
  197. else
  198. file=$dst
  199. fi
  200. dstfile=`basename "$file"`
  201. dstfinal="$dstdir/$dstfile"
  202. # Make a temp file name in the proper directory.
  203. dsttmp=$dstdir/#inst.$$#
  204. # Make a backup file name in the proper directory.
  205. case x$suffixfmt in
  206. *%*) suffix=`echo x |
  207. $awkprog -v bname="$dstfinal" -v fmt="$suffixfmt" '
  208. { cnt = 0;
  209. do {
  210. sfx = sprintf(fmt, cnt++);
  211. name = bname sfx;
  212. } while (system("test -f " name) == 0);
  213. print sfx; }' -`;;
  214. x) ;;
  215. *) suffix="$suffixfmt";;
  216. esac
  217. dstbackup="$dstfinal$suffix"
  218. # Move or copy the file name to the temp name
  219. $doit $doinst $srcarg "$dsttmp" &&
  220. trap "rm -f ${dsttmp}" 0 &&
  221. # and set any options; do chmod last to preserve setuid bits
  222. # If any of these fail, we abort the whole thing. If we want to
  223. # ignore errors from any of these, just make sure not to ignore
  224. # errors from the above "$doit $instcmd $src $dsttmp" command.
  225. if [ x"$chowncmd" != x ]; then $doit $chowncmd "$dsttmp"; else true;fi &&
  226. if [ x"$chgrpcmd" != x ]; then $doit $chgrpcmd "$dsttmp"; else true;fi &&
  227. if [ x"$stripcmd" != x ]; then $doit $stripcmd $stripflags "$dsttmp"; else true;fi &&
  228. if [ x"$chmodcmd" != x ]; then $doit $chmodcmd "$dsttmp"; else true;fi &&
  229. # Now rename the file to the real destination.
  230. if [ x"$suffix" != x ] && [ -f "$dstfinal" ]
  231. then
  232. $doit $mvcmd "$dstfinal" "$dstbackup"
  233. else
  234. $doit $rmcmd -f "$dstfinal"
  235. fi &&
  236. $doit $mvcmd "$dsttmp" "$dstfinal"
  237. fi
  238. done &&
  239. exit 0