install-sh revision 4929c8f8
14fb28925Smrg#!/bin/sh
24fb28925Smrg# install - install a program, script, or datafile
3b66d1acbSmrg
44929c8f8Smrgscriptversion=2013-12-25.23; # UTC
5b66d1acbSmrg
64fb28925Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
74fb28925Smrg# later released in X11R6 (xc/config/util/install.sh) with the
84fb28925Smrg# following copyright and license.
94fb28925Smrg#
104fb28925Smrg# Copyright (C) 1994 X Consortium
114fb28925Smrg#
124fb28925Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
134fb28925Smrg# of this software and associated documentation files (the "Software"), to
144fb28925Smrg# deal in the Software without restriction, including without limitation the
154fb28925Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
164fb28925Smrg# sell copies of the Software, and to permit persons to whom the Software is
174fb28925Smrg# furnished to do so, subject to the following conditions:
184fb28925Smrg#
194fb28925Smrg# The above copyright notice and this permission notice shall be included in
204fb28925Smrg# all copies or substantial portions of the Software.
214fb28925Smrg#
224fb28925Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
234fb28925Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
244fb28925Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
254fb28925Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
264fb28925Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
274fb28925Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
284fb28925Smrg#
294fb28925Smrg# Except as contained in this notice, the name of the X Consortium shall not
304fb28925Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
314fb28925Smrg# ings in this Software without prior written authorization from the X Consor-
324fb28925Smrg# tium.
334fb28925Smrg#
344fb28925Smrg#
354fb28925Smrg# FSF changes to this file are in the public domain.
364fb28925Smrg#
374fb28925Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38df58931aSmrg# 'make' implicit rules from creating a file called install from it
394fb28925Smrg# when there is no Makefile.
404fb28925Smrg#
414fb28925Smrg# This script is compatible with the BSD install script, but was written
42b66d1acbSmrg# from scratch.
434fb28925Smrg
444929c8f8Smrgtab='	'
45b66d1acbSmrgnl='
46b66d1acbSmrg'
474929c8f8SmrgIFS=" $tab$nl"
484fb28925Smrg
494929c8f8Smrg# Set DOITPROG to "echo" to test this script.
504fb28925Smrg
51b66d1acbSmrgdoit=${DOITPROG-}
524929c8f8Smrgdoit_exec=${doit:-exec}
534fb28925Smrg
54b66d1acbSmrg# Put in absolute file names if you don't have them in your path;
55b66d1acbSmrg# or use environment vars.
56b66d1acbSmrg
57b66d1acbSmrgchgrpprog=${CHGRPPROG-chgrp}
58b66d1acbSmrgchmodprog=${CHMODPROG-chmod}
59b66d1acbSmrgchownprog=${CHOWNPROG-chown}
60b66d1acbSmrgcmpprog=${CMPPROG-cmp}
61b66d1acbSmrgcpprog=${CPPROG-cp}
62b66d1acbSmrgmkdirprog=${MKDIRPROG-mkdir}
63b66d1acbSmrgmvprog=${MVPROG-mv}
64b66d1acbSmrgrmprog=${RMPROG-rm}
65b66d1acbSmrgstripprog=${STRIPPROG-strip}
66b66d1acbSmrg
67b66d1acbSmrgposix_mkdir=
68b66d1acbSmrg
69b66d1acbSmrg# Desired mode of installed file.
70b66d1acbSmrgmode=0755
71b66d1acbSmrg
72b66d1acbSmrgchgrpcmd=
73b66d1acbSmrgchmodcmd=$chmodprog
74b66d1acbSmrgchowncmd=
75b66d1acbSmrgmvcmd=$mvprog
76b66d1acbSmrgrmcmd="$rmprog -f"
77b66d1acbSmrgstripcmd=
784fb28925Smrg
79b66d1acbSmrgsrc=
80b66d1acbSmrgdst=
81b66d1acbSmrgdir_arg=
82b66d1acbSmrgdst_arg=
834fb28925Smrg
84b66d1acbSmrgcopy_on_change=false
854929c8f8Smrgis_target_a_directory=possibly
864fb28925Smrg
87b66d1acbSmrgusage="\
88b66d1acbSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89b66d1acbSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90b66d1acbSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91b66d1acbSmrg   or: $0 [OPTION]... -d DIRECTORIES...
924fb28925Smrg
93b66d1acbSmrgIn the 1st form, copy SRCFILE to DSTFILE.
94b66d1acbSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95b66d1acbSmrgIn the 4th, create DIRECTORIES.
964fb28925Smrg
97b66d1acbSmrgOptions:
98b66d1acbSmrg     --help     display this help and exit.
99b66d1acbSmrg     --version  display version info and exit.
1004fb28925Smrg
101b66d1acbSmrg  -c            (ignored)
102b66d1acbSmrg  -C            install only if different (preserve the last data modification time)
103b66d1acbSmrg  -d            create directories instead of installing files.
104b66d1acbSmrg  -g GROUP      $chgrpprog installed files to GROUP.
105b66d1acbSmrg  -m MODE       $chmodprog installed files to MODE.
106b66d1acbSmrg  -o USER       $chownprog installed files to USER.
107b66d1acbSmrg  -s            $stripprog installed files.
108b66d1acbSmrg  -t DIRECTORY  install into DIRECTORY.
109b66d1acbSmrg  -T            report an error if DSTFILE is a directory.
1104fb28925Smrg
111b66d1acbSmrgEnvironment variables override the default commands:
112b66d1acbSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
113b66d1acbSmrg  RMPROG STRIPPROG
114b66d1acbSmrg"
1154fb28925Smrg
116b66d1acbSmrgwhile test $# -ne 0; do
117b66d1acbSmrg  case $1 in
118b66d1acbSmrg    -c) ;;
1194fb28925Smrg
120b66d1acbSmrg    -C) copy_on_change=true;;
1214fb28925Smrg
122b66d1acbSmrg    -d) dir_arg=true;;
1234fb28925Smrg
124b66d1acbSmrg    -g) chgrpcmd="$chgrpprog $2"
1254929c8f8Smrg        shift;;
1264fb28925Smrg
127b66d1acbSmrg    --help) echo "$usage"; exit $?;;
1284fb28925Smrg
129b66d1acbSmrg    -m) mode=$2
1304929c8f8Smrg        case $mode in
1314929c8f8Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1324929c8f8Smrg            echo "$0: invalid mode: $mode" >&2
1334929c8f8Smrg            exit 1;;
1344929c8f8Smrg        esac
1354929c8f8Smrg        shift;;
1364fb28925Smrg
137b66d1acbSmrg    -o) chowncmd="$chownprog $2"
1384929c8f8Smrg        shift;;
1394fb28925Smrg
140b66d1acbSmrg    -s) stripcmd=$stripprog;;
1414fb28925Smrg
1424929c8f8Smrg    -t)
1434929c8f8Smrg        is_target_a_directory=always
1444929c8f8Smrg        dst_arg=$2
1454929c8f8Smrg        # Protect names problematic for 'test' and other utilities.
1464929c8f8Smrg        case $dst_arg in
1474929c8f8Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1484929c8f8Smrg        esac
1494929c8f8Smrg        shift;;
1504fb28925Smrg
1514929c8f8Smrg    -T) is_target_a_directory=never;;
1524fb28925Smrg
153b66d1acbSmrg    --version) echo "$0 $scriptversion"; exit $?;;
1544fb28925Smrg
1554929c8f8Smrg    --) shift
1564929c8f8Smrg        break;;
1574fb28925Smrg
1584929c8f8Smrg    -*) echo "$0: invalid option: $1" >&2
1594929c8f8Smrg        exit 1;;
1604fb28925Smrg
161b66d1acbSmrg    *)  break;;
162b66d1acbSmrg  esac
163b66d1acbSmrg  shift
164b66d1acbSmrgdone
1654fb28925Smrg
1664929c8f8Smrg# We allow the use of options -d and -T together, by making -d
1674929c8f8Smrg# take the precedence; this is for compatibility with GNU install.
1684929c8f8Smrg
1694929c8f8Smrgif test -n "$dir_arg"; then
1704929c8f8Smrg  if test -n "$dst_arg"; then
1714929c8f8Smrg    echo "$0: target directory not allowed when installing a directory." >&2
1724929c8f8Smrg    exit 1
1734929c8f8Smrg  fi
1744929c8f8Smrgfi
1754929c8f8Smrg
176b66d1acbSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177b66d1acbSmrg  # When -d is used, all remaining arguments are directories to create.
178b66d1acbSmrg  # When -t is used, the destination is already specified.
179b66d1acbSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180b66d1acbSmrg  for arg
181b66d1acbSmrg  do
182b66d1acbSmrg    if test -n "$dst_arg"; then
183b66d1acbSmrg      # $@ is not empty: it contains at least $arg.
184b66d1acbSmrg      set fnord "$@" "$dst_arg"
185b66d1acbSmrg      shift # fnord
186b66d1acbSmrg    fi
187b66d1acbSmrg    shift # arg
188b66d1acbSmrg    dst_arg=$arg
189df58931aSmrg    # Protect names problematic for 'test' and other utilities.
190df58931aSmrg    case $dst_arg in
191df58931aSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192df58931aSmrg    esac
193b66d1acbSmrg  done
194b66d1acbSmrgfi
1954fb28925Smrg
196b66d1acbSmrgif test $# -eq 0; then
197b66d1acbSmrg  if test -z "$dir_arg"; then
198b66d1acbSmrg    echo "$0: no input file specified." >&2
199b66d1acbSmrg    exit 1
200b66d1acbSmrg  fi
201df58931aSmrg  # It's OK to call 'install-sh -d' without argument.
202b66d1acbSmrg  # This can happen when creating conditional directories.
203b66d1acbSmrg  exit 0
204b66d1acbSmrgfi
2054fb28925Smrg
2064929c8f8Smrgif test -z "$dir_arg"; then
2074929c8f8Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2084929c8f8Smrg    if test ! -d "$dst_arg"; then
2094929c8f8Smrg      echo "$0: $dst_arg: Is not a directory." >&2
2104929c8f8Smrg      exit 1
2114929c8f8Smrg    fi
2124929c8f8Smrg  fi
2134929c8f8Smrgfi
2144929c8f8Smrg
215b66d1acbSmrgif test -z "$dir_arg"; then
216df58931aSmrg  do_exit='(exit $ret); exit $ret'
217df58931aSmrg  trap "ret=129; $do_exit" 1
218df58931aSmrg  trap "ret=130; $do_exit" 2
219df58931aSmrg  trap "ret=141; $do_exit" 13
220df58931aSmrg  trap "ret=143; $do_exit" 15
221b66d1acbSmrg
222b66d1acbSmrg  # Set umask so as not to create temps with too-generous modes.
223b66d1acbSmrg  # However, 'strip' requires both read and write access to temps.
224b66d1acbSmrg  case $mode in
225b66d1acbSmrg    # Optimize common cases.
226b66d1acbSmrg    *644) cp_umask=133;;
227b66d1acbSmrg    *755) cp_umask=22;;
228b66d1acbSmrg
229b66d1acbSmrg    *[0-7])
230b66d1acbSmrg      if test -z "$stripcmd"; then
2314929c8f8Smrg        u_plus_rw=
232b66d1acbSmrg      else
2334929c8f8Smrg        u_plus_rw='% 200'
234b66d1acbSmrg      fi
235b66d1acbSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
236b66d1acbSmrg    *)
237b66d1acbSmrg      if test -z "$stripcmd"; then
2384929c8f8Smrg        u_plus_rw=
239b66d1acbSmrg      else
2404929c8f8Smrg        u_plus_rw=,u+rw
241b66d1acbSmrg      fi
242b66d1acbSmrg      cp_umask=$mode$u_plus_rw;;
243b66d1acbSmrg  esac
244b66d1acbSmrgfi
2454fb28925Smrg
246b66d1acbSmrgfor src
247b66d1acbSmrgdo
248df58931aSmrg  # Protect names problematic for 'test' and other utilities.
249b66d1acbSmrg  case $src in
250df58931aSmrg    -* | [=\(\)!]) src=./$src;;
251b66d1acbSmrg  esac
252b66d1acbSmrg
253b66d1acbSmrg  if test -n "$dir_arg"; then
254b66d1acbSmrg    dst=$src
255b66d1acbSmrg    dstdir=$dst
256b66d1acbSmrg    test -d "$dstdir"
257b66d1acbSmrg    dstdir_status=$?
258b66d1acbSmrg  else
259b66d1acbSmrg
260b66d1acbSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
261b66d1acbSmrg    # might cause directories to be created, which would be especially bad
262b66d1acbSmrg    # if $src (and thus $dsttmp) contains '*'.
263b66d1acbSmrg    if test ! -f "$src" && test ! -d "$src"; then
264b66d1acbSmrg      echo "$0: $src does not exist." >&2
265b66d1acbSmrg      exit 1
266b66d1acbSmrg    fi
267b66d1acbSmrg
268b66d1acbSmrg    if test -z "$dst_arg"; then
269b66d1acbSmrg      echo "$0: no destination specified." >&2
270b66d1acbSmrg      exit 1
271b66d1acbSmrg    fi
272b66d1acbSmrg    dst=$dst_arg
2734fb28925Smrg
274b66d1acbSmrg    # If destination is a directory, append the input filename; won't work
275b66d1acbSmrg    # if double slashes aren't ignored.
276b66d1acbSmrg    if test -d "$dst"; then
2774929c8f8Smrg      if test "$is_target_a_directory" = never; then
2784929c8f8Smrg        echo "$0: $dst_arg: Is a directory" >&2
2794929c8f8Smrg        exit 1
280b66d1acbSmrg      fi
281b66d1acbSmrg      dstdir=$dst
282b66d1acbSmrg      dst=$dstdir/`basename "$src"`
283b66d1acbSmrg      dstdir_status=0
284b66d1acbSmrg    else
2854929c8f8Smrg      dstdir=`dirname "$dst"`
286b66d1acbSmrg      test -d "$dstdir"
287b66d1acbSmrg      dstdir_status=$?
288b66d1acbSmrg    fi
289b66d1acbSmrg  fi
290b66d1acbSmrg
291b66d1acbSmrg  obsolete_mkdir_used=false
292b66d1acbSmrg
293b66d1acbSmrg  if test $dstdir_status != 0; then
294b66d1acbSmrg    case $posix_mkdir in
295b66d1acbSmrg      '')
2964929c8f8Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
2974929c8f8Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
2984929c8f8Smrg        umask=`umask`
2994929c8f8Smrg        case $stripcmd.$umask in
3004929c8f8Smrg          # Optimize common cases.
3014929c8f8Smrg          *[2367][2367]) mkdir_umask=$umask;;
3024929c8f8Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3034929c8f8Smrg
3044929c8f8Smrg          *[0-7])
3054929c8f8Smrg            mkdir_umask=`expr $umask + 22 \
3064929c8f8Smrg              - $umask % 100 % 40 + $umask % 20 \
3074929c8f8Smrg              - $umask % 10 % 4 + $umask % 2
3084929c8f8Smrg            `;;
3094929c8f8Smrg          *) mkdir_umask=$umask,go-w;;
3104929c8f8Smrg        esac
3114929c8f8Smrg
3124929c8f8Smrg        # With -d, create the new directory with the user-specified mode.
3134929c8f8Smrg        # Otherwise, rely on $mkdir_umask.
3144929c8f8Smrg        if test -n "$dir_arg"; then
3154929c8f8Smrg          mkdir_mode=-m$mode
3164929c8f8Smrg        else
3174929c8f8Smrg          mkdir_mode=
3184929c8f8Smrg        fi
3194929c8f8Smrg
3204929c8f8Smrg        posix_mkdir=false
3214929c8f8Smrg        case $umask in
3224929c8f8Smrg          *[123567][0-7][0-7])
3234929c8f8Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
3244929c8f8Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3254929c8f8Smrg            ;;
3264929c8f8Smrg          *)
3274929c8f8Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3284929c8f8Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3294929c8f8Smrg
3304929c8f8Smrg            if (umask $mkdir_umask &&
3314929c8f8Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3324929c8f8Smrg            then
3334929c8f8Smrg              if test -z "$dir_arg" || {
3344929c8f8Smrg                   # Check for POSIX incompatibilities with -m.
3354929c8f8Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3364929c8f8Smrg                   # other-writable bit of parent directory when it shouldn't.
3374929c8f8Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3384929c8f8Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3394929c8f8Smrg                   case $ls_ld_tmpdir in
3404929c8f8Smrg                     d????-?r-*) different_mode=700;;
3414929c8f8Smrg                     d????-?--*) different_mode=755;;
3424929c8f8Smrg                     *) false;;
3434929c8f8Smrg                   esac &&
3444929c8f8Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3454929c8f8Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3464929c8f8Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3474929c8f8Smrg                   }
3484929c8f8Smrg                 }
3494929c8f8Smrg              then posix_mkdir=:
3504929c8f8Smrg              fi
3514929c8f8Smrg              rmdir "$tmpdir/d" "$tmpdir"
3524929c8f8Smrg            else
3534929c8f8Smrg              # Remove any dirs left behind by ancient mkdir implementations.
3544929c8f8Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3554929c8f8Smrg            fi
3564929c8f8Smrg            trap '' 0;;
3574929c8f8Smrg        esac;;
358b66d1acbSmrg    esac
3594fb28925Smrg
360b66d1acbSmrg    if
361b66d1acbSmrg      $posix_mkdir && (
3624929c8f8Smrg        umask $mkdir_umask &&
3634929c8f8Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
364b66d1acbSmrg      )
365b66d1acbSmrg    then :
366b66d1acbSmrg    else
367b66d1acbSmrg
368b66d1acbSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
369b66d1acbSmrg      # or it failed possibly due to a race condition.  Create the
370b66d1acbSmrg      # directory the slow way, step by step, checking for races as we go.
371b66d1acbSmrg
372b66d1acbSmrg      case $dstdir in
3734929c8f8Smrg        /*) prefix='/';;
3744929c8f8Smrg        [-=\(\)!]*) prefix='./';;
3754929c8f8Smrg        *)  prefix='';;
376b66d1acbSmrg      esac
377b66d1acbSmrg
378b66d1acbSmrg      oIFS=$IFS
379b66d1acbSmrg      IFS=/
3804929c8f8Smrg      set -f
381b66d1acbSmrg      set fnord $dstdir
382b66d1acbSmrg      shift
3834929c8f8Smrg      set +f
384b66d1acbSmrg      IFS=$oIFS
385b66d1acbSmrg
386b66d1acbSmrg      prefixes=
387b66d1acbSmrg
388b66d1acbSmrg      for d
389b66d1acbSmrg      do
3904929c8f8Smrg        test X"$d" = X && continue
3914929c8f8Smrg
3924929c8f8Smrg        prefix=$prefix$d
3934929c8f8Smrg        if test -d "$prefix"; then
3944929c8f8Smrg          prefixes=
3954929c8f8Smrg        else
3964929c8f8Smrg          if $posix_mkdir; then
3974929c8f8Smrg            (umask=$mkdir_umask &&
3984929c8f8Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
3994929c8f8Smrg            # Don't fail if two instances are running concurrently.
4004929c8f8Smrg            test -d "$prefix" || exit 1
4014929c8f8Smrg          else
4024929c8f8Smrg            case $prefix in
4034929c8f8Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4044929c8f8Smrg              *) qprefix=$prefix;;
4054929c8f8Smrg            esac
4064929c8f8Smrg            prefixes="$prefixes '$qprefix'"
4074929c8f8Smrg          fi
4084929c8f8Smrg        fi
4094929c8f8Smrg        prefix=$prefix/
410b66d1acbSmrg      done
411b66d1acbSmrg
412b66d1acbSmrg      if test -n "$prefixes"; then
4134929c8f8Smrg        # Don't fail if two instances are running concurrently.
4144929c8f8Smrg        (umask $mkdir_umask &&
4154929c8f8Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4164929c8f8Smrg          test -d "$dstdir" || exit 1
4174929c8f8Smrg        obsolete_mkdir_used=true
418b66d1acbSmrg      fi
419b66d1acbSmrg    fi
420b66d1acbSmrg  fi
421b66d1acbSmrg
422b66d1acbSmrg  if test -n "$dir_arg"; then
423b66d1acbSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
424b66d1acbSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
425b66d1acbSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
426b66d1acbSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
427b66d1acbSmrg  else
428b66d1acbSmrg
429b66d1acbSmrg    # Make a couple of temp file names in the proper directory.
430b66d1acbSmrg    dsttmp=$dstdir/_inst.$$_
431b66d1acbSmrg    rmtmp=$dstdir/_rm.$$_
432b66d1acbSmrg
433b66d1acbSmrg    # Trap to clean up those temp files at exit.
434b66d1acbSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
435b66d1acbSmrg
436b66d1acbSmrg    # Copy the file name to the temp name.
437b66d1acbSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
438b66d1acbSmrg
439b66d1acbSmrg    # and set any options; do chmod last to preserve setuid bits.
440b66d1acbSmrg    #
441b66d1acbSmrg    # If any of these fail, we abort the whole thing.  If we want to
442b66d1acbSmrg    # ignore errors from any of these, just make sure not to ignore
443b66d1acbSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
444b66d1acbSmrg    #
445b66d1acbSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
446b66d1acbSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
447b66d1acbSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
448b66d1acbSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
449b66d1acbSmrg
450b66d1acbSmrg    # If -C, don't bother to copy if it wouldn't change the file.
451b66d1acbSmrg    if $copy_on_change &&
4524929c8f8Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4534929c8f8Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4544929c8f8Smrg       set -f &&
455b66d1acbSmrg       set X $old && old=:$2:$4:$5:$6 &&
456b66d1acbSmrg       set X $new && new=:$2:$4:$5:$6 &&
4574929c8f8Smrg       set +f &&
458b66d1acbSmrg       test "$old" = "$new" &&
459b66d1acbSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
460b66d1acbSmrg    then
461b66d1acbSmrg      rm -f "$dsttmp"
462b66d1acbSmrg    else
463b66d1acbSmrg      # Rename the file to the real destination.
464b66d1acbSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
465b66d1acbSmrg
466b66d1acbSmrg      # The rename failed, perhaps because mv can't rename something else
467b66d1acbSmrg      # to itself, or perhaps because mv is so ancient that it does not
468b66d1acbSmrg      # support -f.
469b66d1acbSmrg      {
4704929c8f8Smrg        # Now remove or move aside any old file at destination location.
4714929c8f8Smrg        # We try this two ways since rm can't unlink itself on some
4724929c8f8Smrg        # systems and the destination file might be busy for other
4734929c8f8Smrg        # reasons.  In this case, the final cleanup might fail but the new
4744929c8f8Smrg        # file should still install successfully.
4754929c8f8Smrg        {
4764929c8f8Smrg          test ! -f "$dst" ||
4774929c8f8Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
4784929c8f8Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4794929c8f8Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4804929c8f8Smrg          } ||
4814929c8f8Smrg          { echo "$0: cannot unlink or rename $dst" >&2
4824929c8f8Smrg            (exit 1); exit 1
4834929c8f8Smrg          }
4844929c8f8Smrg        } &&
4854929c8f8Smrg
4864929c8f8Smrg        # Now rename the file to the real destination.
4874929c8f8Smrg        $doit $mvcmd "$dsttmp" "$dst"
488b66d1acbSmrg      }
489b66d1acbSmrg    fi || exit 1
490b66d1acbSmrg
491b66d1acbSmrg    trap '' 0
492b66d1acbSmrg  fi
493b66d1acbSmrgdone
4944fb28925Smrg
495b66d1acbSmrg# Local variables:
496b66d1acbSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
497b66d1acbSmrg# time-stamp-start: "scriptversion="
498b66d1acbSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
499b66d1acbSmrg# time-stamp-time-zone: "UTC"
500b66d1acbSmrg# time-stamp-end: "; # UTC"
501b66d1acbSmrg# End:
502