install-sh revision 40c5344f
17a0395d0Smrg#!/bin/sh
27a0395d0Smrg# install - install a program, script, or datafile
37a0395d0Smrg
440c5344fSmrgscriptversion=2013-12-25.23; # UTC
57a0395d0Smrg
67a0395d0Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
77a0395d0Smrg# later released in X11R6 (xc/config/util/install.sh) with the
87a0395d0Smrg# following copyright and license.
97a0395d0Smrg#
107a0395d0Smrg# Copyright (C) 1994 X Consortium
117a0395d0Smrg#
127a0395d0Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
137a0395d0Smrg# of this software and associated documentation files (the "Software"), to
147a0395d0Smrg# deal in the Software without restriction, including without limitation the
157a0395d0Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
167a0395d0Smrg# sell copies of the Software, and to permit persons to whom the Software is
177a0395d0Smrg# furnished to do so, subject to the following conditions:
187a0395d0Smrg#
197a0395d0Smrg# The above copyright notice and this permission notice shall be included in
207a0395d0Smrg# all copies or substantial portions of the Software.
217a0395d0Smrg#
227a0395d0Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
237a0395d0Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
247a0395d0Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
257a0395d0Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
267a0395d0Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
277a0395d0Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
287a0395d0Smrg#
297a0395d0Smrg# Except as contained in this notice, the name of the X Consortium shall not
307a0395d0Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
317a0395d0Smrg# ings in this Software without prior written authorization from the X Consor-
327a0395d0Smrg# tium.
337a0395d0Smrg#
347a0395d0Smrg#
357a0395d0Smrg# FSF changes to this file are in the public domain.
367a0395d0Smrg#
377a0395d0Smrg# Calling this script install-sh is preferred over install.sh, to prevent
388abc0ccfSmrg# 'make' implicit rules from creating a file called install from it
397a0395d0Smrg# when there is no Makefile.
407a0395d0Smrg#
417a0395d0Smrg# This script is compatible with the BSD install script, but was written
427a0395d0Smrg# from scratch.
437a0395d0Smrg
4440c5344fSmrgtab='	'
457a0395d0Smrgnl='
467a0395d0Smrg'
4740c5344fSmrgIFS=" $tab$nl"
487a0395d0Smrg
4940c5344fSmrg# Set DOITPROG to "echo" to test this script.
507a0395d0Smrg
517a0395d0Smrgdoit=${DOITPROG-}
5240c5344fSmrgdoit_exec=${doit:-exec}
537a0395d0Smrg
547a0395d0Smrg# Put in absolute file names if you don't have them in your path;
557a0395d0Smrg# or use environment vars.
567a0395d0Smrg
577a0395d0Smrgchgrpprog=${CHGRPPROG-chgrp}
587a0395d0Smrgchmodprog=${CHMODPROG-chmod}
597a0395d0Smrgchownprog=${CHOWNPROG-chown}
607a0395d0Smrgcmpprog=${CMPPROG-cmp}
617a0395d0Smrgcpprog=${CPPROG-cp}
627a0395d0Smrgmkdirprog=${MKDIRPROG-mkdir}
637a0395d0Smrgmvprog=${MVPROG-mv}
647a0395d0Smrgrmprog=${RMPROG-rm}
657a0395d0Smrgstripprog=${STRIPPROG-strip}
667a0395d0Smrg
677a0395d0Smrgposix_mkdir=
687a0395d0Smrg
697a0395d0Smrg# Desired mode of installed file.
707a0395d0Smrgmode=0755
717a0395d0Smrg
727a0395d0Smrgchgrpcmd=
737a0395d0Smrgchmodcmd=$chmodprog
747a0395d0Smrgchowncmd=
757a0395d0Smrgmvcmd=$mvprog
767a0395d0Smrgrmcmd="$rmprog -f"
777a0395d0Smrgstripcmd=
787a0395d0Smrg
797a0395d0Smrgsrc=
807a0395d0Smrgdst=
817a0395d0Smrgdir_arg=
827a0395d0Smrgdst_arg=
837a0395d0Smrg
847a0395d0Smrgcopy_on_change=false
8540c5344fSmrgis_target_a_directory=possibly
867a0395d0Smrg
877a0395d0Smrgusage="\
887a0395d0SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
897a0395d0Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
907a0395d0Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
917a0395d0Smrg   or: $0 [OPTION]... -d DIRECTORIES...
927a0395d0Smrg
937a0395d0SmrgIn the 1st form, copy SRCFILE to DSTFILE.
947a0395d0SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
957a0395d0SmrgIn the 4th, create DIRECTORIES.
967a0395d0Smrg
977a0395d0SmrgOptions:
987a0395d0Smrg     --help     display this help and exit.
997a0395d0Smrg     --version  display version info and exit.
1007a0395d0Smrg
1017a0395d0Smrg  -c            (ignored)
1027a0395d0Smrg  -C            install only if different (preserve the last data modification time)
1037a0395d0Smrg  -d            create directories instead of installing files.
1047a0395d0Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1057a0395d0Smrg  -m MODE       $chmodprog installed files to MODE.
1067a0395d0Smrg  -o USER       $chownprog installed files to USER.
1077a0395d0Smrg  -s            $stripprog installed files.
1087a0395d0Smrg  -t DIRECTORY  install into DIRECTORY.
1097a0395d0Smrg  -T            report an error if DSTFILE is a directory.
1107a0395d0Smrg
1117a0395d0SmrgEnvironment variables override the default commands:
1127a0395d0Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1137a0395d0Smrg  RMPROG STRIPPROG
1147a0395d0Smrg"
1157a0395d0Smrg
1167a0395d0Smrgwhile test $# -ne 0; do
1177a0395d0Smrg  case $1 in
1187a0395d0Smrg    -c) ;;
1197a0395d0Smrg
1207a0395d0Smrg    -C) copy_on_change=true;;
1217a0395d0Smrg
1227a0395d0Smrg    -d) dir_arg=true;;
1237a0395d0Smrg
1247a0395d0Smrg    -g) chgrpcmd="$chgrpprog $2"
12540c5344fSmrg        shift;;
1267a0395d0Smrg
1277a0395d0Smrg    --help) echo "$usage"; exit $?;;
1287a0395d0Smrg
1297a0395d0Smrg    -m) mode=$2
13040c5344fSmrg        case $mode in
13140c5344fSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
13240c5344fSmrg            echo "$0: invalid mode: $mode" >&2
13340c5344fSmrg            exit 1;;
13440c5344fSmrg        esac
13540c5344fSmrg        shift;;
1367a0395d0Smrg
1377a0395d0Smrg    -o) chowncmd="$chownprog $2"
13840c5344fSmrg        shift;;
1397a0395d0Smrg
1407a0395d0Smrg    -s) stripcmd=$stripprog;;
1417a0395d0Smrg
14240c5344fSmrg    -t)
14340c5344fSmrg        is_target_a_directory=always
14440c5344fSmrg        dst_arg=$2
14540c5344fSmrg        # Protect names problematic for 'test' and other utilities.
14640c5344fSmrg        case $dst_arg in
14740c5344fSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
14840c5344fSmrg        esac
14940c5344fSmrg        shift;;
1507a0395d0Smrg
15140c5344fSmrg    -T) is_target_a_directory=never;;
1527a0395d0Smrg
1537a0395d0Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1547a0395d0Smrg
15540c5344fSmrg    --) shift
15640c5344fSmrg        break;;
1577a0395d0Smrg
15840c5344fSmrg    -*) echo "$0: invalid option: $1" >&2
15940c5344fSmrg        exit 1;;
1607a0395d0Smrg
1617a0395d0Smrg    *)  break;;
1627a0395d0Smrg  esac
1637a0395d0Smrg  shift
1647a0395d0Smrgdone
1657a0395d0Smrg
16640c5344fSmrg# We allow the use of options -d and -T together, by making -d
16740c5344fSmrg# take the precedence; this is for compatibility with GNU install.
16840c5344fSmrg
16940c5344fSmrgif test -n "$dir_arg"; then
17040c5344fSmrg  if test -n "$dst_arg"; then
17140c5344fSmrg    echo "$0: target directory not allowed when installing a directory." >&2
17240c5344fSmrg    exit 1
17340c5344fSmrg  fi
17440c5344fSmrgfi
17540c5344fSmrg
1767a0395d0Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1777a0395d0Smrg  # When -d is used, all remaining arguments are directories to create.
1787a0395d0Smrg  # When -t is used, the destination is already specified.
1797a0395d0Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1807a0395d0Smrg  for arg
1817a0395d0Smrg  do
1827a0395d0Smrg    if test -n "$dst_arg"; then
1837a0395d0Smrg      # $@ is not empty: it contains at least $arg.
1847a0395d0Smrg      set fnord "$@" "$dst_arg"
1857a0395d0Smrg      shift # fnord
1867a0395d0Smrg    fi
1877a0395d0Smrg    shift # arg
1887a0395d0Smrg    dst_arg=$arg
1898abc0ccfSmrg    # Protect names problematic for 'test' and other utilities.
1908abc0ccfSmrg    case $dst_arg in
1918abc0ccfSmrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
1928abc0ccfSmrg    esac
1937a0395d0Smrg  done
1947a0395d0Smrgfi
1957a0395d0Smrg
1967a0395d0Smrgif test $# -eq 0; then
1977a0395d0Smrg  if test -z "$dir_arg"; then
1987a0395d0Smrg    echo "$0: no input file specified." >&2
1997a0395d0Smrg    exit 1
2007a0395d0Smrg  fi
2018abc0ccfSmrg  # It's OK to call 'install-sh -d' without argument.
2027a0395d0Smrg  # This can happen when creating conditional directories.
2037a0395d0Smrg  exit 0
2047a0395d0Smrgfi
2057a0395d0Smrg
20640c5344fSmrgif test -z "$dir_arg"; then
20740c5344fSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
20840c5344fSmrg    if test ! -d "$dst_arg"; then
20940c5344fSmrg      echo "$0: $dst_arg: Is not a directory." >&2
21040c5344fSmrg      exit 1
21140c5344fSmrg    fi
21240c5344fSmrg  fi
21340c5344fSmrgfi
21440c5344fSmrg
2157a0395d0Smrgif test -z "$dir_arg"; then
2168abc0ccfSmrg  do_exit='(exit $ret); exit $ret'
2178abc0ccfSmrg  trap "ret=129; $do_exit" 1
2188abc0ccfSmrg  trap "ret=130; $do_exit" 2
2198abc0ccfSmrg  trap "ret=141; $do_exit" 13
2208abc0ccfSmrg  trap "ret=143; $do_exit" 15
2217a0395d0Smrg
2227a0395d0Smrg  # Set umask so as not to create temps with too-generous modes.
2237a0395d0Smrg  # However, 'strip' requires both read and write access to temps.
2247a0395d0Smrg  case $mode in
2257a0395d0Smrg    # Optimize common cases.
2267a0395d0Smrg    *644) cp_umask=133;;
2277a0395d0Smrg    *755) cp_umask=22;;
2287a0395d0Smrg
2297a0395d0Smrg    *[0-7])
2307a0395d0Smrg      if test -z "$stripcmd"; then
23140c5344fSmrg        u_plus_rw=
2327a0395d0Smrg      else
23340c5344fSmrg        u_plus_rw='% 200'
2347a0395d0Smrg      fi
2357a0395d0Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2367a0395d0Smrg    *)
2377a0395d0Smrg      if test -z "$stripcmd"; then
23840c5344fSmrg        u_plus_rw=
2397a0395d0Smrg      else
24040c5344fSmrg        u_plus_rw=,u+rw
2417a0395d0Smrg      fi
2427a0395d0Smrg      cp_umask=$mode$u_plus_rw;;
2437a0395d0Smrg  esac
2447a0395d0Smrgfi
2457a0395d0Smrg
2467a0395d0Smrgfor src
2477a0395d0Smrgdo
2488abc0ccfSmrg  # Protect names problematic for 'test' and other utilities.
2497a0395d0Smrg  case $src in
2508abc0ccfSmrg    -* | [=\(\)!]) src=./$src;;
2517a0395d0Smrg  esac
2527a0395d0Smrg
2537a0395d0Smrg  if test -n "$dir_arg"; then
2547a0395d0Smrg    dst=$src
2557a0395d0Smrg    dstdir=$dst
2567a0395d0Smrg    test -d "$dstdir"
2577a0395d0Smrg    dstdir_status=$?
2587a0395d0Smrg  else
2597a0395d0Smrg
2607a0395d0Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2617a0395d0Smrg    # might cause directories to be created, which would be especially bad
2627a0395d0Smrg    # if $src (and thus $dsttmp) contains '*'.
2637a0395d0Smrg    if test ! -f "$src" && test ! -d "$src"; then
2647a0395d0Smrg      echo "$0: $src does not exist." >&2
2657a0395d0Smrg      exit 1
2667a0395d0Smrg    fi
2677a0395d0Smrg
2687a0395d0Smrg    if test -z "$dst_arg"; then
2697a0395d0Smrg      echo "$0: no destination specified." >&2
2707a0395d0Smrg      exit 1
2717a0395d0Smrg    fi
2727a0395d0Smrg    dst=$dst_arg
2737a0395d0Smrg
2747a0395d0Smrg    # If destination is a directory, append the input filename; won't work
2757a0395d0Smrg    # if double slashes aren't ignored.
2767a0395d0Smrg    if test -d "$dst"; then
27740c5344fSmrg      if test "$is_target_a_directory" = never; then
27840c5344fSmrg        echo "$0: $dst_arg: Is a directory" >&2
27940c5344fSmrg        exit 1
2807a0395d0Smrg      fi
2817a0395d0Smrg      dstdir=$dst
2827a0395d0Smrg      dst=$dstdir/`basename "$src"`
2837a0395d0Smrg      dstdir_status=0
2847a0395d0Smrg    else
28540c5344fSmrg      dstdir=`dirname "$dst"`
2867a0395d0Smrg      test -d "$dstdir"
2877a0395d0Smrg      dstdir_status=$?
2887a0395d0Smrg    fi
2897a0395d0Smrg  fi
2907a0395d0Smrg
2917a0395d0Smrg  obsolete_mkdir_used=false
2927a0395d0Smrg
2937a0395d0Smrg  if test $dstdir_status != 0; then
2947a0395d0Smrg    case $posix_mkdir in
2957a0395d0Smrg      '')
29640c5344fSmrg        # Create intermediate dirs using mode 755 as modified by the umask.
29740c5344fSmrg        # This is like FreeBSD 'install' as of 1997-10-28.
29840c5344fSmrg        umask=`umask`
29940c5344fSmrg        case $stripcmd.$umask in
30040c5344fSmrg          # Optimize common cases.
30140c5344fSmrg          *[2367][2367]) mkdir_umask=$umask;;
30240c5344fSmrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
30340c5344fSmrg
30440c5344fSmrg          *[0-7])
30540c5344fSmrg            mkdir_umask=`expr $umask + 22 \
30640c5344fSmrg              - $umask % 100 % 40 + $umask % 20 \
30740c5344fSmrg              - $umask % 10 % 4 + $umask % 2
30840c5344fSmrg            `;;
30940c5344fSmrg          *) mkdir_umask=$umask,go-w;;
31040c5344fSmrg        esac
31140c5344fSmrg
31240c5344fSmrg        # With -d, create the new directory with the user-specified mode.
31340c5344fSmrg        # Otherwise, rely on $mkdir_umask.
31440c5344fSmrg        if test -n "$dir_arg"; then
31540c5344fSmrg          mkdir_mode=-m$mode
31640c5344fSmrg        else
31740c5344fSmrg          mkdir_mode=
31840c5344fSmrg        fi
31940c5344fSmrg
32040c5344fSmrg        posix_mkdir=false
32140c5344fSmrg        case $umask in
32240c5344fSmrg          *[123567][0-7][0-7])
32340c5344fSmrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
32440c5344fSmrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
32540c5344fSmrg            ;;
32640c5344fSmrg          *)
32740c5344fSmrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
32840c5344fSmrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
32940c5344fSmrg
33040c5344fSmrg            if (umask $mkdir_umask &&
33140c5344fSmrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
33240c5344fSmrg            then
33340c5344fSmrg              if test -z "$dir_arg" || {
33440c5344fSmrg                   # Check for POSIX incompatibilities with -m.
33540c5344fSmrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
33640c5344fSmrg                   # other-writable bit of parent directory when it shouldn't.
33740c5344fSmrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
33840c5344fSmrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
33940c5344fSmrg                   case $ls_ld_tmpdir in
34040c5344fSmrg                     d????-?r-*) different_mode=700;;
34140c5344fSmrg                     d????-?--*) different_mode=755;;
34240c5344fSmrg                     *) false;;
34340c5344fSmrg                   esac &&
34440c5344fSmrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
34540c5344fSmrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
34640c5344fSmrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
34740c5344fSmrg                   }
34840c5344fSmrg                 }
34940c5344fSmrg              then posix_mkdir=:
35040c5344fSmrg              fi
35140c5344fSmrg              rmdir "$tmpdir/d" "$tmpdir"
35240c5344fSmrg            else
35340c5344fSmrg              # Remove any dirs left behind by ancient mkdir implementations.
35440c5344fSmrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
35540c5344fSmrg            fi
35640c5344fSmrg            trap '' 0;;
35740c5344fSmrg        esac;;
3587a0395d0Smrg    esac
3597a0395d0Smrg
3607a0395d0Smrg    if
3617a0395d0Smrg      $posix_mkdir && (
36240c5344fSmrg        umask $mkdir_umask &&
36340c5344fSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3647a0395d0Smrg      )
3657a0395d0Smrg    then :
3667a0395d0Smrg    else
3677a0395d0Smrg
3687a0395d0Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3697a0395d0Smrg      # or it failed possibly due to a race condition.  Create the
3707a0395d0Smrg      # directory the slow way, step by step, checking for races as we go.
3717a0395d0Smrg
3727a0395d0Smrg      case $dstdir in
37340c5344fSmrg        /*) prefix='/';;
37440c5344fSmrg        [-=\(\)!]*) prefix='./';;
37540c5344fSmrg        *)  prefix='';;
3767a0395d0Smrg      esac
3777a0395d0Smrg
3787a0395d0Smrg      oIFS=$IFS
3797a0395d0Smrg      IFS=/
38040c5344fSmrg      set -f
3817a0395d0Smrg      set fnord $dstdir
3827a0395d0Smrg      shift
38340c5344fSmrg      set +f
3847a0395d0Smrg      IFS=$oIFS
3857a0395d0Smrg
3867a0395d0Smrg      prefixes=
3877a0395d0Smrg
3887a0395d0Smrg      for d
3897a0395d0Smrg      do
39040c5344fSmrg        test X"$d" = X && continue
39140c5344fSmrg
39240c5344fSmrg        prefix=$prefix$d
39340c5344fSmrg        if test -d "$prefix"; then
39440c5344fSmrg          prefixes=
39540c5344fSmrg        else
39640c5344fSmrg          if $posix_mkdir; then
39740c5344fSmrg            (umask=$mkdir_umask &&
39840c5344fSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
39940c5344fSmrg            # Don't fail if two instances are running concurrently.
40040c5344fSmrg            test -d "$prefix" || exit 1
40140c5344fSmrg          else
40240c5344fSmrg            case $prefix in
40340c5344fSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
40440c5344fSmrg              *) qprefix=$prefix;;
40540c5344fSmrg            esac
40640c5344fSmrg            prefixes="$prefixes '$qprefix'"
40740c5344fSmrg          fi
40840c5344fSmrg        fi
40940c5344fSmrg        prefix=$prefix/
4107a0395d0Smrg      done
4117a0395d0Smrg
4127a0395d0Smrg      if test -n "$prefixes"; then
41340c5344fSmrg        # Don't fail if two instances are running concurrently.
41440c5344fSmrg        (umask $mkdir_umask &&
41540c5344fSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
41640c5344fSmrg          test -d "$dstdir" || exit 1
41740c5344fSmrg        obsolete_mkdir_used=true
4187a0395d0Smrg      fi
4197a0395d0Smrg    fi
4207a0395d0Smrg  fi
4217a0395d0Smrg
4227a0395d0Smrg  if test -n "$dir_arg"; then
4237a0395d0Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4247a0395d0Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4257a0395d0Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4267a0395d0Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4277a0395d0Smrg  else
4287a0395d0Smrg
4297a0395d0Smrg    # Make a couple of temp file names in the proper directory.
4307a0395d0Smrg    dsttmp=$dstdir/_inst.$$_
4317a0395d0Smrg    rmtmp=$dstdir/_rm.$$_
4327a0395d0Smrg
4337a0395d0Smrg    # Trap to clean up those temp files at exit.
4347a0395d0Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4357a0395d0Smrg
4367a0395d0Smrg    # Copy the file name to the temp name.
4377a0395d0Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4387a0395d0Smrg
4397a0395d0Smrg    # and set any options; do chmod last to preserve setuid bits.
4407a0395d0Smrg    #
4417a0395d0Smrg    # If any of these fail, we abort the whole thing.  If we want to
4427a0395d0Smrg    # ignore errors from any of these, just make sure not to ignore
4437a0395d0Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4447a0395d0Smrg    #
4457a0395d0Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4467a0395d0Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4477a0395d0Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4487a0395d0Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4497a0395d0Smrg
4507a0395d0Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4517a0395d0Smrg    if $copy_on_change &&
45240c5344fSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
45340c5344fSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
45440c5344fSmrg       set -f &&
4557a0395d0Smrg       set X $old && old=:$2:$4:$5:$6 &&
4567a0395d0Smrg       set X $new && new=:$2:$4:$5:$6 &&
45740c5344fSmrg       set +f &&
4587a0395d0Smrg       test "$old" = "$new" &&
4597a0395d0Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4607a0395d0Smrg    then
4617a0395d0Smrg      rm -f "$dsttmp"
4627a0395d0Smrg    else
4637a0395d0Smrg      # Rename the file to the real destination.
4647a0395d0Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4657a0395d0Smrg
4667a0395d0Smrg      # The rename failed, perhaps because mv can't rename something else
4677a0395d0Smrg      # to itself, or perhaps because mv is so ancient that it does not
4687a0395d0Smrg      # support -f.
4697a0395d0Smrg      {
47040c5344fSmrg        # Now remove or move aside any old file at destination location.
47140c5344fSmrg        # We try this two ways since rm can't unlink itself on some
47240c5344fSmrg        # systems and the destination file might be busy for other
47340c5344fSmrg        # reasons.  In this case, the final cleanup might fail but the new
47440c5344fSmrg        # file should still install successfully.
47540c5344fSmrg        {
47640c5344fSmrg          test ! -f "$dst" ||
47740c5344fSmrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
47840c5344fSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
47940c5344fSmrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
48040c5344fSmrg          } ||
48140c5344fSmrg          { echo "$0: cannot unlink or rename $dst" >&2
48240c5344fSmrg            (exit 1); exit 1
48340c5344fSmrg          }
48440c5344fSmrg        } &&
48540c5344fSmrg
48640c5344fSmrg        # Now rename the file to the real destination.
48740c5344fSmrg        $doit $mvcmd "$dsttmp" "$dst"
4887a0395d0Smrg      }
4897a0395d0Smrg    fi || exit 1
4907a0395d0Smrg
4917a0395d0Smrg    trap '' 0
4927a0395d0Smrg  fi
4937a0395d0Smrgdone
4947a0395d0Smrg
4957a0395d0Smrg# Local variables:
4967a0395d0Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
4977a0395d0Smrg# time-stamp-start: "scriptversion="
4987a0395d0Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
4997366012aSmrg# time-stamp-time-zone: "UTC"
5007366012aSmrg# time-stamp-end: "; # UTC"
5017a0395d0Smrg# End:
502