install-sh revision bd1da9d7
1bd1da9d7Smrg#!/bin/sh 2bd1da9d7Smrg# install - install a program, script, or datafile 3bd1da9d7Smrg 4bd1da9d7Smrgscriptversion=2005-05-14.22 5bd1da9d7Smrg 6bd1da9d7Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was 7bd1da9d7Smrg# later released in X11R6 (xc/config/util/install.sh) with the 8bd1da9d7Smrg# following copyright and license. 9bd1da9d7Smrg# 10bd1da9d7Smrg# Copyright (C) 1994 X Consortium 11bd1da9d7Smrg# 12bd1da9d7Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy 13bd1da9d7Smrg# of this software and associated documentation files (the "Software"), to 14bd1da9d7Smrg# deal in the Software without restriction, including without limitation the 15bd1da9d7Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or 16bd1da9d7Smrg# sell copies of the Software, and to permit persons to whom the Software is 17bd1da9d7Smrg# furnished to do so, subject to the following conditions: 18bd1da9d7Smrg# 19bd1da9d7Smrg# The above copyright notice and this permission notice shall be included in 20bd1da9d7Smrg# all copies or substantial portions of the Software. 21bd1da9d7Smrg# 22bd1da9d7Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 23bd1da9d7Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 24bd1da9d7Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 25bd1da9d7Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN 26bd1da9d7Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC- 27bd1da9d7Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. 28bd1da9d7Smrg# 29bd1da9d7Smrg# Except as contained in this notice, the name of the X Consortium shall not 30bd1da9d7Smrg# be used in advertising or otherwise to promote the sale, use or other deal- 31bd1da9d7Smrg# ings in this Software without prior written authorization from the X Consor- 32bd1da9d7Smrg# tium. 33bd1da9d7Smrg# 34bd1da9d7Smrg# 35bd1da9d7Smrg# FSF changes to this file are in the public domain. 36bd1da9d7Smrg# 37bd1da9d7Smrg# Calling this script install-sh is preferred over install.sh, to prevent 38bd1da9d7Smrg# `make' implicit rules from creating a file called install from it 39bd1da9d7Smrg# when there is no Makefile. 40bd1da9d7Smrg# 41bd1da9d7Smrg# This script is compatible with the BSD install script, but was written 42bd1da9d7Smrg# from scratch. It can only install one file at a time, a restriction 43bd1da9d7Smrg# shared with many OS's install programs. 44bd1da9d7Smrg 45bd1da9d7Smrg# set DOITPROG to echo to test this script 46bd1da9d7Smrg 47bd1da9d7Smrg# Don't use :- since 4.3BSD and earlier shells don't like it. 48bd1da9d7Smrgdoit="${DOITPROG-}" 49bd1da9d7Smrg 50bd1da9d7Smrg# put in absolute paths if you don't have them in your path; or use env. vars. 51bd1da9d7Smrg 52bd1da9d7Smrgmvprog="${MVPROG-mv}" 53bd1da9d7Smrgcpprog="${CPPROG-cp}" 54bd1da9d7Smrgchmodprog="${CHMODPROG-chmod}" 55bd1da9d7Smrgchownprog="${CHOWNPROG-chown}" 56bd1da9d7Smrgchgrpprog="${CHGRPPROG-chgrp}" 57bd1da9d7Smrgstripprog="${STRIPPROG-strip}" 58bd1da9d7Smrgrmprog="${RMPROG-rm}" 59bd1da9d7Smrgmkdirprog="${MKDIRPROG-mkdir}" 60bd1da9d7Smrg 61bd1da9d7Smrgchmodcmd="$chmodprog 0755" 62bd1da9d7Smrgchowncmd= 63bd1da9d7Smrgchgrpcmd= 64bd1da9d7Smrgstripcmd= 65bd1da9d7Smrgrmcmd="$rmprog -f" 66bd1da9d7Smrgmvcmd="$mvprog" 67bd1da9d7Smrgsrc= 68bd1da9d7Smrgdst= 69bd1da9d7Smrgdir_arg= 70bd1da9d7Smrgdstarg= 71bd1da9d7Smrgno_target_directory= 72bd1da9d7Smrg 73bd1da9d7Smrgusage="Usage: $0 [OPTION]... [-T] SRCFILE DSTFILE 74bd1da9d7Smrg or: $0 [OPTION]... SRCFILES... DIRECTORY 75bd1da9d7Smrg or: $0 [OPTION]... -t DIRECTORY SRCFILES... 76bd1da9d7Smrg or: $0 [OPTION]... -d DIRECTORIES... 77bd1da9d7Smrg 78bd1da9d7SmrgIn the 1st form, copy SRCFILE to DSTFILE. 79bd1da9d7SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY. 80bd1da9d7SmrgIn the 4th, create DIRECTORIES. 81bd1da9d7Smrg 82bd1da9d7SmrgOptions: 83bd1da9d7Smrg-c (ignored) 84bd1da9d7Smrg-d create directories instead of installing files. 85bd1da9d7Smrg-g GROUP $chgrpprog installed files to GROUP. 86bd1da9d7Smrg-m MODE $chmodprog installed files to MODE. 87bd1da9d7Smrg-o USER $chownprog installed files to USER. 88bd1da9d7Smrg-s $stripprog installed files. 89bd1da9d7Smrg-t DIRECTORY install into DIRECTORY. 90bd1da9d7Smrg-T report an error if DSTFILE is a directory. 91bd1da9d7Smrg--help display this help and exit. 92bd1da9d7Smrg--version display version info and exit. 93bd1da9d7Smrg 94bd1da9d7SmrgEnvironment variables override the default commands: 95bd1da9d7Smrg CHGRPPROG CHMODPROG CHOWNPROG CPPROG MKDIRPROG MVPROG RMPROG STRIPPROG 96bd1da9d7Smrg" 97bd1da9d7Smrg 98bd1da9d7Smrgwhile test -n "$1"; do 99bd1da9d7Smrg case $1 in 100bd1da9d7Smrg -c) shift 101bd1da9d7Smrg continue;; 102bd1da9d7Smrg 103bd1da9d7Smrg -d) dir_arg=true 104bd1da9d7Smrg shift 105bd1da9d7Smrg continue;; 106bd1da9d7Smrg 107bd1da9d7Smrg -g) chgrpcmd="$chgrpprog $2" 108bd1da9d7Smrg shift 109bd1da9d7Smrg shift 110bd1da9d7Smrg continue;; 111bd1da9d7Smrg 112bd1da9d7Smrg --help) echo "$usage"; exit $?;; 113bd1da9d7Smrg 114bd1da9d7Smrg -m) chmodcmd="$chmodprog $2" 115bd1da9d7Smrg shift 116bd1da9d7Smrg shift 117bd1da9d7Smrg continue;; 118bd1da9d7Smrg 119bd1da9d7Smrg -o) chowncmd="$chownprog $2" 120bd1da9d7Smrg shift 121bd1da9d7Smrg shift 122bd1da9d7Smrg continue;; 123bd1da9d7Smrg 124bd1da9d7Smrg -s) stripcmd=$stripprog 125bd1da9d7Smrg shift 126bd1da9d7Smrg continue;; 127bd1da9d7Smrg 128bd1da9d7Smrg -t) dstarg=$2 129bd1da9d7Smrg shift 130bd1da9d7Smrg shift 131bd1da9d7Smrg continue;; 132bd1da9d7Smrg 133bd1da9d7Smrg -T) no_target_directory=true 134bd1da9d7Smrg shift 135bd1da9d7Smrg continue;; 136bd1da9d7Smrg 137bd1da9d7Smrg --version) echo "$0 $scriptversion"; exit $?;; 138bd1da9d7Smrg 139bd1da9d7Smrg *) # When -d is used, all remaining arguments are directories to create. 140bd1da9d7Smrg # When -t is used, the destination is already specified. 141bd1da9d7Smrg test -n "$dir_arg$dstarg" && break 142bd1da9d7Smrg # Otherwise, the last argument is the destination. Remove it from $@. 143bd1da9d7Smrg for arg 144bd1da9d7Smrg do 145bd1da9d7Smrg if test -n "$dstarg"; then 146bd1da9d7Smrg # $@ is not empty: it contains at least $arg. 147bd1da9d7Smrg set fnord "$@" "$dstarg" 148bd1da9d7Smrg shift # fnord 149bd1da9d7Smrg fi 150bd1da9d7Smrg shift # arg 151bd1da9d7Smrg dstarg=$arg 152bd1da9d7Smrg done 153bd1da9d7Smrg break;; 154bd1da9d7Smrg esac 155bd1da9d7Smrgdone 156bd1da9d7Smrg 157bd1da9d7Smrgif test -z "$1"; then 158bd1da9d7Smrg if test -z "$dir_arg"; then 159bd1da9d7Smrg echo "$0: no input file specified." >&2 160bd1da9d7Smrg exit 1 161bd1da9d7Smrg fi 162bd1da9d7Smrg # It's OK to call `install-sh -d' without argument. 163bd1da9d7Smrg # This can happen when creating conditional directories. 164bd1da9d7Smrg exit 0 165bd1da9d7Smrgfi 166bd1da9d7Smrg 167bd1da9d7Smrgfor src 168bd1da9d7Smrgdo 169bd1da9d7Smrg # Protect names starting with `-'. 170bd1da9d7Smrg case $src in 171bd1da9d7Smrg -*) src=./$src ;; 172bd1da9d7Smrg esac 173bd1da9d7Smrg 174bd1da9d7Smrg if test -n "$dir_arg"; then 175bd1da9d7Smrg dst=$src 176bd1da9d7Smrg src= 177bd1da9d7Smrg 178bd1da9d7Smrg if test -d "$dst"; then 179bd1da9d7Smrg mkdircmd=: 180bd1da9d7Smrg chmodcmd= 181bd1da9d7Smrg else 182bd1da9d7Smrg mkdircmd=$mkdirprog 183bd1da9d7Smrg fi 184bd1da9d7Smrg else 185bd1da9d7Smrg # Waiting for this to be detected by the "$cpprog $src $dsttmp" command 186bd1da9d7Smrg # might cause directories to be created, which would be especially bad 187bd1da9d7Smrg # if $src (and thus $dsttmp) contains '*'. 188bd1da9d7Smrg if test ! -f "$src" && test ! -d "$src"; then 189bd1da9d7Smrg echo "$0: $src does not exist." >&2 190bd1da9d7Smrg exit 1 191bd1da9d7Smrg fi 192bd1da9d7Smrg 193bd1da9d7Smrg if test -z "$dstarg"; then 194bd1da9d7Smrg echo "$0: no destination specified." >&2 195bd1da9d7Smrg exit 1 196bd1da9d7Smrg fi 197bd1da9d7Smrg 198bd1da9d7Smrg dst=$dstarg 199bd1da9d7Smrg # Protect names starting with `-'. 200bd1da9d7Smrg case $dst in 201bd1da9d7Smrg -*) dst=./$dst ;; 202bd1da9d7Smrg esac 203bd1da9d7Smrg 204bd1da9d7Smrg # If destination is a directory, append the input filename; won't work 205bd1da9d7Smrg # if double slashes aren't ignored. 206bd1da9d7Smrg if test -d "$dst"; then 207bd1da9d7Smrg if test -n "$no_target_directory"; then 208bd1da9d7Smrg echo "$0: $dstarg: Is a directory" >&2 209bd1da9d7Smrg exit 1 210bd1da9d7Smrg fi 211bd1da9d7Smrg dst=$dst/`basename "$src"` 212bd1da9d7Smrg fi 213bd1da9d7Smrg fi 214bd1da9d7Smrg 215bd1da9d7Smrg # This sed command emulates the dirname command. 216bd1da9d7Smrg dstdir=`echo "$dst" | sed -e 's,/*$,,;s,[^/]*$,,;s,/*$,,;s,^$,.,'` 217bd1da9d7Smrg 218bd1da9d7Smrg # Make sure that the destination directory exists. 219bd1da9d7Smrg 220bd1da9d7Smrg # Skip lots of stat calls in the usual case. 221bd1da9d7Smrg if test ! -d "$dstdir"; then 222bd1da9d7Smrg defaultIFS=' 223bd1da9d7Smrg ' 224bd1da9d7Smrg IFS="${IFS-$defaultIFS}" 225bd1da9d7Smrg 226bd1da9d7Smrg oIFS=$IFS 227bd1da9d7Smrg # Some sh's can't handle IFS=/ for some reason. 228bd1da9d7Smrg IFS='%' 229bd1da9d7Smrg set x `echo "$dstdir" | sed -e 's@/@%@g' -e 's@^%@/@'` 230bd1da9d7Smrg shift 231bd1da9d7Smrg IFS=$oIFS 232bd1da9d7Smrg 233bd1da9d7Smrg pathcomp= 234bd1da9d7Smrg 235bd1da9d7Smrg while test $# -ne 0 ; do 236bd1da9d7Smrg pathcomp=$pathcomp$1 237bd1da9d7Smrg shift 238bd1da9d7Smrg if test ! -d "$pathcomp"; then 239bd1da9d7Smrg $mkdirprog "$pathcomp" 240bd1da9d7Smrg # mkdir can fail with a `File exist' error in case several 241bd1da9d7Smrg # install-sh are creating the directory concurrently. This 242bd1da9d7Smrg # is OK. 243bd1da9d7Smrg test -d "$pathcomp" || exit 244bd1da9d7Smrg fi 245bd1da9d7Smrg pathcomp=$pathcomp/ 246bd1da9d7Smrg done 247bd1da9d7Smrg fi 248bd1da9d7Smrg 249bd1da9d7Smrg if test -n "$dir_arg"; then 250bd1da9d7Smrg $doit $mkdircmd "$dst" \ 251bd1da9d7Smrg && { test -z "$chowncmd" || $doit $chowncmd "$dst"; } \ 252bd1da9d7Smrg && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } \ 253bd1da9d7Smrg && { test -z "$stripcmd" || $doit $stripcmd "$dst"; } \ 254bd1da9d7Smrg && { test -z "$chmodcmd" || $doit $chmodcmd "$dst"; } 255bd1da9d7Smrg 256bd1da9d7Smrg else 257bd1da9d7Smrg dstfile=`basename "$dst"` 258bd1da9d7Smrg 259bd1da9d7Smrg # Make a couple of temp file names in the proper directory. 260bd1da9d7Smrg dsttmp=$dstdir/_inst.$$_ 261bd1da9d7Smrg rmtmp=$dstdir/_rm.$$_ 262bd1da9d7Smrg 263bd1da9d7Smrg # Trap to clean up those temp files at exit. 264bd1da9d7Smrg trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0 265bd1da9d7Smrg trap '(exit $?); exit' 1 2 13 15 266bd1da9d7Smrg 267bd1da9d7Smrg # Copy the file name to the temp name. 268bd1da9d7Smrg $doit $cpprog "$src" "$dsttmp" && 269bd1da9d7Smrg 270bd1da9d7Smrg # and set any options; do chmod last to preserve setuid bits. 271bd1da9d7Smrg # 272bd1da9d7Smrg # If any of these fail, we abort the whole thing. If we want to 273bd1da9d7Smrg # ignore errors from any of these, just make sure not to ignore 274bd1da9d7Smrg # errors from the above "$doit $cpprog $src $dsttmp" command. 275bd1da9d7Smrg # 276bd1da9d7Smrg { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } \ 277bd1da9d7Smrg && { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } \ 278bd1da9d7Smrg && { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } \ 279bd1da9d7Smrg && { test -z "$chmodcmd" || $doit $chmodcmd "$dsttmp"; } && 280bd1da9d7Smrg 281bd1da9d7Smrg # Now rename the file to the real destination. 282bd1da9d7Smrg { $doit $mvcmd -f "$dsttmp" "$dstdir/$dstfile" 2>/dev/null \ 283bd1da9d7Smrg || { 284bd1da9d7Smrg # The rename failed, perhaps because mv can't rename something else 285bd1da9d7Smrg # to itself, or perhaps because mv is so ancient that it does not 286bd1da9d7Smrg # support -f. 287bd1da9d7Smrg 288bd1da9d7Smrg # Now remove or move aside any old file at destination location. 289bd1da9d7Smrg # We try this two ways since rm can't unlink itself on some 290bd1da9d7Smrg # systems and the destination file might be busy for other 291bd1da9d7Smrg # reasons. In this case, the final cleanup might fail but the new 292bd1da9d7Smrg # file should still install successfully. 293bd1da9d7Smrg { 294bd1da9d7Smrg if test -f "$dstdir/$dstfile"; then 295bd1da9d7Smrg $doit $rmcmd -f "$dstdir/$dstfile" 2>/dev/null \ 296bd1da9d7Smrg || $doit $mvcmd -f "$dstdir/$dstfile" "$rmtmp" 2>/dev/null \ 297bd1da9d7Smrg || { 298bd1da9d7Smrg echo "$0: cannot unlink or rename $dstdir/$dstfile" >&2 299bd1da9d7Smrg (exit 1); exit 1 300bd1da9d7Smrg } 301bd1da9d7Smrg else 302bd1da9d7Smrg : 303bd1da9d7Smrg fi 304bd1da9d7Smrg } && 305bd1da9d7Smrg 306bd1da9d7Smrg # Now rename the file to the real destination. 307bd1da9d7Smrg $doit $mvcmd "$dsttmp" "$dstdir/$dstfile" 308bd1da9d7Smrg } 309bd1da9d7Smrg } 310bd1da9d7Smrg fi || { (exit 1); exit 1; } 311bd1da9d7Smrgdone 312bd1da9d7Smrg 313bd1da9d7Smrg# The final little trick to "correctly" pass the exit status to the exit trap. 314bd1da9d7Smrg{ 315bd1da9d7Smrg (exit 0); exit 0 316bd1da9d7Smrg} 317bd1da9d7Smrg 318bd1da9d7Smrg# Local variables: 319bd1da9d7Smrg# eval: (add-hook 'write-file-hooks 'time-stamp) 320bd1da9d7Smrg# time-stamp-start: "scriptversion=" 321bd1da9d7Smrg# time-stamp-format: "%:y-%02m-%02d.%02H" 322bd1da9d7Smrg# time-stamp-end: "$" 323bd1da9d7Smrg# End: 324