install-sh (141098) | install-sh (174832) |
---|---|
1#!/bin/sh 2# install - install a program, script, or datafile 3 | 1#!/bin/sh 2# install - install a program, script, or datafile 3 |
4scriptversion=2004-04-01.17 | 4scriptversion=2005-05-14.22 |
5 6# This originates from X11R5 (mit/util/scripts/install.sh), which was 7# later released in X11R6 (xc/config/util/install.sh) with the 8# following copyright and license. 9# 10# Copyright (C) 1994 X Consortium 11# 12# Permission is hereby granted, free of charge, to any person obtaining a copy --- 40 unchanged lines hidden (view full) --- 53cpprog="${CPPROG-cp}" 54chmodprog="${CHMODPROG-chmod}" 55chownprog="${CHOWNPROG-chown}" 56chgrpprog="${CHGRPPROG-chgrp}" 57stripprog="${STRIPPROG-strip}" 58rmprog="${RMPROG-rm}" 59mkdirprog="${MKDIRPROG-mkdir}" 60 | 5 6# This originates from X11R5 (mit/util/scripts/install.sh), which was 7# later released in X11R6 (xc/config/util/install.sh) with the 8# following copyright and license. 9# 10# Copyright (C) 1994 X Consortium 11# 12# Permission is hereby granted, free of charge, to any person obtaining a copy --- 40 unchanged lines hidden (view full) --- 53cpprog="${CPPROG-cp}" 54chmodprog="${CHMODPROG-chmod}" 55chownprog="${CHOWNPROG-chown}" 56chgrpprog="${CHGRPPROG-chgrp}" 57stripprog="${STRIPPROG-strip}" 58rmprog="${RMPROG-rm}" 59mkdirprog="${MKDIRPROG-mkdir}" 60 |
61transformbasename= 62transform_arg= 63instcmd="$mvprog" | |
64chmodcmd="$chmodprog 0755" 65chowncmd= 66chgrpcmd= 67stripcmd= 68rmcmd="$rmprog -f" 69mvcmd="$mvprog" 70src= 71dst= 72dir_arg= | 61chmodcmd="$chmodprog 0755" 62chowncmd= 63chgrpcmd= 64stripcmd= 65rmcmd="$rmprog -f" 66mvcmd="$mvprog" 67src= 68dst= 69dir_arg= |
70dstarg= 71no_target_directory= |
|
73 | 72 |
74usage="Usage: $0 [OPTION]... SRCFILE DSTFILE | 73usage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE |
75 or: $0 [OPTION]... SRCFILES... DIRECTORY | 74 or: $0 [OPTION]... SRCFILES... DIRECTORY |
76 or: $0 -d DIRECTORIES... | 75 or: $0 [OPTION]... -t DIRECTORY SRCFILES... 76 or: $0 [OPTION]... -d DIRECTORIES... |
77 | 77 |
78In the first form, install SRCFILE to DSTFILE, removing SRCFILE by default. 79In the second, create the directory path DIR. | 78In the 1st form, copy SRCFILE to DSTFILE. 79In the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 80In the 4th, create DIRECTORIES. |
80 81Options: | 81 82Options: |
82-b=TRANSFORMBASENAME 83-c copy source (using $cpprog) instead of moving (using $mvprog). | 83-c (ignored) |
84-d create directories instead of installing files. | 84-d create directories instead of installing files. |
85-g GROUP $chgrp installed files to GROUP. 86-m MODE $chmod installed files to MODE. 87-o USER $chown installed files to USER. 88-s strip installed files (using $stripprog). 89-t=TRANSFORM | 85-g GROUP $chgrpprog installed files to GROUP. 86-m MODE $chmodprog installed files to MODE. 87-o USER $chownprog installed files to USER. 88-s $stripprog installed files. 89-t DIRECTORY install into DIRECTORY. 90-T report an error if DSTFILE is a directory. |
90--help display this help and exit. 91--version display version info and exit. 92 93Environment variables override the default commands: 94 CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG 95" 96 97while test -n "$1"; do 98 case $1 in | 91--help display this help and exit. 92--version display version info and exit. 93 94Environment variables override the default commands: 95 CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG 96" 97 98while test -n "$1"; do 99 case $1 in |
99 -b=*) transformbasename=`echo $1 | sed 's/-b=//'` 100 shift | 100 -c) shift |
101 continue;; 102 | 101 continue;; 102 |
103 -c) instcmd=$cpprog 104 shift 105 continue;; 106 | |
107 -d) dir_arg=true 108 shift 109 continue;; 110 111 -g) chgrpcmd="$chgrpprog $2" 112 shift 113 shift 114 continue;; 115 | 103 -d) dir_arg=true 104 shift 105 continue;; 106 107 -g) chgrpcmd="$chgrpprog $2" 108 shift 109 shift 110 continue;; 111 |
116 --help) echo "$usage"; exit 0;; | 112 --help) echo "$usage"; exit $?;; |
117 118 -m) chmodcmd="$chmodprog $2" 119 shift 120 shift 121 continue;; 122 123 -o) chowncmd="$chownprog $2" 124 shift 125 shift 126 continue;; 127 128 -s) stripcmd=$stripprog 129 shift 130 continue;; 131 | 113 114 -m) chmodcmd="$chmodprog $2" 115 shift 116 shift 117 continue;; 118 119 -o) chowncmd="$chownprog $2" 120 shift 121 shift 122 continue;; 123 124 -s) stripcmd=$stripprog 125 shift 126 continue;; 127 |
132 -t=*) transformarg=`echo $1 | sed 's/-t=//'` 133 shift 134 continue;; | 128 -t) dstarg=$2 129 shift 130 shift 131 continue;; |
135 | 132 |
136 --version) echo "$0 $scriptversion"; exit 0;; | 133 -T) no_target_directory=true 134 shift 135 continue;; |
137 | 136 |
137 --version) echo "$0 $scriptversion"; exit $?;; 138 |
|
138 *) # When -d is used, all remaining arguments are directories to create. | 139 *) # When -d is used, all remaining arguments are directories to create. |
139 test -n "$dir_arg" && break | 140 # When -t is used, the destination is already specified. 141 test -n "$dir_arg$dstarg" && break |
140 # Otherwise, the last argument is the destination. Remove it from $@. 141 for arg 142 do 143 if test -n "$dstarg"; then 144 # $@ is not empty: it contains at least $arg. 145 set fnord "$@" "$dstarg" 146 shift # fnord 147 fi --- 21 unchanged lines hidden (view full) --- 169 -*) src=./$src ;; 170 esac 171 172 if test -n "$dir_arg"; then 173 dst=$src 174 src= 175 176 if test -d "$dst"; then | 142 # Otherwise, the last argument is the destination. Remove it from $@. 143 for arg 144 do 145 if test -n "$dstarg"; then 146 # $@ is not empty: it contains at least $arg. 147 set fnord "$@" "$dstarg" 148 shift # fnord 149 fi --- 21 unchanged lines hidden (view full) --- 171 -*) src=./$src ;; 172 esac 173 174 if test -n "$dir_arg"; then 175 dst=$src 176 src= 177 178 if test -d "$dst"; then |
177 instcmd=: | 179 mkdircmd=: |
178 chmodcmd= 179 else | 180 chmodcmd= 181 else |
180 instcmd=$mkdirprog | 182 mkdircmd=$mkdirprog |
181 fi 182 else | 183 fi 184 else |
183 # Waiting for this to be detected by the "$instcmd $src $dsttmp" command | 185 # Waiting for this to be detected by the "$cpprog $src $dsttmp" command |
184 # might cause directories to be created, which would be especially bad 185 # if $src (and thus $dsttmp) contains '*'. 186 if test ! -f "$src" && test ! -d "$src"; then 187 echo "$0: $src does not exist." >&2 188 exit 1 189 fi 190 191 if test -z "$dstarg"; then --- 5 unchanged lines hidden (view full) --- 197 # Protect names starting with `-'. 198 case $dst in 199 -*) dst=./$dst ;; 200 esac 201 202 # If destination is a directory, append the input filename; won't work 203 # if double slashes aren't ignored. 204 if test -d "$dst"; then | 186 # might cause directories to be created, which would be especially bad 187 # if $src (and thus $dsttmp) contains '*'. 188 if test ! -f "$src" && test ! -d "$src"; then 189 echo "$0: $src does not exist." >&2 190 exit 1 191 fi 192 193 if test -z "$dstarg"; then --- 5 unchanged lines hidden (view full) --- 199 # Protect names starting with `-'. 200 case $dst in 201 -*) dst=./$dst ;; 202 esac 203 204 # If destination is a directory, append the input filename; won't work 205 # if double slashes aren't ignored. 206 if test -d "$dst"; then |
207 if test -n "$no_target_directory"; then 208 echo "$0: $dstarg: Is a directory" >&2 209 exit 1 210 fi |
|
205 dst=$dst/`basename "$src"` 206 fi 207 fi 208 209 # This sed command emulates the dirname command. | 211 dst=$dst/`basename "$src"` 212 fi 213 fi 214 215 # This sed command emulates the dirname command. |
210 dstdir=`echo "$dst" | sed -e 's,[^/]*$,,;s,/$,,;s,^$,.,'` | 216 dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'` |
211 212 # Make sure that the destination directory exists. 213 214 # Skip lots of stat calls in the usual case. 215 if test ! -d "$dstdir"; then 216 defaultIFS=' 217 ' 218 IFS="${IFS-$defaultIFS}" 219 220 oIFS=$IFS 221 # Some sh's can't handle IFS=/ for some reason. 222 IFS='%' | 217 218 # Make sure that the destination directory exists. 219 220 # Skip lots of stat calls in the usual case. 221 if test ! -d "$dstdir"; then 222 defaultIFS=' 223 ' 224 IFS="${IFS-$defaultIFS}" 225 226 oIFS=$IFS 227 # Some sh's can't handle IFS=/ for some reason. 228 IFS='%' |
223 set - `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` | 229 set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` 230 shift |
224 IFS=$oIFS 225 226 pathcomp= 227 228 while test $# -ne 0 ; do 229 pathcomp=$pathcomp$1 230 shift 231 if test ! -d "$pathcomp"; then | 231 IFS=$oIFS 232 233 pathcomp= 234 235 while test $# -ne 0 ; do 236 pathcomp=$pathcomp$1 237 shift 238 if test ! -d "$pathcomp"; then |
232 $mkdirprog "$pathcomp" || lasterr=$? | 239 $mkdirprog "$pathcomp" |
233 # mkdir can fail with a `File exist' error in case several 234 # install-sh are creating the directory concurrently. This 235 # is OK. | 240 # mkdir can fail with a `File exist' error in case several 241 # install-sh are creating the directory concurrently. This 242 # is OK. |
236 test ! -d "$pathcomp" && { (exit ${lasterr-1}); exit; } | 243 test -d "$pathcomp" || exit |
237 fi 238 pathcomp=$pathcomp/ 239 done 240 fi 241 242 if test -n "$dir_arg"; then | 244 fi 245 pathcomp=$pathcomp/ 246 done 247 fi 248 249 if test -n "$dir_arg"; then |
243 $doit $instcmd "$dst" \ | 250 $doit $mkdircmd "$dst" \ |
244 && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \ 245 && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \ 246 && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \ 247 && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; } 248 249 else | 251 && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \ 252 && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \ 253 && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \ 254 && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; } 255 256 else |
250 # If we're going to rename the final executable, determine the name now. 251 if test -z "$transformarg"; then 252 dstfile=`basename "$dst"` 253 else 254 dstfile=`basename "$dst" $transformbasename \ 255 | sed $transformarg`$transformbasename 256 fi | 257 dstfile=`basename "$dst"` |
257 | 258 |
258 # don't allow the sed command to completely eliminate the filename. 259 test -z "$dstfile" && dstfile=`basename "$dst"` 260 | |
261 # Make a couple of temp file names in the proper directory. 262 dsttmp=$dstdir/_inst.$$_ 263 rmtmp=$dstdir/_rm.$$_ 264 265 # Trap to clean up those temp files at exit. | 259 # Make a couple of temp file names in the proper directory. 260 dsttmp=$dstdir/_inst.$$_ 261 rmtmp=$dstdir/_rm.$$_ 262 263 # Trap to clean up those temp files at exit. |
266 trap 'status=$?; rm -f "$dsttmp" "$rmtmp" && exit $status' 0 | 264 trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 |
267 trap '(exit $?); exit' 1 2 13 15 268 | 265 trap '(exit $?); exit' 1 2 13 15 266 |
269 # Move or copy the file name to the temp name 270 $doit $instcmd "$src" "$dsttmp" && | 267 # Copy the file name to the temp name. 268 $doit $cpprog "$src" "$dsttmp" && |
271 272 # and set any options; do chmod last to preserve setuid bits. 273 # 274 # If any of these fail, we abort the whole thing. If we want to 275 # ignore errors from any of these, just make sure not to ignore | 269 270 # and set any options; do chmod last to preserve setuid bits. 271 # 272 # If any of these fail, we abort the whole thing. If we want to 273 # ignore errors from any of these, just make sure not to ignore |
276 # errors from the above "$doit $instcmd $src $dsttmp" command. | 274 # errors from the above "$doit $cpprog $src $dsttmp" command. |
277 # 278 { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \ 279 && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \ 280 && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \ 281 && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } && 282 283 # Now rename the file to the real destination. 284 { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \ --- 8 unchanged lines hidden (view full) --- 293 # reasons. In this case, the final cleanup might fail but the new 294 # file should still install successfully. 295 { 296 if test -f "$dstdir/$dstfile"; then 297 $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \ 298 || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \ 299 || { 300 echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2 | 275 # 276 { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \ 277 && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \ 278 && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \ 279 && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } && 280 281 # Now rename the file to the real destination. 282 { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \ --- 8 unchanged lines hidden (view full) --- 291 # reasons. In this case, the final cleanup might fail but the new 292 # file should still install successfully. 293 { 294 if test -f "$dstdir/$dstfile"; then 295 $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \ 296 || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \ 297 || { 298 echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2 |
301 (exit 1); exit | 299 (exit 1); exit 1 |
302 } 303 else 304 : 305 fi 306 } && 307 308 # Now rename the file to the real destination. 309 $doit $mvcmd "$dsttmp" "$dstdir/$dstfile" 310 } 311 } | 300 } 301 else 302 : 303 fi 304 } && 305 306 # Now rename the file to the real destination. 307 $doit $mvcmd "$dsttmp" "$dstdir/$dstfile" 308 } 309 } |
312 fi || { (exit 1); exit; } | 310 fi || { (exit 1); exit 1; } |
313done 314 315# The final little trick to "correctly" pass the exit status to the exit trap. 316{ | 311done 312 313# The final little trick to "correctly" pass the exit status to the exit trap. 314{ |
317 (exit 0); exit | 315 (exit 0); exit 0 |
318} 319 320# Local variables: 321# eval: (add-hook 'write-file-hooks 'time-stamp) 322# time-stamp-start: "scriptversion=" 323# time-stamp-format: "%:y-%02m-%02d.%02H" 324# time-stamp-end: "$" 325# End: | 316} 317 318# Local variables: 319# eval: (add-hook 'write-file-hooks 'time-stamp) 320# time-stamp-start: "scriptversion=" 321# time-stamp-format: "%:y-%02m-%02d.%02H" 322# time-stamp-end: "$" 323# End: |