install-sh revision 95fdd44c
164ce7165Smrg#!/bin/sh
264ce7165Smrg# install - install a program, script, or datafile
364ce7165Smrg
495fdd44cSmrgscriptversion=2013-12-25.23; # UTC
564ce7165Smrg
664ce7165Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
764ce7165Smrg# later released in X11R6 (xc/config/util/install.sh) with the
864ce7165Smrg# following copyright and license.
964ce7165Smrg#
1064ce7165Smrg# Copyright (C) 1994 X Consortium
1164ce7165Smrg#
1264ce7165Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
1364ce7165Smrg# of this software and associated documentation files (the "Software"), to
1464ce7165Smrg# deal in the Software without restriction, including without limitation the
1564ce7165Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
1664ce7165Smrg# sell copies of the Software, and to permit persons to whom the Software is
1764ce7165Smrg# furnished to do so, subject to the following conditions:
1864ce7165Smrg#
1964ce7165Smrg# The above copyright notice and this permission notice shall be included in
2064ce7165Smrg# all copies or substantial portions of the Software.
2164ce7165Smrg#
2264ce7165Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
2364ce7165Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
2464ce7165Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
2564ce7165Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
2664ce7165Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
2764ce7165Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
2864ce7165Smrg#
2964ce7165Smrg# Except as contained in this notice, the name of the X Consortium shall not
3064ce7165Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
3164ce7165Smrg# ings in this Software without prior written authorization from the X Consor-
3264ce7165Smrg# tium.
3364ce7165Smrg#
3464ce7165Smrg#
3564ce7165Smrg# FSF changes to this file are in the public domain.
3664ce7165Smrg#
3764ce7165Smrg# Calling this script install-sh is preferred over install.sh, to prevent
3881440437Smrg# 'make' implicit rules from creating a file called install from it
3964ce7165Smrg# when there is no Makefile.
4064ce7165Smrg#
4164ce7165Smrg# This script is compatible with the BSD install script, but was written
4264ce7165Smrg# from scratch.
4364ce7165Smrg
4495fdd44cSmrgtab='	'
4564ce7165Smrgnl='
4664ce7165Smrg'
4795fdd44cSmrgIFS=" $tab$nl"
4864ce7165Smrg
4995fdd44cSmrg# Set DOITPROG to "echo" to test this script.
5064ce7165Smrg
51fb23d3a8Smrgdoit=${DOITPROG-}
5295fdd44cSmrgdoit_exec=${doit:-exec}
5364ce7165Smrg
5464ce7165Smrg# Put in absolute file names if you don't have them in your path;
5564ce7165Smrg# or use environment vars.
5664ce7165Smrg
57fb23d3a8Smrgchgrpprog=${CHGRPPROG-chgrp}
58fb23d3a8Smrgchmodprog=${CHMODPROG-chmod}
59fb23d3a8Smrgchownprog=${CHOWNPROG-chown}
60fb23d3a8Smrgcmpprog=${CMPPROG-cmp}
61fb23d3a8Smrgcpprog=${CPPROG-cp}
62fb23d3a8Smrgmkdirprog=${MKDIRPROG-mkdir}
63fb23d3a8Smrgmvprog=${MVPROG-mv}
64fb23d3a8Smrgrmprog=${RMPROG-rm}
65fb23d3a8Smrgstripprog=${STRIPPROG-strip}
66fb23d3a8Smrg
6764ce7165Smrgposix_mkdir=
6864ce7165Smrg
6964ce7165Smrg# Desired mode of installed file.
7064ce7165Smrgmode=0755
7164ce7165Smrg
72fb23d3a8Smrgchgrpcmd=
7364ce7165Smrgchmodcmd=$chmodprog
7464ce7165Smrgchowncmd=
75fb23d3a8Smrgmvcmd=$mvprog
7664ce7165Smrgrmcmd="$rmprog -f"
77fb23d3a8Smrgstripcmd=
78fb23d3a8Smrg
7964ce7165Smrgsrc=
8064ce7165Smrgdst=
8164ce7165Smrgdir_arg=
82fb23d3a8Smrgdst_arg=
83fb23d3a8Smrg
84fb23d3a8Smrgcopy_on_change=false
8595fdd44cSmrgis_target_a_directory=possibly
8664ce7165Smrg
87fb23d3a8Smrgusage="\
88fb23d3a8SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
8964ce7165Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
9064ce7165Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
9164ce7165Smrg   or: $0 [OPTION]... -d DIRECTORIES...
9264ce7165Smrg
9364ce7165SmrgIn the 1st form, copy SRCFILE to DSTFILE.
9464ce7165SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
9564ce7165SmrgIn the 4th, create DIRECTORIES.
9664ce7165Smrg
9764ce7165SmrgOptions:
98fb23d3a8Smrg     --help     display this help and exit.
99fb23d3a8Smrg     --version  display version info and exit.
100fb23d3a8Smrg
101fb23d3a8Smrg  -c            (ignored)
102fb23d3a8Smrg  -C            install only if different (preserve the last data modification time)
103fb23d3a8Smrg  -d            create directories instead of installing files.
104fb23d3a8Smrg  -g GROUP      $chgrpprog installed files to GROUP.
105fb23d3a8Smrg  -m MODE       $chmodprog installed files to MODE.
106fb23d3a8Smrg  -o USER       $chownprog installed files to USER.
107fb23d3a8Smrg  -s            $stripprog installed files.
108fb23d3a8Smrg  -t DIRECTORY  install into DIRECTORY.
109fb23d3a8Smrg  -T            report an error if DSTFILE is a directory.
11064ce7165Smrg
11164ce7165SmrgEnvironment variables override the default commands:
112fb23d3a8Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
113fb23d3a8Smrg  RMPROG STRIPPROG
11464ce7165Smrg"
11564ce7165Smrg
11664ce7165Smrgwhile test $# -ne 0; do
11764ce7165Smrg  case $1 in
118fb23d3a8Smrg    -c) ;;
119fb23d3a8Smrg
120fb23d3a8Smrg    -C) copy_on_change=true;;
12164ce7165Smrg
122fb23d3a8Smrg    -d) dir_arg=true;;
12364ce7165Smrg
12464ce7165Smrg    -g) chgrpcmd="$chgrpprog $2"
12595fdd44cSmrg        shift;;
12664ce7165Smrg
12764ce7165Smrg    --help) echo "$usage"; exit $?;;
12864ce7165Smrg
12964ce7165Smrg    -m) mode=$2
13095fdd44cSmrg        case $mode in
13195fdd44cSmrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
13295fdd44cSmrg            echo "$0: invalid mode: $mode" >&2
13395fdd44cSmrg            exit 1;;
13495fdd44cSmrg        esac
13595fdd44cSmrg        shift;;
13664ce7165Smrg
13764ce7165Smrg    -o) chowncmd="$chownprog $2"
13895fdd44cSmrg        shift;;
13964ce7165Smrg
140fb23d3a8Smrg    -s) stripcmd=$stripprog;;
14164ce7165Smrg
14295fdd44cSmrg    -t)
14395fdd44cSmrg        is_target_a_directory=always
14495fdd44cSmrg        dst_arg=$2
14595fdd44cSmrg        # Protect names problematic for 'test' and other utilities.
14695fdd44cSmrg        case $dst_arg in
14795fdd44cSmrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
14895fdd44cSmrg        esac
14995fdd44cSmrg        shift;;
15064ce7165Smrg
15195fdd44cSmrg    -T) is_target_a_directory=never;;
15264ce7165Smrg
15364ce7165Smrg    --version) echo "$0 $scriptversion"; exit $?;;
15464ce7165Smrg
15595fdd44cSmrg    --) shift
15695fdd44cSmrg        break;;
15764ce7165Smrg
15895fdd44cSmrg    -*) echo "$0: invalid option: $1" >&2
15995fdd44cSmrg        exit 1;;
16064ce7165Smrg
16164ce7165Smrg    *)  break;;
16264ce7165Smrg  esac
163fb23d3a8Smrg  shift
16464ce7165Smrgdone
16564ce7165Smrg
16695fdd44cSmrg# We allow the use of options -d and -T together, by making -d
16795fdd44cSmrg# take the precedence; this is for compatibility with GNU install.
16895fdd44cSmrg
16995fdd44cSmrgif test -n "$dir_arg"; then
17095fdd44cSmrg  if test -n "$dst_arg"; then
17195fdd44cSmrg    echo "$0: target directory not allowed when installing a directory." >&2
17295fdd44cSmrg    exit 1
17395fdd44cSmrg  fi
17495fdd44cSmrgfi
17595fdd44cSmrg
176fb23d3a8Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
17764ce7165Smrg  # When -d is used, all remaining arguments are directories to create.
17864ce7165Smrg  # When -t is used, the destination is already specified.
17964ce7165Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
18064ce7165Smrg  for arg
18164ce7165Smrg  do
182fb23d3a8Smrg    if test -n "$dst_arg"; then
18364ce7165Smrg      # $@ is not empty: it contains at least $arg.
184fb23d3a8Smrg      set fnord "$@" "$dst_arg"
18564ce7165Smrg      shift # fnord
18664ce7165Smrg    fi
18764ce7165Smrg    shift # arg
188fb23d3a8Smrg    dst_arg=$arg
18981440437Smrg    # Protect names problematic for 'test' and other utilities.
19081440437Smrg    case $dst_arg in
19181440437Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
19281440437Smrg    esac
19364ce7165Smrg  done
19464ce7165Smrgfi
19564ce7165Smrg
19664ce7165Smrgif test $# -eq 0; then
19764ce7165Smrg  if test -z "$dir_arg"; then
19864ce7165Smrg    echo "$0: no input file specified." >&2
19964ce7165Smrg    exit 1
20064ce7165Smrg  fi
20181440437Smrg  # It's OK to call 'install-sh -d' without argument.
20264ce7165Smrg  # This can happen when creating conditional directories.
20364ce7165Smrg  exit 0
20464ce7165Smrgfi
20564ce7165Smrg
20695fdd44cSmrgif test -z "$dir_arg"; then
20795fdd44cSmrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
20895fdd44cSmrg    if test ! -d "$dst_arg"; then
20995fdd44cSmrg      echo "$0: $dst_arg: Is not a directory." >&2
21095fdd44cSmrg      exit 1
21195fdd44cSmrg    fi
21295fdd44cSmrg  fi
21395fdd44cSmrgfi
21495fdd44cSmrg
21564ce7165Smrgif test -z "$dir_arg"; then
21681440437Smrg  do_exit='(exit $ret); exit $ret'
21781440437Smrg  trap "ret=129; $do_exit" 1
21881440437Smrg  trap "ret=130; $do_exit" 2
21981440437Smrg  trap "ret=141; $do_exit" 13
22081440437Smrg  trap "ret=143; $do_exit" 15
22164ce7165Smrg
22264ce7165Smrg  # Set umask so as not to create temps with too-generous modes.
22364ce7165Smrg  # However, 'strip' requires both read and write access to temps.
22464ce7165Smrg  case $mode in
22564ce7165Smrg    # Optimize common cases.
22664ce7165Smrg    *644) cp_umask=133;;
22764ce7165Smrg    *755) cp_umask=22;;
22864ce7165Smrg
22964ce7165Smrg    *[0-7])
23064ce7165Smrg      if test -z "$stripcmd"; then
23195fdd44cSmrg        u_plus_rw=
23264ce7165Smrg      else
23395fdd44cSmrg        u_plus_rw='% 200'
23464ce7165Smrg      fi
23564ce7165Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
23664ce7165Smrg    *)
23764ce7165Smrg      if test -z "$stripcmd"; then
23895fdd44cSmrg        u_plus_rw=
23964ce7165Smrg      else
24095fdd44cSmrg        u_plus_rw=,u+rw
24164ce7165Smrg      fi
24264ce7165Smrg      cp_umask=$mode$u_plus_rw;;
24364ce7165Smrg  esac
24464ce7165Smrgfi
24564ce7165Smrg
24664ce7165Smrgfor src
24764ce7165Smrgdo
24881440437Smrg  # Protect names problematic for 'test' and other utilities.
24964ce7165Smrg  case $src in
25081440437Smrg    -* | [=\(\)!]) src=./$src;;
25164ce7165Smrg  esac
25264ce7165Smrg
25364ce7165Smrg  if test -n "$dir_arg"; then
25464ce7165Smrg    dst=$src
25564ce7165Smrg    dstdir=$dst
25664ce7165Smrg    test -d "$dstdir"
25764ce7165Smrg    dstdir_status=$?
25864ce7165Smrg  else
25964ce7165Smrg
26064ce7165Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
26164ce7165Smrg    # might cause directories to be created, which would be especially bad
26264ce7165Smrg    # if $src (and thus $dsttmp) contains '*'.
26364ce7165Smrg    if test ! -f "$src" && test ! -d "$src"; then
26464ce7165Smrg      echo "$0: $src does not exist." >&2
26564ce7165Smrg      exit 1
26664ce7165Smrg    fi
26764ce7165Smrg
268fb23d3a8Smrg    if test -z "$dst_arg"; then
26964ce7165Smrg      echo "$0: no destination specified." >&2
27064ce7165Smrg      exit 1
27164ce7165Smrg    fi
272fb23d3a8Smrg    dst=$dst_arg
27364ce7165Smrg
27464ce7165Smrg    # If destination is a directory, append the input filename; won't work
27564ce7165Smrg    # if double slashes aren't ignored.
27664ce7165Smrg    if test -d "$dst"; then
27795fdd44cSmrg      if test "$is_target_a_directory" = never; then
27895fdd44cSmrg        echo "$0: $dst_arg: Is a directory" >&2
27995fdd44cSmrg        exit 1
28064ce7165Smrg      fi
28164ce7165Smrg      dstdir=$dst
28264ce7165Smrg      dst=$dstdir/`basename "$src"`
28364ce7165Smrg      dstdir_status=0
28464ce7165Smrg    else
28595fdd44cSmrg      dstdir=`dirname "$dst"`
28664ce7165Smrg      test -d "$dstdir"
28764ce7165Smrg      dstdir_status=$?
28864ce7165Smrg    fi
28964ce7165Smrg  fi
29064ce7165Smrg
29164ce7165Smrg  obsolete_mkdir_used=false
29264ce7165Smrg
29364ce7165Smrg  if test $dstdir_status != 0; then
29464ce7165Smrg    case $posix_mkdir in
29564ce7165Smrg      '')
29695fdd44cSmrg        # Create intermediate dirs using mode 755 as modified by the umask.
29795fdd44cSmrg        # This is like FreeBSD 'install' as of 1997-10-28.
29895fdd44cSmrg        umask=`umask`
29995fdd44cSmrg        case $stripcmd.$umask in
30095fdd44cSmrg          # Optimize common cases.
30195fdd44cSmrg          *[2367][2367]) mkdir_umask=$umask;;
30295fdd44cSmrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
30395fdd44cSmrg
30495fdd44cSmrg          *[0-7])
30595fdd44cSmrg            mkdir_umask=`expr $umask + 22 \
30695fdd44cSmrg              - $umask % 100 % 40 + $umask % 20 \
30795fdd44cSmrg              - $umask % 10 % 4 + $umask % 2
30895fdd44cSmrg            `;;
30995fdd44cSmrg          *) mkdir_umask=$umask,go-w;;
31095fdd44cSmrg        esac
31195fdd44cSmrg
31295fdd44cSmrg        # With -d, create the new directory with the user-specified mode.
31395fdd44cSmrg        # Otherwise, rely on $mkdir_umask.
31495fdd44cSmrg        if test -n "$dir_arg"; then
31595fdd44cSmrg          mkdir_mode=-m$mode
31695fdd44cSmrg        else
31795fdd44cSmrg          mkdir_mode=
31895fdd44cSmrg        fi
31995fdd44cSmrg
32095fdd44cSmrg        posix_mkdir=false
32195fdd44cSmrg        case $umask in
32295fdd44cSmrg          *[123567][0-7][0-7])
32395fdd44cSmrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
32495fdd44cSmrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
32595fdd44cSmrg            ;;
32695fdd44cSmrg          *)
32795fdd44cSmrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
32895fdd44cSmrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
32995fdd44cSmrg
33095fdd44cSmrg            if (umask $mkdir_umask &&
33195fdd44cSmrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
33295fdd44cSmrg            then
33395fdd44cSmrg              if test -z "$dir_arg" || {
33495fdd44cSmrg                   # Check for POSIX incompatibilities with -m.
33595fdd44cSmrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
33695fdd44cSmrg                   # other-writable bit of parent directory when it shouldn't.
33795fdd44cSmrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
33895fdd44cSmrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
33995fdd44cSmrg                   case $ls_ld_tmpdir in
34095fdd44cSmrg                     d????-?r-*) different_mode=700;;
34195fdd44cSmrg                     d????-?--*) different_mode=755;;
34295fdd44cSmrg                     *) false;;
34395fdd44cSmrg                   esac &&
34495fdd44cSmrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
34595fdd44cSmrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
34695fdd44cSmrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
34795fdd44cSmrg                   }
34895fdd44cSmrg                 }
34995fdd44cSmrg              then posix_mkdir=:
35095fdd44cSmrg              fi
35195fdd44cSmrg              rmdir "$tmpdir/d" "$tmpdir"
35295fdd44cSmrg            else
35395fdd44cSmrg              # Remove any dirs left behind by ancient mkdir implementations.
35495fdd44cSmrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
35595fdd44cSmrg            fi
35695fdd44cSmrg            trap '' 0;;
35795fdd44cSmrg        esac;;
35864ce7165Smrg    esac
35964ce7165Smrg
36064ce7165Smrg    if
36164ce7165Smrg      $posix_mkdir && (
36295fdd44cSmrg        umask $mkdir_umask &&
36395fdd44cSmrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
36464ce7165Smrg      )
36564ce7165Smrg    then :
36664ce7165Smrg    else
36764ce7165Smrg
36864ce7165Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
36964ce7165Smrg      # or it failed possibly due to a race condition.  Create the
37064ce7165Smrg      # directory the slow way, step by step, checking for races as we go.
37164ce7165Smrg
37264ce7165Smrg      case $dstdir in
37395fdd44cSmrg        /*) prefix='/';;
37495fdd44cSmrg        [-=\(\)!]*) prefix='./';;
37595fdd44cSmrg        *)  prefix='';;
37664ce7165Smrg      esac
37764ce7165Smrg
37864ce7165Smrg      oIFS=$IFS
37964ce7165Smrg      IFS=/
38095fdd44cSmrg      set -f
38164ce7165Smrg      set fnord $dstdir
38264ce7165Smrg      shift
38395fdd44cSmrg      set +f
38464ce7165Smrg      IFS=$oIFS
38564ce7165Smrg
38664ce7165Smrg      prefixes=
38764ce7165Smrg
38864ce7165Smrg      for d
38964ce7165Smrg      do
39095fdd44cSmrg        test X"$d" = X && continue
39195fdd44cSmrg
39295fdd44cSmrg        prefix=$prefix$d
39395fdd44cSmrg        if test -d "$prefix"; then
39495fdd44cSmrg          prefixes=
39595fdd44cSmrg        else
39695fdd44cSmrg          if $posix_mkdir; then
39795fdd44cSmrg            (umask=$mkdir_umask &&
39895fdd44cSmrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
39995fdd44cSmrg            # Don't fail if two instances are running concurrently.
40095fdd44cSmrg            test -d "$prefix" || exit 1
40195fdd44cSmrg          else
40295fdd44cSmrg            case $prefix in
40395fdd44cSmrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
40495fdd44cSmrg              *) qprefix=$prefix;;
40595fdd44cSmrg            esac
40695fdd44cSmrg            prefixes="$prefixes '$qprefix'"
40795fdd44cSmrg          fi
40895fdd44cSmrg        fi
40995fdd44cSmrg        prefix=$prefix/
41064ce7165Smrg      done
41164ce7165Smrg
41264ce7165Smrg      if test -n "$prefixes"; then
41395fdd44cSmrg        # Don't fail if two instances are running concurrently.
41495fdd44cSmrg        (umask $mkdir_umask &&
41595fdd44cSmrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
41695fdd44cSmrg          test -d "$dstdir" || exit 1
41795fdd44cSmrg        obsolete_mkdir_used=true
41864ce7165Smrg      fi
41964ce7165Smrg    fi
42064ce7165Smrg  fi
42164ce7165Smrg
42264ce7165Smrg  if test -n "$dir_arg"; then
42364ce7165Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
42464ce7165Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
42564ce7165Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
42664ce7165Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
42764ce7165Smrg  else
42864ce7165Smrg
42964ce7165Smrg    # Make a couple of temp file names in the proper directory.
43064ce7165Smrg    dsttmp=$dstdir/_inst.$$_
43164ce7165Smrg    rmtmp=$dstdir/_rm.$$_
43264ce7165Smrg
43364ce7165Smrg    # Trap to clean up those temp files at exit.
43464ce7165Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
43564ce7165Smrg
43664ce7165Smrg    # Copy the file name to the temp name.
43764ce7165Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
43864ce7165Smrg
43964ce7165Smrg    # and set any options; do chmod last to preserve setuid bits.
44064ce7165Smrg    #
44164ce7165Smrg    # If any of these fail, we abort the whole thing.  If we want to
44264ce7165Smrg    # ignore errors from any of these, just make sure not to ignore
44364ce7165Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
44464ce7165Smrg    #
445fb23d3a8Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
446fb23d3a8Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
447fb23d3a8Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
448fb23d3a8Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
449fb23d3a8Smrg
450fb23d3a8Smrg    # If -C, don't bother to copy if it wouldn't change the file.
451fb23d3a8Smrg    if $copy_on_change &&
45295fdd44cSmrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
45395fdd44cSmrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
45495fdd44cSmrg       set -f &&
455fb23d3a8Smrg       set X $old && old=:$2:$4:$5:$6 &&
456fb23d3a8Smrg       set X $new && new=:$2:$4:$5:$6 &&
45795fdd44cSmrg       set +f &&
458fb23d3a8Smrg       test "$old" = "$new" &&
459fb23d3a8Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
460fb23d3a8Smrg    then
461fb23d3a8Smrg      rm -f "$dsttmp"
462fb23d3a8Smrg    else
463fb23d3a8Smrg      # Rename the file to the real destination.
464fb23d3a8Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
465fb23d3a8Smrg
466fb23d3a8Smrg      # The rename failed, perhaps because mv can't rename something else
467fb23d3a8Smrg      # to itself, or perhaps because mv is so ancient that it does not
468fb23d3a8Smrg      # support -f.
469fb23d3a8Smrg      {
47095fdd44cSmrg        # Now remove or move aside any old file at destination location.
47195fdd44cSmrg        # We try this two ways since rm can't unlink itself on some
47295fdd44cSmrg        # systems and the destination file might be busy for other
47395fdd44cSmrg        # reasons.  In this case, the final cleanup might fail but the new
47495fdd44cSmrg        # file should still install successfully.
47595fdd44cSmrg        {
47695fdd44cSmrg          test ! -f "$dst" ||
47795fdd44cSmrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
47895fdd44cSmrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
47995fdd44cSmrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
48095fdd44cSmrg          } ||
48195fdd44cSmrg          { echo "$0: cannot unlink or rename $dst" >&2
48295fdd44cSmrg            (exit 1); exit 1
48395fdd44cSmrg          }
48495fdd44cSmrg        } &&
48595fdd44cSmrg
48695fdd44cSmrg        # Now rename the file to the real destination.
48795fdd44cSmrg        $doit $mvcmd "$dsttmp" "$dst"
488fb23d3a8Smrg      }
489fb23d3a8Smrg    fi || exit 1
49064ce7165Smrg
49164ce7165Smrg    trap '' 0
49264ce7165Smrg  fi
49364ce7165Smrgdone
49464ce7165Smrg
49564ce7165Smrg# Local variables:
49664ce7165Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
49764ce7165Smrg# time-stamp-start: "scriptversion="
49864ce7165Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
499fb23d3a8Smrg# time-stamp-time-zone: "UTC"
500fb23d3a8Smrg# time-stamp-end: "; # UTC"
50164ce7165Smrg# End:
502