install-sh revision 8a954913
1bb2e14f3Smrg#!/bin/sh
2bb2e14f3Smrg# install - install a program, script, or datafile
3bb2e14f3Smrg
48a954913Smrgscriptversion=2013-12-25.23; # UTC
5bb2e14f3Smrg
6bb2e14f3Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7bb2e14f3Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8bb2e14f3Smrg# following copyright and license.
9bb2e14f3Smrg#
10bb2e14f3Smrg# Copyright (C) 1994 X Consortium
11bb2e14f3Smrg#
12bb2e14f3Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13bb2e14f3Smrg# of this software and associated documentation files (the "Software"), to
14bb2e14f3Smrg# deal in the Software without restriction, including without limitation the
15bb2e14f3Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16bb2e14f3Smrg# sell copies of the Software, and to permit persons to whom the Software is
17bb2e14f3Smrg# furnished to do so, subject to the following conditions:
18bb2e14f3Smrg#
19bb2e14f3Smrg# The above copyright notice and this permission notice shall be included in
20bb2e14f3Smrg# all copies or substantial portions of the Software.
21bb2e14f3Smrg#
22bb2e14f3Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23bb2e14f3Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24bb2e14f3Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25bb2e14f3Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26bb2e14f3Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27bb2e14f3Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28bb2e14f3Smrg#
29bb2e14f3Smrg# Except as contained in this notice, the name of the X Consortium shall not
30bb2e14f3Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31bb2e14f3Smrg# ings in this Software without prior written authorization from the X Consor-
32bb2e14f3Smrg# tium.
33bb2e14f3Smrg#
34bb2e14f3Smrg#
35bb2e14f3Smrg# FSF changes to this file are in the public domain.
36bb2e14f3Smrg#
37bb2e14f3Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3839f9c979Smrg# 'make' implicit rules from creating a file called install from it
39bb2e14f3Smrg# when there is no Makefile.
40bb2e14f3Smrg#
41bb2e14f3Smrg# This script is compatible with the BSD install script, but was written
42a5399cb1Smrg# from scratch.
43a5399cb1Smrg
448a954913Smrgtab='	'
45a5399cb1Smrgnl='
46a5399cb1Smrg'
478a954913SmrgIFS=" $tab$nl"
48bb2e14f3Smrg
498a954913Smrg# Set DOITPROG to "echo" to test this script.
50bb2e14f3Smrg
51a5399cb1Smrgdoit=${DOITPROG-}
528a954913Smrgdoit_exec=${doit:-exec}
53bb2e14f3Smrg
54a5399cb1Smrg# Put in absolute file names if you don't have them in your path;
55a5399cb1Smrg# or use environment vars.
56a5399cb1Smrg
57a5399cb1Smrgchgrpprog=${CHGRPPROG-chgrp}
58a5399cb1Smrgchmodprog=${CHMODPROG-chmod}
59a5399cb1Smrgchownprog=${CHOWNPROG-chown}
60a5399cb1Smrgcmpprog=${CMPPROG-cmp}
61a5399cb1Smrgcpprog=${CPPROG-cp}
62a5399cb1Smrgmkdirprog=${MKDIRPROG-mkdir}
63a5399cb1Smrgmvprog=${MVPROG-mv}
64a5399cb1Smrgrmprog=${RMPROG-rm}
65a5399cb1Smrgstripprog=${STRIPPROG-strip}
66a5399cb1Smrg
67a5399cb1Smrgposix_mkdir=
68a5399cb1Smrg
69a5399cb1Smrg# Desired mode of installed file.
70a5399cb1Smrgmode=0755
71bb2e14f3Smrg
72bb2e14f3Smrgchgrpcmd=
73a5399cb1Smrgchmodcmd=$chmodprog
74a5399cb1Smrgchowncmd=
75a5399cb1Smrgmvcmd=$mvprog
76bb2e14f3Smrgrmcmd="$rmprog -f"
77a5399cb1Smrgstripcmd=
78a5399cb1Smrg
79bb2e14f3Smrgsrc=
80bb2e14f3Smrgdst=
81bb2e14f3Smrgdir_arg=
82a5399cb1Smrgdst_arg=
83a5399cb1Smrg
84a5399cb1Smrgcopy_on_change=false
858a954913Smrgis_target_a_directory=possibly
86bb2e14f3Smrg
87a5399cb1Smrgusage="\
88a5399cb1SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89bb2e14f3Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90bb2e14f3Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91bb2e14f3Smrg   or: $0 [OPTION]... -d DIRECTORIES...
92bb2e14f3Smrg
93bb2e14f3SmrgIn the 1st form, copy SRCFILE to DSTFILE.
94bb2e14f3SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95bb2e14f3SmrgIn the 4th, create DIRECTORIES.
96bb2e14f3Smrg
97bb2e14f3SmrgOptions:
98a5399cb1Smrg     --help     display this help and exit.
99a5399cb1Smrg     --version  display version info and exit.
100a5399cb1Smrg
101a5399cb1Smrg  -c            (ignored)
102a5399cb1Smrg  -C            install only if different (preserve the last data modification time)
103a5399cb1Smrg  -d            create directories instead of installing files.
104a5399cb1Smrg  -g GROUP      $chgrpprog installed files to GROUP.
105a5399cb1Smrg  -m MODE       $chmodprog installed files to MODE.
106a5399cb1Smrg  -o USER       $chownprog installed files to USER.
107a5399cb1Smrg  -s            $stripprog installed files.
108a5399cb1Smrg  -t DIRECTORY  install into DIRECTORY.
109a5399cb1Smrg  -T            report an error if DSTFILE is a directory.
110bb2e14f3Smrg
111bb2e14f3SmrgEnvironment variables override the default commands:
112a5399cb1Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
113a5399cb1Smrg  RMPROG STRIPPROG
114bb2e14f3Smrg"
115bb2e14f3Smrg
116a5399cb1Smrgwhile test $# -ne 0; do
117bb2e14f3Smrg  case $1 in
118a5399cb1Smrg    -c) ;;
119a5399cb1Smrg
120a5399cb1Smrg    -C) copy_on_change=true;;
121bb2e14f3Smrg
122a5399cb1Smrg    -d) dir_arg=true;;
123bb2e14f3Smrg
124bb2e14f3Smrg    -g) chgrpcmd="$chgrpprog $2"
1258a954913Smrg        shift;;
126bb2e14f3Smrg
127bb2e14f3Smrg    --help) echo "$usage"; exit $?;;
128bb2e14f3Smrg
129a5399cb1Smrg    -m) mode=$2
1308a954913Smrg        case $mode in
1318a954913Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1328a954913Smrg            echo "$0: invalid mode: $mode" >&2
1338a954913Smrg            exit 1;;
1348a954913Smrg        esac
1358a954913Smrg        shift;;
136bb2e14f3Smrg
137bb2e14f3Smrg    -o) chowncmd="$chownprog $2"
1388a954913Smrg        shift;;
139bb2e14f3Smrg
140a5399cb1Smrg    -s) stripcmd=$stripprog;;
141bb2e14f3Smrg
1428a954913Smrg    -t)
1438a954913Smrg        is_target_a_directory=always
1448a954913Smrg        dst_arg=$2
1458a954913Smrg        # Protect names problematic for 'test' and other utilities.
1468a954913Smrg        case $dst_arg in
1478a954913Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1488a954913Smrg        esac
1498a954913Smrg        shift;;
150bb2e14f3Smrg
1518a954913Smrg    -T) is_target_a_directory=never;;
152bb2e14f3Smrg
153bb2e14f3Smrg    --version) echo "$0 $scriptversion"; exit $?;;
154bb2e14f3Smrg
1558a954913Smrg    --) shift
1568a954913Smrg        break;;
157a5399cb1Smrg
1588a954913Smrg    -*) echo "$0: invalid option: $1" >&2
1598a954913Smrg        exit 1;;
160a5399cb1Smrg
161a5399cb1Smrg    *)  break;;
162bb2e14f3Smrg  esac
163a5399cb1Smrg  shift
164bb2e14f3Smrgdone
165bb2e14f3Smrg
1668a954913Smrg# We allow the use of options -d and -T together, by making -d
1678a954913Smrg# take the precedence; this is for compatibility with GNU install.
1688a954913Smrg
1698a954913Smrgif test -n "$dir_arg"; then
1708a954913Smrg  if test -n "$dst_arg"; then
1718a954913Smrg    echo "$0: target directory not allowed when installing a directory." >&2
1728a954913Smrg    exit 1
1738a954913Smrg  fi
1748a954913Smrgfi
1758a954913Smrg
176a5399cb1Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177a5399cb1Smrg  # When -d is used, all remaining arguments are directories to create.
178a5399cb1Smrg  # When -t is used, the destination is already specified.
179a5399cb1Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180a5399cb1Smrg  for arg
181a5399cb1Smrg  do
182a5399cb1Smrg    if test -n "$dst_arg"; then
183a5399cb1Smrg      # $@ is not empty: it contains at least $arg.
184a5399cb1Smrg      set fnord "$@" "$dst_arg"
185a5399cb1Smrg      shift # fnord
186a5399cb1Smrg    fi
187a5399cb1Smrg    shift # arg
188a5399cb1Smrg    dst_arg=$arg
18939f9c979Smrg    # Protect names problematic for 'test' and other utilities.
19039f9c979Smrg    case $dst_arg in
19139f9c979Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
19239f9c979Smrg    esac
193a5399cb1Smrg  done
194a5399cb1Smrgfi
195a5399cb1Smrg
196a5399cb1Smrgif test $# -eq 0; then
197bb2e14f3Smrg  if test -z "$dir_arg"; then
198bb2e14f3Smrg    echo "$0: no input file specified." >&2
199bb2e14f3Smrg    exit 1
200bb2e14f3Smrg  fi
20139f9c979Smrg  # It's OK to call 'install-sh -d' without argument.
202bb2e14f3Smrg  # This can happen when creating conditional directories.
203bb2e14f3Smrg  exit 0
204bb2e14f3Smrgfi
205bb2e14f3Smrg
2068a954913Smrgif test -z "$dir_arg"; then
2078a954913Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2088a954913Smrg    if test ! -d "$dst_arg"; then
2098a954913Smrg      echo "$0: $dst_arg: Is not a directory." >&2
2108a954913Smrg      exit 1
2118a954913Smrg    fi
2128a954913Smrg  fi
2138a954913Smrgfi
2148a954913Smrg
215a5399cb1Smrgif test -z "$dir_arg"; then
21639f9c979Smrg  do_exit='(exit $ret); exit $ret'
21739f9c979Smrg  trap "ret=129; $do_exit" 1
21839f9c979Smrg  trap "ret=130; $do_exit" 2
21939f9c979Smrg  trap "ret=141; $do_exit" 13
22039f9c979Smrg  trap "ret=143; $do_exit" 15
221a5399cb1Smrg
222a5399cb1Smrg  # Set umask so as not to create temps with too-generous modes.
223a5399cb1Smrg  # However, 'strip' requires both read and write access to temps.
224a5399cb1Smrg  case $mode in
225a5399cb1Smrg    # Optimize common cases.
226a5399cb1Smrg    *644) cp_umask=133;;
227a5399cb1Smrg    *755) cp_umask=22;;
228a5399cb1Smrg
229a5399cb1Smrg    *[0-7])
230a5399cb1Smrg      if test -z "$stripcmd"; then
2318a954913Smrg        u_plus_rw=
232a5399cb1Smrg      else
2338a954913Smrg        u_plus_rw='% 200'
234a5399cb1Smrg      fi
235a5399cb1Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
236a5399cb1Smrg    *)
237a5399cb1Smrg      if test -z "$stripcmd"; then
2388a954913Smrg        u_plus_rw=
239a5399cb1Smrg      else
2408a954913Smrg        u_plus_rw=,u+rw
241a5399cb1Smrg      fi
242a5399cb1Smrg      cp_umask=$mode$u_plus_rw;;
243a5399cb1Smrg  esac
244a5399cb1Smrgfi
245a5399cb1Smrg
246bb2e14f3Smrgfor src
247bb2e14f3Smrgdo
24839f9c979Smrg  # Protect names problematic for 'test' and other utilities.
249bb2e14f3Smrg  case $src in
25039f9c979Smrg    -* | [=\(\)!]) src=./$src;;
251bb2e14f3Smrg  esac
252bb2e14f3Smrg
253bb2e14f3Smrg  if test -n "$dir_arg"; then
254bb2e14f3Smrg    dst=$src
255a5399cb1Smrg    dstdir=$dst
256a5399cb1Smrg    test -d "$dstdir"
257a5399cb1Smrg    dstdir_status=$?
258bb2e14f3Smrg  else
259a5399cb1Smrg
260bb2e14f3Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
261bb2e14f3Smrg    # might cause directories to be created, which would be especially bad
262bb2e14f3Smrg    # if $src (and thus $dsttmp) contains '*'.
263bb2e14f3Smrg    if test ! -f "$src" && test ! -d "$src"; then
264bb2e14f3Smrg      echo "$0: $src does not exist." >&2
265bb2e14f3Smrg      exit 1
266bb2e14f3Smrg    fi
267bb2e14f3Smrg
268a5399cb1Smrg    if test -z "$dst_arg"; then
269bb2e14f3Smrg      echo "$0: no destination specified." >&2
270bb2e14f3Smrg      exit 1
271bb2e14f3Smrg    fi
272a5399cb1Smrg    dst=$dst_arg
273bb2e14f3Smrg
274bb2e14f3Smrg    # If destination is a directory, append the input filename; won't work
275bb2e14f3Smrg    # if double slashes aren't ignored.
276bb2e14f3Smrg    if test -d "$dst"; then
2778a954913Smrg      if test "$is_target_a_directory" = never; then
2788a954913Smrg        echo "$0: $dst_arg: Is a directory" >&2
2798a954913Smrg        exit 1
280bb2e14f3Smrg      fi
281a5399cb1Smrg      dstdir=$dst
282a5399cb1Smrg      dst=$dstdir/`basename "$src"`
283a5399cb1Smrg      dstdir_status=0
284a5399cb1Smrg    else
2858a954913Smrg      dstdir=`dirname "$dst"`
286a5399cb1Smrg      test -d "$dstdir"
287a5399cb1Smrg      dstdir_status=$?
288bb2e14f3Smrg    fi
289bb2e14f3Smrg  fi
290bb2e14f3Smrg
291a5399cb1Smrg  obsolete_mkdir_used=false
292a5399cb1Smrg
293a5399cb1Smrg  if test $dstdir_status != 0; then
294a5399cb1Smrg    case $posix_mkdir in
295a5399cb1Smrg      '')
2968a954913Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
2978a954913Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
2988a954913Smrg        umask=`umask`
2998a954913Smrg        case $stripcmd.$umask in
3008a954913Smrg          # Optimize common cases.
3018a954913Smrg          *[2367][2367]) mkdir_umask=$umask;;
3028a954913Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3038a954913Smrg
3048a954913Smrg          *[0-7])
3058a954913Smrg            mkdir_umask=`expr $umask + 22 \
3068a954913Smrg              - $umask % 100 % 40 + $umask % 20 \
3078a954913Smrg              - $umask % 10 % 4 + $umask % 2
3088a954913Smrg            `;;
3098a954913Smrg          *) mkdir_umask=$umask,go-w;;
3108a954913Smrg        esac
3118a954913Smrg
3128a954913Smrg        # With -d, create the new directory with the user-specified mode.
3138a954913Smrg        # Otherwise, rely on $mkdir_umask.
3148a954913Smrg        if test -n "$dir_arg"; then
3158a954913Smrg          mkdir_mode=-m$mode
3168a954913Smrg        else
3178a954913Smrg          mkdir_mode=
3188a954913Smrg        fi
3198a954913Smrg
3208a954913Smrg        posix_mkdir=false
3218a954913Smrg        case $umask in
3228a954913Smrg          *[123567][0-7][0-7])
3238a954913Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
3248a954913Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3258a954913Smrg            ;;
3268a954913Smrg          *)
3278a954913Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3288a954913Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
3298a954913Smrg
3308a954913Smrg            if (umask $mkdir_umask &&
3318a954913Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
3328a954913Smrg            then
3338a954913Smrg              if test -z "$dir_arg" || {
3348a954913Smrg                   # Check for POSIX incompatibilities with -m.
3358a954913Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3368a954913Smrg                   # other-writable bit of parent directory when it shouldn't.
3378a954913Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3388a954913Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
3398a954913Smrg                   case $ls_ld_tmpdir in
3408a954913Smrg                     d????-?r-*) different_mode=700;;
3418a954913Smrg                     d????-?--*) different_mode=755;;
3428a954913Smrg                     *) false;;
3438a954913Smrg                   esac &&
3448a954913Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
3458a954913Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
3468a954913Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3478a954913Smrg                   }
3488a954913Smrg                 }
3498a954913Smrg              then posix_mkdir=:
3508a954913Smrg              fi
3518a954913Smrg              rmdir "$tmpdir/d" "$tmpdir"
3528a954913Smrg            else
3538a954913Smrg              # Remove any dirs left behind by ancient mkdir implementations.
3548a954913Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
3558a954913Smrg            fi
3568a954913Smrg            trap '' 0;;
3578a954913Smrg        esac;;
358a5399cb1Smrg    esac
359bb2e14f3Smrg
360a5399cb1Smrg    if
361a5399cb1Smrg      $posix_mkdir && (
3628a954913Smrg        umask $mkdir_umask &&
3638a954913Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
364a5399cb1Smrg      )
365a5399cb1Smrg    then :
366a5399cb1Smrg    else
367bb2e14f3Smrg
368a5399cb1Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
369a5399cb1Smrg      # or it failed possibly due to a race condition.  Create the
370a5399cb1Smrg      # directory the slow way, step by step, checking for races as we go.
371bb2e14f3Smrg
372a5399cb1Smrg      case $dstdir in
3738a954913Smrg        /*) prefix='/';;
3748a954913Smrg        [-=\(\)!]*) prefix='./';;
3758a954913Smrg        *)  prefix='';;
376a5399cb1Smrg      esac
377bb2e14f3Smrg
378a5399cb1Smrg      oIFS=$IFS
379a5399cb1Smrg      IFS=/
3808a954913Smrg      set -f
381a5399cb1Smrg      set fnord $dstdir
382bb2e14f3Smrg      shift
3838a954913Smrg      set +f
384a5399cb1Smrg      IFS=$oIFS
385a5399cb1Smrg
386a5399cb1Smrg      prefixes=
387a5399cb1Smrg
388a5399cb1Smrg      for d
389a5399cb1Smrg      do
3908a954913Smrg        test X"$d" = X && continue
3918a954913Smrg
3928a954913Smrg        prefix=$prefix$d
3938a954913Smrg        if test -d "$prefix"; then
3948a954913Smrg          prefixes=
3958a954913Smrg        else
3968a954913Smrg          if $posix_mkdir; then
3978a954913Smrg            (umask=$mkdir_umask &&
3988a954913Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
3998a954913Smrg            # Don't fail if two instances are running concurrently.
4008a954913Smrg            test -d "$prefix" || exit 1
4018a954913Smrg          else
4028a954913Smrg            case $prefix in
4038a954913Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4048a954913Smrg              *) qprefix=$prefix;;
4058a954913Smrg            esac
4068a954913Smrg            prefixes="$prefixes '$qprefix'"
4078a954913Smrg          fi
4088a954913Smrg        fi
4098a954913Smrg        prefix=$prefix/
410a5399cb1Smrg      done
411a5399cb1Smrg
412a5399cb1Smrg      if test -n "$prefixes"; then
4138a954913Smrg        # Don't fail if two instances are running concurrently.
4148a954913Smrg        (umask $mkdir_umask &&
4158a954913Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4168a954913Smrg          test -d "$dstdir" || exit 1
4178a954913Smrg        obsolete_mkdir_used=true
418bb2e14f3Smrg      fi
419a5399cb1Smrg    fi
420bb2e14f3Smrg  fi
421bb2e14f3Smrg
422bb2e14f3Smrg  if test -n "$dir_arg"; then
423a5399cb1Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
424a5399cb1Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
425a5399cb1Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
426a5399cb1Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
427bb2e14f3Smrg  else
428bb2e14f3Smrg
429bb2e14f3Smrg    # Make a couple of temp file names in the proper directory.
430bb2e14f3Smrg    dsttmp=$dstdir/_inst.$$_
431bb2e14f3Smrg    rmtmp=$dstdir/_rm.$$_
432bb2e14f3Smrg
433bb2e14f3Smrg    # Trap to clean up those temp files at exit.
434bb2e14f3Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
435bb2e14f3Smrg
436bb2e14f3Smrg    # Copy the file name to the temp name.
437a5399cb1Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
438bb2e14f3Smrg
439bb2e14f3Smrg    # and set any options; do chmod last to preserve setuid bits.
440bb2e14f3Smrg    #
441bb2e14f3Smrg    # If any of these fail, we abort the whole thing.  If we want to
442bb2e14f3Smrg    # ignore errors from any of these, just make sure not to ignore
443bb2e14f3Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
444bb2e14f3Smrg    #
445a5399cb1Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
446a5399cb1Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
447a5399cb1Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
448a5399cb1Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
449a5399cb1Smrg
450a5399cb1Smrg    # If -C, don't bother to copy if it wouldn't change the file.
451a5399cb1Smrg    if $copy_on_change &&
4528a954913Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4538a954913Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4548a954913Smrg       set -f &&
455a5399cb1Smrg       set X $old && old=:$2:$4:$5:$6 &&
456a5399cb1Smrg       set X $new && new=:$2:$4:$5:$6 &&
4578a954913Smrg       set +f &&
458a5399cb1Smrg       test "$old" = "$new" &&
459a5399cb1Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
460a5399cb1Smrg    then
461a5399cb1Smrg      rm -f "$dsttmp"
462a5399cb1Smrg    else
463a5399cb1Smrg      # Rename the file to the real destination.
464a5399cb1Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
465a5399cb1Smrg
466a5399cb1Smrg      # The rename failed, perhaps because mv can't rename something else
467a5399cb1Smrg      # to itself, or perhaps because mv is so ancient that it does not
468a5399cb1Smrg      # support -f.
469a5399cb1Smrg      {
4708a954913Smrg        # Now remove or move aside any old file at destination location.
4718a954913Smrg        # We try this two ways since rm can't unlink itself on some
4728a954913Smrg        # systems and the destination file might be busy for other
4738a954913Smrg        # reasons.  In this case, the final cleanup might fail but the new
4748a954913Smrg        # file should still install successfully.
4758a954913Smrg        {
4768a954913Smrg          test ! -f "$dst" ||
4778a954913Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
4788a954913Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4798a954913Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4808a954913Smrg          } ||
4818a954913Smrg          { echo "$0: cannot unlink or rename $dst" >&2
4828a954913Smrg            (exit 1); exit 1
4838a954913Smrg          }
4848a954913Smrg        } &&
4858a954913Smrg
4868a954913Smrg        # Now rename the file to the real destination.
4878a954913Smrg        $doit $mvcmd "$dsttmp" "$dst"
488a5399cb1Smrg      }
489a5399cb1Smrg    fi || exit 1
490a5399cb1Smrg
491a5399cb1Smrg    trap '' 0
492a5399cb1Smrg  fi
493bb2e14f3Smrgdone
494bb2e14f3Smrg
495bb2e14f3Smrg# Local variables:
496bb2e14f3Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
497bb2e14f3Smrg# time-stamp-start: "scriptversion="
498bb2e14f3Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
499a5399cb1Smrg# time-stamp-time-zone: "UTC"
500a5399cb1Smrg# time-stamp-end: "; # UTC"
501bb2e14f3Smrg# End:
502