install-sh revision 2f60eb46
1692f60a7Smrg#!/bin/sh
2692f60a7Smrg# install - install a program, script, or datafile
3692f60a7Smrg
42f60eb46Smrgscriptversion=2014-09-12.12; # UTC
5692f60a7Smrg
6692f60a7Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7692f60a7Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8692f60a7Smrg# following copyright and license.
9692f60a7Smrg#
10692f60a7Smrg# Copyright (C) 1994 X Consortium
11692f60a7Smrg#
12692f60a7Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13692f60a7Smrg# of this software and associated documentation files (the "Software"), to
14692f60a7Smrg# deal in the Software without restriction, including without limitation the
15692f60a7Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16692f60a7Smrg# sell copies of the Software, and to permit persons to whom the Software is
17692f60a7Smrg# furnished to do so, subject to the following conditions:
18692f60a7Smrg#
19692f60a7Smrg# The above copyright notice and this permission notice shall be included in
20692f60a7Smrg# all copies or substantial portions of the Software.
21692f60a7Smrg#
22692f60a7Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23692f60a7Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24692f60a7Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25692f60a7Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26692f60a7Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27692f60a7Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28692f60a7Smrg#
29692f60a7Smrg# Except as contained in this notice, the name of the X Consortium shall not
30692f60a7Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31692f60a7Smrg# ings in this Software without prior written authorization from the X Consor-
32692f60a7Smrg# tium.
33692f60a7Smrg#
34692f60a7Smrg#
35692f60a7Smrg# FSF changes to this file are in the public domain.
36692f60a7Smrg#
37692f60a7Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38eaa3dbe0Smrg# 'make' implicit rules from creating a file called install from it
39692f60a7Smrg# when there is no Makefile.
40692f60a7Smrg#
41692f60a7Smrg# This script is compatible with the BSD install script, but was written
4254569438Smrg# from scratch.
4354569438Smrg
442f60eb46Smrgtab='	'
4554569438Smrgnl='
4654569438Smrg'
472f60eb46SmrgIFS=" $tab$nl"
48692f60a7Smrg
492f60eb46Smrg# Set DOITPROG to "echo" to test this script.
50692f60a7Smrg
5154569438Smrgdoit=${DOITPROG-}
522f60eb46Smrgdoit_exec=${doit:-exec}
53692f60a7Smrg
5454569438Smrg# Put in absolute file names if you don't have them in your path;
5554569438Smrg# or use environment vars.
5654569438Smrg
5754569438Smrgchgrpprog=${CHGRPPROG-chgrp}
5854569438Smrgchmodprog=${CHMODPROG-chmod}
5954569438Smrgchownprog=${CHOWNPROG-chown}
6054569438Smrgcmpprog=${CMPPROG-cmp}
6154569438Smrgcpprog=${CPPROG-cp}
6254569438Smrgmkdirprog=${MKDIRPROG-mkdir}
6354569438Smrgmvprog=${MVPROG-mv}
6454569438Smrgrmprog=${RMPROG-rm}
6554569438Smrgstripprog=${STRIPPROG-strip}
6654569438Smrg
6754569438Smrgposix_mkdir=
6854569438Smrg
6954569438Smrg# Desired mode of installed file.
7054569438Smrgmode=0755
71692f60a7Smrg
72692f60a7Smrgchgrpcmd=
7354569438Smrgchmodcmd=$chmodprog
7454569438Smrgchowncmd=
7554569438Smrgmvcmd=$mvprog
76692f60a7Smrgrmcmd="$rmprog -f"
7754569438Smrgstripcmd=
7854569438Smrg
79692f60a7Smrgsrc=
80692f60a7Smrgdst=
81692f60a7Smrgdir_arg=
8254569438Smrgdst_arg=
8354569438Smrg
8454569438Smrgcopy_on_change=false
852f60eb46Smrgis_target_a_directory=possibly
86692f60a7Smrg
8754569438Smrgusage="\
8854569438SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89692f60a7Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90692f60a7Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91692f60a7Smrg   or: $0 [OPTION]... -d DIRECTORIES...
92692f60a7Smrg
93692f60a7SmrgIn the 1st form, copy SRCFILE to DSTFILE.
94692f60a7SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95692f60a7SmrgIn the 4th, create DIRECTORIES.
96692f60a7Smrg
97692f60a7SmrgOptions:
9854569438Smrg     --help     display this help and exit.
9954569438Smrg     --version  display version info and exit.
10054569438Smrg
10154569438Smrg  -c            (ignored)
10254569438Smrg  -C            install only if different (preserve the last data modification time)
10354569438Smrg  -d            create directories instead of installing files.
10454569438Smrg  -g GROUP      $chgrpprog installed files to GROUP.
10554569438Smrg  -m MODE       $chmodprog installed files to MODE.
10654569438Smrg  -o USER       $chownprog installed files to USER.
10754569438Smrg  -s            $stripprog installed files.
10854569438Smrg  -t DIRECTORY  install into DIRECTORY.
10954569438Smrg  -T            report an error if DSTFILE is a directory.
110692f60a7Smrg
111692f60a7SmrgEnvironment variables override the default commands:
11254569438Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
11354569438Smrg  RMPROG STRIPPROG
114692f60a7Smrg"
115692f60a7Smrg
11654569438Smrgwhile test $# -ne 0; do
117692f60a7Smrg  case $1 in
11854569438Smrg    -c) ;;
11954569438Smrg
12054569438Smrg    -C) copy_on_change=true;;
121692f60a7Smrg
12254569438Smrg    -d) dir_arg=true;;
123692f60a7Smrg
124692f60a7Smrg    -g) chgrpcmd="$chgrpprog $2"
1252f60eb46Smrg        shift;;
126692f60a7Smrg
127692f60a7Smrg    --help) echo "$usage"; exit $?;;
128692f60a7Smrg
12954569438Smrg    -m) mode=$2
1302f60eb46Smrg        case $mode in
1312f60eb46Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
1322f60eb46Smrg            echo "$0: invalid mode: $mode" >&2
1332f60eb46Smrg            exit 1;;
1342f60eb46Smrg        esac
1352f60eb46Smrg        shift;;
136692f60a7Smrg
137692f60a7Smrg    -o) chowncmd="$chownprog $2"
1382f60eb46Smrg        shift;;
139692f60a7Smrg
14054569438Smrg    -s) stripcmd=$stripprog;;
141692f60a7Smrg
1422f60eb46Smrg    -t)
1432f60eb46Smrg        is_target_a_directory=always
1442f60eb46Smrg        dst_arg=$2
1452f60eb46Smrg        # Protect names problematic for 'test' and other utilities.
1462f60eb46Smrg        case $dst_arg in
1472f60eb46Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
1482f60eb46Smrg        esac
1492f60eb46Smrg        shift;;
150692f60a7Smrg
1512f60eb46Smrg    -T) is_target_a_directory=never;;
152692f60a7Smrg
153692f60a7Smrg    --version) echo "$0 $scriptversion"; exit $?;;
154692f60a7Smrg
1552f60eb46Smrg    --) shift
1562f60eb46Smrg        break;;
15754569438Smrg
1582f60eb46Smrg    -*) echo "$0: invalid option: $1" >&2
1592f60eb46Smrg        exit 1;;
16054569438Smrg
16154569438Smrg    *)  break;;
162692f60a7Smrg  esac
16354569438Smrg  shift
164692f60a7Smrgdone
165692f60a7Smrg
1662f60eb46Smrg# We allow the use of options -d and -T together, by making -d
1672f60eb46Smrg# take the precedence; this is for compatibility with GNU install.
1682f60eb46Smrg
1692f60eb46Smrgif test -n "$dir_arg"; then
1702f60eb46Smrg  if test -n "$dst_arg"; then
1712f60eb46Smrg    echo "$0: target directory not allowed when installing a directory." >&2
1722f60eb46Smrg    exit 1
1732f60eb46Smrg  fi
1742f60eb46Smrgfi
1752f60eb46Smrg
17654569438Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17754569438Smrg  # When -d is used, all remaining arguments are directories to create.
17854569438Smrg  # When -t is used, the destination is already specified.
17954569438Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18054569438Smrg  for arg
18154569438Smrg  do
18254569438Smrg    if test -n "$dst_arg"; then
18354569438Smrg      # $@ is not empty: it contains at least $arg.
18454569438Smrg      set fnord "$@" "$dst_arg"
18554569438Smrg      shift # fnord
18654569438Smrg    fi
18754569438Smrg    shift # arg
18854569438Smrg    dst_arg=$arg
189eaa3dbe0Smrg    # Protect names problematic for 'test' and other utilities.
1903f6d0e1dSmrg    case $dst_arg in
1913f6d0e1dSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1923f6d0e1dSmrg    esac
19354569438Smrg  done
19454569438Smrgfi
19554569438Smrg
19654569438Smrgif test $# -eq 0; then
197692f60a7Smrg  if test -z "$dir_arg"; then
198692f60a7Smrg    echo "$0: no input file specified." >&2
199692f60a7Smrg    exit 1
200692f60a7Smrg  fi
201eaa3dbe0Smrg  # It's OK to call 'install-sh -d' without argument.
202692f60a7Smrg  # This can happen when creating conditional directories.
203692f60a7Smrg  exit 0
204692f60a7Smrgfi
205692f60a7Smrg
2062f60eb46Smrgif test -z "$dir_arg"; then
2072f60eb46Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
2082f60eb46Smrg    if test ! -d "$dst_arg"; then
2092f60eb46Smrg      echo "$0: $dst_arg: Is not a directory." >&2
2102f60eb46Smrg      exit 1
2112f60eb46Smrg    fi
2122f60eb46Smrg  fi
2132f60eb46Smrgfi
2142f60eb46Smrg
21554569438Smrgif test -z "$dir_arg"; then
2163f6d0e1dSmrg  do_exit='(exit $ret); exit $ret'
2173f6d0e1dSmrg  trap "ret=129; $do_exit" 1
2183f6d0e1dSmrg  trap "ret=130; $do_exit" 2
2193f6d0e1dSmrg  trap "ret=141; $do_exit" 13
2203f6d0e1dSmrg  trap "ret=143; $do_exit" 15
22154569438Smrg
22254569438Smrg  # Set umask so as not to create temps with too-generous modes.
22354569438Smrg  # However, 'strip' requires both read and write access to temps.
22454569438Smrg  case $mode in
22554569438Smrg    # Optimize common cases.
22654569438Smrg    *644) cp_umask=133;;
22754569438Smrg    *755) cp_umask=22;;
22854569438Smrg
22954569438Smrg    *[0-7])
23054569438Smrg      if test -z "$stripcmd"; then
2312f60eb46Smrg        u_plus_rw=
23254569438Smrg      else
2332f60eb46Smrg        u_plus_rw='% 200'
23454569438Smrg      fi
23554569438Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
23654569438Smrg    *)
23754569438Smrg      if test -z "$stripcmd"; then
2382f60eb46Smrg        u_plus_rw=
23954569438Smrg      else
2402f60eb46Smrg        u_plus_rw=,u+rw
24154569438Smrg      fi
24254569438Smrg      cp_umask=$mode$u_plus_rw;;
24354569438Smrg  esac
24454569438Smrgfi
24554569438Smrg
246692f60a7Smrgfor src
247692f60a7Smrgdo
248eaa3dbe0Smrg  # Protect names problematic for 'test' and other utilities.
249692f60a7Smrg  case $src in
2503f6d0e1dSmrg    -* | [=\(\)!]) src=./$src;;
251692f60a7Smrg  esac
252692f60a7Smrg
253692f60a7Smrg  if test -n "$dir_arg"; then
254692f60a7Smrg    dst=$src
25554569438Smrg    dstdir=$dst
25654569438Smrg    test -d "$dstdir"
25754569438Smrg    dstdir_status=$?
258692f60a7Smrg  else
25954569438Smrg
260692f60a7Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
261692f60a7Smrg    # might cause directories to be created, which would be especially bad
262692f60a7Smrg    # if $src (and thus $dsttmp) contains '*'.
263692f60a7Smrg    if test ! -f "$src" && test ! -d "$src"; then
264692f60a7Smrg      echo "$0: $src does not exist." >&2
265692f60a7Smrg      exit 1
266692f60a7Smrg    fi
267692f60a7Smrg
26854569438Smrg    if test -z "$dst_arg"; then
269692f60a7Smrg      echo "$0: no destination specified." >&2
270692f60a7Smrg      exit 1
271692f60a7Smrg    fi
27254569438Smrg    dst=$dst_arg
273692f60a7Smrg
274692f60a7Smrg    # If destination is a directory, append the input filename; won't work
275692f60a7Smrg    # if double slashes aren't ignored.
276692f60a7Smrg    if test -d "$dst"; then
2772f60eb46Smrg      if test "$is_target_a_directory" = never; then
2782f60eb46Smrg        echo "$0: $dst_arg: Is a directory" >&2
2792f60eb46Smrg        exit 1
280692f60a7Smrg      fi
28154569438Smrg      dstdir=$dst
28254569438Smrg      dst=$dstdir/`basename "$src"`
28354569438Smrg      dstdir_status=0
28454569438Smrg    else
2852f60eb46Smrg      dstdir=`dirname "$dst"`
28654569438Smrg      test -d "$dstdir"
28754569438Smrg      dstdir_status=$?
288692f60a7Smrg    fi
289692f60a7Smrg  fi
290692f60a7Smrg
29154569438Smrg  obsolete_mkdir_used=false
29254569438Smrg
29354569438Smrg  if test $dstdir_status != 0; then
29454569438Smrg    case $posix_mkdir in
29554569438Smrg      '')
2962f60eb46Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
2972f60eb46Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
2982f60eb46Smrg        umask=`umask`
2992f60eb46Smrg        case $stripcmd.$umask in
3002f60eb46Smrg          # Optimize common cases.
3012f60eb46Smrg          *[2367][2367]) mkdir_umask=$umask;;
3022f60eb46Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
3032f60eb46Smrg
3042f60eb46Smrg          *[0-7])
3052f60eb46Smrg            mkdir_umask=`expr $umask + 22 \
3062f60eb46Smrg              - $umask % 100 % 40 + $umask % 20 \
3072f60eb46Smrg              - $umask % 10 % 4 + $umask % 2
3082f60eb46Smrg            `;;
3092f60eb46Smrg          *) mkdir_umask=$umask,go-w;;
3102f60eb46Smrg        esac
3112f60eb46Smrg
3122f60eb46Smrg        # With -d, create the new directory with the user-specified mode.
3132f60eb46Smrg        # Otherwise, rely on $mkdir_umask.
3142f60eb46Smrg        if test -n "$dir_arg"; then
3152f60eb46Smrg          mkdir_mode=-m$mode
3162f60eb46Smrg        else
3172f60eb46Smrg          mkdir_mode=
3182f60eb46Smrg        fi
3192f60eb46Smrg
3202f60eb46Smrg        posix_mkdir=false
3212f60eb46Smrg        case $umask in
3222f60eb46Smrg          *[123567][0-7][0-7])
3232f60eb46Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
3242f60eb46Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
3252f60eb46Smrg            ;;
3262f60eb46Smrg          *)
3272f60eb46Smrg            # $RANDOM is not portable (e.g. dash);  use it when possible to
3282f60eb46Smrg            # lower collision chance
3292f60eb46Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
3302f60eb46Smrg            trap 'ret=$?; rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir" 2>/dev/null; exit $ret' 0
3312f60eb46Smrg
3322f60eb46Smrg            # As "mkdir -p" follows symlinks and we work in /tmp possibly;  so
3332f60eb46Smrg            # create the $tmpdir first (and fail if unsuccessful) to make sure
3342f60eb46Smrg            # that nobody tries to guess the $tmpdir name.
3352f60eb46Smrg            if (umask $mkdir_umask &&
3362f60eb46Smrg                $mkdirprog $mkdir_mode "$tmpdir" &&
3372f60eb46Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/a/b") >/dev/null 2>&1
3382f60eb46Smrg            then
3392f60eb46Smrg              if test -z "$dir_arg" || {
3402f60eb46Smrg                   # Check for POSIX incompatibilities with -m.
3412f60eb46Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
3422f60eb46Smrg                   # other-writable bit of parent directory when it shouldn't.
3432f60eb46Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
3442f60eb46Smrg                   test_tmpdir="$tmpdir/a"
3452f60eb46Smrg                   ls_ld_tmpdir=`ls -ld "$test_tmpdir"`
3462f60eb46Smrg                   case $ls_ld_tmpdir in
3472f60eb46Smrg                     d????-?r-*) different_mode=700;;
3482f60eb46Smrg                     d????-?--*) different_mode=755;;
3492f60eb46Smrg                     *) false;;
3502f60eb46Smrg                   esac &&
3512f60eb46Smrg                   $mkdirprog -m$different_mode -p -- "$test_tmpdir" && {
3522f60eb46Smrg                     ls_ld_tmpdir_1=`ls -ld "$test_tmpdir"`
3532f60eb46Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
3542f60eb46Smrg                   }
3552f60eb46Smrg                 }
3562f60eb46Smrg              then posix_mkdir=:
3572f60eb46Smrg              fi
3582f60eb46Smrg              rmdir "$tmpdir/a/b" "$tmpdir/a" "$tmpdir"
3592f60eb46Smrg            else
3602f60eb46Smrg              # Remove any dirs left behind by ancient mkdir implementations.
3612f60eb46Smrg              rmdir ./$mkdir_mode ./-p ./-- "$tmpdir" 2>/dev/null
3622f60eb46Smrg            fi
3632f60eb46Smrg            trap '' 0;;
3642f60eb46Smrg        esac;;
36554569438Smrg    esac
366692f60a7Smrg
36754569438Smrg    if
36854569438Smrg      $posix_mkdir && (
3692f60eb46Smrg        umask $mkdir_umask &&
3702f60eb46Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
37154569438Smrg      )
37254569438Smrg    then :
37354569438Smrg    else
374692f60a7Smrg
37554569438Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
37654569438Smrg      # or it failed possibly due to a race condition.  Create the
37754569438Smrg      # directory the slow way, step by step, checking for races as we go.
378692f60a7Smrg
37954569438Smrg      case $dstdir in
3802f60eb46Smrg        /*) prefix='/';;
3812f60eb46Smrg        [-=\(\)!]*) prefix='./';;
3822f60eb46Smrg        *)  prefix='';;
38354569438Smrg      esac
384692f60a7Smrg
38554569438Smrg      oIFS=$IFS
38654569438Smrg      IFS=/
3872f60eb46Smrg      set -f
38854569438Smrg      set fnord $dstdir
389692f60a7Smrg      shift
3902f60eb46Smrg      set +f
39154569438Smrg      IFS=$oIFS
39254569438Smrg
39354569438Smrg      prefixes=
39454569438Smrg
39554569438Smrg      for d
39654569438Smrg      do
3972f60eb46Smrg        test X"$d" = X && continue
3982f60eb46Smrg
3992f60eb46Smrg        prefix=$prefix$d
4002f60eb46Smrg        if test -d "$prefix"; then
4012f60eb46Smrg          prefixes=
4022f60eb46Smrg        else
4032f60eb46Smrg          if $posix_mkdir; then
4042f60eb46Smrg            (umask=$mkdir_umask &&
4052f60eb46Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
4062f60eb46Smrg            # Don't fail if two instances are running concurrently.
4072f60eb46Smrg            test -d "$prefix" || exit 1
4082f60eb46Smrg          else
4092f60eb46Smrg            case $prefix in
4102f60eb46Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
4112f60eb46Smrg              *) qprefix=$prefix;;
4122f60eb46Smrg            esac
4132f60eb46Smrg            prefixes="$prefixes '$qprefix'"
4142f60eb46Smrg          fi
4152f60eb46Smrg        fi
4162f60eb46Smrg        prefix=$prefix/
41754569438Smrg      done
41854569438Smrg
41954569438Smrg      if test -n "$prefixes"; then
4202f60eb46Smrg        # Don't fail if two instances are running concurrently.
4212f60eb46Smrg        (umask $mkdir_umask &&
4222f60eb46Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
4232f60eb46Smrg          test -d "$dstdir" || exit 1
4242f60eb46Smrg        obsolete_mkdir_used=true
425692f60a7Smrg      fi
42654569438Smrg    fi
427692f60a7Smrg  fi
428692f60a7Smrg
429692f60a7Smrg  if test -n "$dir_arg"; then
43054569438Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
43154569438Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
43254569438Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
43354569438Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
434692f60a7Smrg  else
435692f60a7Smrg
436692f60a7Smrg    # Make a couple of temp file names in the proper directory.
437692f60a7Smrg    dsttmp=$dstdir/_inst.$$_
438692f60a7Smrg    rmtmp=$dstdir/_rm.$$_
439692f60a7Smrg
440692f60a7Smrg    # Trap to clean up those temp files at exit.
441692f60a7Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
442692f60a7Smrg
443692f60a7Smrg    # Copy the file name to the temp name.
44454569438Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
445692f60a7Smrg
446692f60a7Smrg    # and set any options; do chmod last to preserve setuid bits.
447692f60a7Smrg    #
448692f60a7Smrg    # If any of these fail, we abort the whole thing.  If we want to
449692f60a7Smrg    # ignore errors from any of these, just make sure not to ignore
450692f60a7Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
451692f60a7Smrg    #
45254569438Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
45354569438Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
45454569438Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
45554569438Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
45654569438Smrg
45754569438Smrg    # If -C, don't bother to copy if it wouldn't change the file.
45854569438Smrg    if $copy_on_change &&
4592f60eb46Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
4602f60eb46Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
4612f60eb46Smrg       set -f &&
46254569438Smrg       set X $old && old=:$2:$4:$5:$6 &&
46354569438Smrg       set X $new && new=:$2:$4:$5:$6 &&
4642f60eb46Smrg       set +f &&
46554569438Smrg       test "$old" = "$new" &&
46654569438Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
46754569438Smrg    then
46854569438Smrg      rm -f "$dsttmp"
46954569438Smrg    else
47054569438Smrg      # Rename the file to the real destination.
47154569438Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
47254569438Smrg
47354569438Smrg      # The rename failed, perhaps because mv can't rename something else
47454569438Smrg      # to itself, or perhaps because mv is so ancient that it does not
47554569438Smrg      # support -f.
47654569438Smrg      {
4772f60eb46Smrg        # Now remove or move aside any old file at destination location.
4782f60eb46Smrg        # We try this two ways since rm can't unlink itself on some
4792f60eb46Smrg        # systems and the destination file might be busy for other
4802f60eb46Smrg        # reasons.  In this case, the final cleanup might fail but the new
4812f60eb46Smrg        # file should still install successfully.
4822f60eb46Smrg        {
4832f60eb46Smrg          test ! -f "$dst" ||
4842f60eb46Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
4852f60eb46Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
4862f60eb46Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
4872f60eb46Smrg          } ||
4882f60eb46Smrg          { echo "$0: cannot unlink or rename $dst" >&2
4892f60eb46Smrg            (exit 1); exit 1
4902f60eb46Smrg          }
4912f60eb46Smrg        } &&
4922f60eb46Smrg
4932f60eb46Smrg        # Now rename the file to the real destination.
4942f60eb46Smrg        $doit $mvcmd "$dsttmp" "$dst"
49554569438Smrg      }
49654569438Smrg    fi || exit 1
49754569438Smrg
49854569438Smrg    trap '' 0
49954569438Smrg  fi
500692f60a7Smrgdone
501692f60a7Smrg
502692f60a7Smrg# Local variables:
503692f60a7Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
504692f60a7Smrg# time-stamp-start: "scriptversion="
505692f60a7Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
5063f6d0e1dSmrg# time-stamp-time-zone: "UTC"
5073f6d0e1dSmrg# time-stamp-end: "; # UTC"
508692f60a7Smrg# End:
509