install-sh revision aef24d58
1c9e2be55Smrg#!/bin/sh
2c9e2be55Smrg# install - install a program, script, or datafile
3e2264b6dSmrg
4aef24d58Smrgscriptversion=2013-12-25.23; # UTC
5e2264b6dSmrg
6c9e2be55Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
7c9e2be55Smrg# later released in X11R6 (xc/config/util/install.sh) with the
8c9e2be55Smrg# following copyright and license.
9c9e2be55Smrg#
10c9e2be55Smrg# Copyright (C) 1994 X Consortium
11c9e2be55Smrg#
12c9e2be55Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
13c9e2be55Smrg# of this software and associated documentation files (the "Software"), to
14c9e2be55Smrg# deal in the Software without restriction, including without limitation the
15c9e2be55Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
16c9e2be55Smrg# sell copies of the Software, and to permit persons to whom the Software is
17c9e2be55Smrg# furnished to do so, subject to the following conditions:
18c9e2be55Smrg#
19c9e2be55Smrg# The above copyright notice and this permission notice shall be included in
20c9e2be55Smrg# all copies or substantial portions of the Software.
21c9e2be55Smrg#
22c9e2be55Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23c9e2be55Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24c9e2be55Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
25c9e2be55Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
26c9e2be55Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
27c9e2be55Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28c9e2be55Smrg#
29c9e2be55Smrg# Except as contained in this notice, the name of the X Consortium shall not
30c9e2be55Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
31c9e2be55Smrg# ings in this Software without prior written authorization from the X Consor-
32c9e2be55Smrg# tium.
33c9e2be55Smrg#
34c9e2be55Smrg#
35c9e2be55Smrg# FSF changes to this file are in the public domain.
36c9e2be55Smrg#
37c9e2be55Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38aef24d58Smrg# 'make' implicit rules from creating a file called install from it
39c9e2be55Smrg# when there is no Makefile.
40c9e2be55Smrg#
41c9e2be55Smrg# This script is compatible with the BSD install script, but was written
42e2264b6dSmrg# from scratch.
43c9e2be55Smrg
44aef24d58Smrgtab='	'
45e2264b6dSmrgnl='
46e2264b6dSmrg'
47aef24d58SmrgIFS=" $tab$nl"
48c9e2be55Smrg
49aef24d58Smrg# Set DOITPROG to "echo" to test this script.
50c9e2be55Smrg
51e2264b6dSmrgdoit=${DOITPROG-}
52aef24d58Smrgdoit_exec=${doit:-exec}
53c9e2be55Smrg
54e2264b6dSmrg# Put in absolute file names if you don't have them in your path;
55e2264b6dSmrg# or use environment vars.
56e2264b6dSmrg
57e2264b6dSmrgchgrpprog=${CHGRPPROG-chgrp}
58e2264b6dSmrgchmodprog=${CHMODPROG-chmod}
59e2264b6dSmrgchownprog=${CHOWNPROG-chown}
60e2264b6dSmrgcmpprog=${CMPPROG-cmp}
61e2264b6dSmrgcpprog=${CPPROG-cp}
62e2264b6dSmrgmkdirprog=${MKDIRPROG-mkdir}
63e2264b6dSmrgmvprog=${MVPROG-mv}
64e2264b6dSmrgrmprog=${RMPROG-rm}
65e2264b6dSmrgstripprog=${STRIPPROG-strip}
66e2264b6dSmrg
67e2264b6dSmrgposix_mkdir=
68e2264b6dSmrg
69e2264b6dSmrg# Desired mode of installed file.
70e2264b6dSmrgmode=0755
71e2264b6dSmrg
72e2264b6dSmrgchgrpcmd=
73e2264b6dSmrgchmodcmd=$chmodprog
74e2264b6dSmrgchowncmd=
75e2264b6dSmrgmvcmd=$mvprog
76e2264b6dSmrgrmcmd="$rmprog -f"
77e2264b6dSmrgstripcmd=
78c9e2be55Smrg
79e2264b6dSmrgsrc=
80e2264b6dSmrgdst=
81e2264b6dSmrgdir_arg=
82e2264b6dSmrgdst_arg=
83c9e2be55Smrg
84e2264b6dSmrgcopy_on_change=false
85aef24d58Smrgis_target_a_directory=possibly
86c9e2be55Smrg
87e2264b6dSmrgusage="\
88e2264b6dSmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
89e2264b6dSmrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
90e2264b6dSmrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
91e2264b6dSmrg   or: $0 [OPTION]... -d DIRECTORIES...
92c9e2be55Smrg
93e2264b6dSmrgIn the 1st form, copy SRCFILE to DSTFILE.
94e2264b6dSmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
95e2264b6dSmrgIn the 4th, create DIRECTORIES.
96c9e2be55Smrg
97e2264b6dSmrgOptions:
98e2264b6dSmrg     --help     display this help and exit.
99e2264b6dSmrg     --version  display version info and exit.
100c9e2be55Smrg
101e2264b6dSmrg  -c            (ignored)
102e2264b6dSmrg  -C            install only if different (preserve the last data modification time)
103e2264b6dSmrg  -d            create directories instead of installing files.
104e2264b6dSmrg  -g GROUP      $chgrpprog installed files to GROUP.
105e2264b6dSmrg  -m MODE       $chmodprog installed files to MODE.
106e2264b6dSmrg  -o USER       $chownprog installed files to USER.
107e2264b6dSmrg  -s            $stripprog installed files.
108e2264b6dSmrg  -t DIRECTORY  install into DIRECTORY.
109e2264b6dSmrg  -T            report an error if DSTFILE is a directory.
110c9e2be55Smrg
111e2264b6dSmrgEnvironment variables override the default commands:
112e2264b6dSmrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
113e2264b6dSmrg  RMPROG STRIPPROG
114e2264b6dSmrg"
115c9e2be55Smrg
116e2264b6dSmrgwhile test $# -ne 0; do
117e2264b6dSmrg  case $1 in
118e2264b6dSmrg    -c) ;;
119c9e2be55Smrg
120e2264b6dSmrg    -C) copy_on_change=true;;
121c9e2be55Smrg
122e2264b6dSmrg    -d) dir_arg=true;;
123c9e2be55Smrg
124e2264b6dSmrg    -g) chgrpcmd="$chgrpprog $2"
125aef24d58Smrg        shift;;
126c9e2be55Smrg
127e2264b6dSmrg    --help) echo "$usage"; exit $?;;
128c9e2be55Smrg
129e2264b6dSmrg    -m) mode=$2
130aef24d58Smrg        case $mode in
131aef24d58Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132aef24d58Smrg            echo "$0: invalid mode: $mode" >&2
133aef24d58Smrg            exit 1;;
134aef24d58Smrg        esac
135aef24d58Smrg        shift;;
136c9e2be55Smrg
137e2264b6dSmrg    -o) chowncmd="$chownprog $2"
138aef24d58Smrg        shift;;
139c9e2be55Smrg
140e2264b6dSmrg    -s) stripcmd=$stripprog;;
141c9e2be55Smrg
142aef24d58Smrg    -t)
143aef24d58Smrg        is_target_a_directory=always
144aef24d58Smrg        dst_arg=$2
145aef24d58Smrg        # Protect names problematic for 'test' and other utilities.
146aef24d58Smrg        case $dst_arg in
147aef24d58Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148aef24d58Smrg        esac
149aef24d58Smrg        shift;;
150c9e2be55Smrg
151aef24d58Smrg    -T) is_target_a_directory=never;;
152c9e2be55Smrg
153e2264b6dSmrg    --version) echo "$0 $scriptversion"; exit $?;;
154c9e2be55Smrg
155aef24d58Smrg    --) shift
156aef24d58Smrg        break;;
157c9e2be55Smrg
158aef24d58Smrg    -*) echo "$0: invalid option: $1" >&2
159aef24d58Smrg        exit 1;;
160c9e2be55Smrg
161e2264b6dSmrg    *)  break;;
162e2264b6dSmrg  esac
163e2264b6dSmrg  shift
164e2264b6dSmrgdone
165c9e2be55Smrg
166aef24d58Smrg# We allow the use of options -d and -T together, by making -d
167aef24d58Smrg# take the precedence; this is for compatibility with GNU install.
168aef24d58Smrg
169aef24d58Smrgif test -n "$dir_arg"; then
170aef24d58Smrg  if test -n "$dst_arg"; then
171aef24d58Smrg    echo "$0: target directory not allowed when installing a directory." >&2
172aef24d58Smrg    exit 1
173aef24d58Smrg  fi
174aef24d58Smrgfi
175aef24d58Smrg
176e2264b6dSmrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
177e2264b6dSmrg  # When -d is used, all remaining arguments are directories to create.
178e2264b6dSmrg  # When -t is used, the destination is already specified.
179e2264b6dSmrg  # Otherwise, the last argument is the destination.  Remove it from $@.
180e2264b6dSmrg  for arg
181e2264b6dSmrg  do
182e2264b6dSmrg    if test -n "$dst_arg"; then
183e2264b6dSmrg      # $@ is not empty: it contains at least $arg.
184e2264b6dSmrg      set fnord "$@" "$dst_arg"
185e2264b6dSmrg      shift # fnord
186e2264b6dSmrg    fi
187e2264b6dSmrg    shift # arg
188e2264b6dSmrg    dst_arg=$arg
189aef24d58Smrg    # Protect names problematic for 'test' and other utilities.
190aef24d58Smrg    case $dst_arg in
191aef24d58Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192aef24d58Smrg    esac
193e2264b6dSmrg  done
194e2264b6dSmrgfi
195c9e2be55Smrg
196e2264b6dSmrgif test $# -eq 0; then
197e2264b6dSmrg  if test -z "$dir_arg"; then
198e2264b6dSmrg    echo "$0: no input file specified." >&2
199e2264b6dSmrg    exit 1
200e2264b6dSmrg  fi
201aef24d58Smrg  # It's OK to call 'install-sh -d' without argument.
202e2264b6dSmrg  # This can happen when creating conditional directories.
203e2264b6dSmrg  exit 0
204e2264b6dSmrgfi
205c9e2be55Smrg
206e2264b6dSmrgif test -z "$dir_arg"; then
207aef24d58Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208aef24d58Smrg    if test ! -d "$dst_arg"; then
209aef24d58Smrg      echo "$0: $dst_arg: Is not a directory." >&2
210aef24d58Smrg      exit 1
211aef24d58Smrg    fi
212aef24d58Smrg  fi
213aef24d58Smrgfi
214aef24d58Smrg
215aef24d58Smrgif test -z "$dir_arg"; then
216aef24d58Smrg  do_exit='(exit $ret); exit $ret'
217aef24d58Smrg  trap "ret=129; $do_exit" 1
218aef24d58Smrg  trap "ret=130; $do_exit" 2
219aef24d58Smrg  trap "ret=141; $do_exit" 13
220aef24d58Smrg  trap "ret=143; $do_exit" 15
221e2264b6dSmrg
222e2264b6dSmrg  # Set umask so as not to create temps with too-generous modes.
223e2264b6dSmrg  # However, 'strip' requires both read and write access to temps.
224e2264b6dSmrg  case $mode in
225e2264b6dSmrg    # Optimize common cases.
226e2264b6dSmrg    *644) cp_umask=133;;
227e2264b6dSmrg    *755) cp_umask=22;;
228e2264b6dSmrg
229e2264b6dSmrg    *[0-7])
230e2264b6dSmrg      if test -z "$stripcmd"; then
231aef24d58Smrg        u_plus_rw=
232e2264b6dSmrg      else
233aef24d58Smrg        u_plus_rw='% 200'
234e2264b6dSmrg      fi
235e2264b6dSmrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
236e2264b6dSmrg    *)
237e2264b6dSmrg      if test -z "$stripcmd"; then
238aef24d58Smrg        u_plus_rw=
239e2264b6dSmrg      else
240aef24d58Smrg        u_plus_rw=,u+rw
241e2264b6dSmrg      fi
242e2264b6dSmrg      cp_umask=$mode$u_plus_rw;;
243e2264b6dSmrg  esac
244e2264b6dSmrgfi
245c9e2be55Smrg
246e2264b6dSmrgfor src
247e2264b6dSmrgdo
248aef24d58Smrg  # Protect names problematic for 'test' and other utilities.
249e2264b6dSmrg  case $src in
250aef24d58Smrg    -* | [=\(\)!]) src=./$src;;
251e2264b6dSmrg  esac
252e2264b6dSmrg
253e2264b6dSmrg  if test -n "$dir_arg"; then
254e2264b6dSmrg    dst=$src
255e2264b6dSmrg    dstdir=$dst
256e2264b6dSmrg    test -d "$dstdir"
257e2264b6dSmrg    dstdir_status=$?
258e2264b6dSmrg  else
259e2264b6dSmrg
260e2264b6dSmrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
261e2264b6dSmrg    # might cause directories to be created, which would be especially bad
262e2264b6dSmrg    # if $src (and thus $dsttmp) contains '*'.
263e2264b6dSmrg    if test ! -f "$src" && test ! -d "$src"; then
264e2264b6dSmrg      echo "$0: $src does not exist." >&2
265e2264b6dSmrg      exit 1
266e2264b6dSmrg    fi
267e2264b6dSmrg
268e2264b6dSmrg    if test -z "$dst_arg"; then
269e2264b6dSmrg      echo "$0: no destination specified." >&2
270e2264b6dSmrg      exit 1
271e2264b6dSmrg    fi
272e2264b6dSmrg    dst=$dst_arg
273c9e2be55Smrg
274e2264b6dSmrg    # If destination is a directory, append the input filename; won't work
275e2264b6dSmrg    # if double slashes aren't ignored.
276e2264b6dSmrg    if test -d "$dst"; then
277aef24d58Smrg      if test "$is_target_a_directory" = never; then
278aef24d58Smrg        echo "$0: $dst_arg: Is a directory" >&2
279aef24d58Smrg        exit 1
280e2264b6dSmrg      fi
281e2264b6dSmrg      dstdir=$dst
282e2264b6dSmrg      dst=$dstdir/`basename "$src"`
283e2264b6dSmrg      dstdir_status=0
284e2264b6dSmrg    else
285aef24d58Smrg      dstdir=`dirname "$dst"`
286e2264b6dSmrg      test -d "$dstdir"
287e2264b6dSmrg      dstdir_status=$?
288e2264b6dSmrg    fi
289e2264b6dSmrg  fi
290e2264b6dSmrg
291e2264b6dSmrg  obsolete_mkdir_used=false
292e2264b6dSmrg
293e2264b6dSmrg  if test $dstdir_status != 0; then
294e2264b6dSmrg    case $posix_mkdir in
295e2264b6dSmrg      '')
296aef24d58Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
297aef24d58Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
298aef24d58Smrg        umask=`umask`
299aef24d58Smrg        case $stripcmd.$umask in
300aef24d58Smrg          # Optimize common cases.
301aef24d58Smrg          *[2367][2367]) mkdir_umask=$umask;;
302aef24d58Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
303aef24d58Smrg
304aef24d58Smrg          *[0-7])
305aef24d58Smrg            mkdir_umask=`expr $umask + 22 \
306aef24d58Smrg              - $umask % 100 % 40 + $umask % 20 \
307aef24d58Smrg              - $umask % 10 % 4 + $umask % 2
308aef24d58Smrg            `;;
309aef24d58Smrg          *) mkdir_umask=$umask,go-w;;
310aef24d58Smrg        esac
311aef24d58Smrg
312aef24d58Smrg        # With -d, create the new directory with the user-specified mode.
313aef24d58Smrg        # Otherwise, rely on $mkdir_umask.
314aef24d58Smrg        if test -n "$dir_arg"; then
315aef24d58Smrg          mkdir_mode=-m$mode
316aef24d58Smrg        else
317aef24d58Smrg          mkdir_mode=
318aef24d58Smrg        fi
319aef24d58Smrg
320aef24d58Smrg        posix_mkdir=false
321aef24d58Smrg        case $umask in
322aef24d58Smrg          *[123567][0-7][0-7])
323aef24d58Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
324aef24d58Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
325aef24d58Smrg            ;;
326aef24d58Smrg          *)
327aef24d58Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
328aef24d58Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
329aef24d58Smrg
330aef24d58Smrg            if (umask $mkdir_umask &&
331aef24d58Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
332aef24d58Smrg            then
333aef24d58Smrg              if test -z "$dir_arg" || {
334aef24d58Smrg                   # Check for POSIX incompatibilities with -m.
335aef24d58Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
336aef24d58Smrg                   # other-writable bit of parent directory when it shouldn't.
337aef24d58Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
338aef24d58Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
339aef24d58Smrg                   case $ls_ld_tmpdir in
340aef24d58Smrg                     d????-?r-*) different_mode=700;;
341aef24d58Smrg                     d????-?--*) different_mode=755;;
342aef24d58Smrg                     *) false;;
343aef24d58Smrg                   esac &&
344aef24d58Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
345aef24d58Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
346aef24d58Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
347aef24d58Smrg                   }
348aef24d58Smrg                 }
349aef24d58Smrg              then posix_mkdir=:
350aef24d58Smrg              fi
351aef24d58Smrg              rmdir "$tmpdir/d" "$tmpdir"
352aef24d58Smrg            else
353aef24d58Smrg              # Remove any dirs left behind by ancient mkdir implementations.
354aef24d58Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
355aef24d58Smrg            fi
356aef24d58Smrg            trap '' 0;;
357aef24d58Smrg        esac;;
358e2264b6dSmrg    esac
359c9e2be55Smrg
360e2264b6dSmrg    if
361e2264b6dSmrg      $posix_mkdir && (
362aef24d58Smrg        umask $mkdir_umask &&
363aef24d58Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
364e2264b6dSmrg      )
365e2264b6dSmrg    then :
366e2264b6dSmrg    else
367e2264b6dSmrg
368e2264b6dSmrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
369e2264b6dSmrg      # or it failed possibly due to a race condition.  Create the
370e2264b6dSmrg      # directory the slow way, step by step, checking for races as we go.
371e2264b6dSmrg
372e2264b6dSmrg      case $dstdir in
373aef24d58Smrg        /*) prefix='/';;
374aef24d58Smrg        [-=\(\)!]*) prefix='./';;
375aef24d58Smrg        *)  prefix='';;
376e2264b6dSmrg      esac
377e2264b6dSmrg
378e2264b6dSmrg      oIFS=$IFS
379e2264b6dSmrg      IFS=/
380aef24d58Smrg      set -f
381e2264b6dSmrg      set fnord $dstdir
382e2264b6dSmrg      shift
383aef24d58Smrg      set +f
384e2264b6dSmrg      IFS=$oIFS
385e2264b6dSmrg
386e2264b6dSmrg      prefixes=
387e2264b6dSmrg
388e2264b6dSmrg      for d
389e2264b6dSmrg      do
390aef24d58Smrg        test X"$d" = X && continue
391aef24d58Smrg
392aef24d58Smrg        prefix=$prefix$d
393aef24d58Smrg        if test -d "$prefix"; then
394aef24d58Smrg          prefixes=
395aef24d58Smrg        else
396aef24d58Smrg          if $posix_mkdir; then
397aef24d58Smrg            (umask=$mkdir_umask &&
398aef24d58Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
399aef24d58Smrg            # Don't fail if two instances are running concurrently.
400aef24d58Smrg            test -d "$prefix" || exit 1
401aef24d58Smrg          else
402aef24d58Smrg            case $prefix in
403aef24d58Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
404aef24d58Smrg              *) qprefix=$prefix;;
405aef24d58Smrg            esac
406aef24d58Smrg            prefixes="$prefixes '$qprefix'"
407aef24d58Smrg          fi
408aef24d58Smrg        fi
409aef24d58Smrg        prefix=$prefix/
410e2264b6dSmrg      done
411e2264b6dSmrg
412e2264b6dSmrg      if test -n "$prefixes"; then
413aef24d58Smrg        # Don't fail if two instances are running concurrently.
414aef24d58Smrg        (umask $mkdir_umask &&
415aef24d58Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
416aef24d58Smrg          test -d "$dstdir" || exit 1
417aef24d58Smrg        obsolete_mkdir_used=true
418e2264b6dSmrg      fi
419e2264b6dSmrg    fi
420e2264b6dSmrg  fi
421e2264b6dSmrg
422e2264b6dSmrg  if test -n "$dir_arg"; then
423e2264b6dSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
424e2264b6dSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
425e2264b6dSmrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
426e2264b6dSmrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
427e2264b6dSmrg  else
428e2264b6dSmrg
429e2264b6dSmrg    # Make a couple of temp file names in the proper directory.
430e2264b6dSmrg    dsttmp=$dstdir/_inst.$$_
431e2264b6dSmrg    rmtmp=$dstdir/_rm.$$_
432e2264b6dSmrg
433e2264b6dSmrg    # Trap to clean up those temp files at exit.
434e2264b6dSmrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
435e2264b6dSmrg
436e2264b6dSmrg    # Copy the file name to the temp name.
437e2264b6dSmrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
438e2264b6dSmrg
439e2264b6dSmrg    # and set any options; do chmod last to preserve setuid bits.
440e2264b6dSmrg    #
441e2264b6dSmrg    # If any of these fail, we abort the whole thing.  If we want to
442e2264b6dSmrg    # ignore errors from any of these, just make sure not to ignore
443e2264b6dSmrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
444e2264b6dSmrg    #
445e2264b6dSmrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
446e2264b6dSmrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
447e2264b6dSmrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
448e2264b6dSmrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
449e2264b6dSmrg
450e2264b6dSmrg    # If -C, don't bother to copy if it wouldn't change the file.
451e2264b6dSmrg    if $copy_on_change &&
452aef24d58Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
453aef24d58Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
454aef24d58Smrg       set -f &&
455e2264b6dSmrg       set X $old && old=:$2:$4:$5:$6 &&
456e2264b6dSmrg       set X $new && new=:$2:$4:$5:$6 &&
457aef24d58Smrg       set +f &&
458e2264b6dSmrg       test "$old" = "$new" &&
459e2264b6dSmrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
460e2264b6dSmrg    then
461e2264b6dSmrg      rm -f "$dsttmp"
462e2264b6dSmrg    else
463e2264b6dSmrg      # Rename the file to the real destination.
464e2264b6dSmrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
465e2264b6dSmrg
466e2264b6dSmrg      # The rename failed, perhaps because mv can't rename something else
467e2264b6dSmrg      # to itself, or perhaps because mv is so ancient that it does not
468e2264b6dSmrg      # support -f.
469e2264b6dSmrg      {
470aef24d58Smrg        # Now remove or move aside any old file at destination location.
471aef24d58Smrg        # We try this two ways since rm can't unlink itself on some
472aef24d58Smrg        # systems and the destination file might be busy for other
473aef24d58Smrg        # reasons.  In this case, the final cleanup might fail but the new
474aef24d58Smrg        # file should still install successfully.
475aef24d58Smrg        {
476aef24d58Smrg          test ! -f "$dst" ||
477aef24d58Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
478aef24d58Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
479aef24d58Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
480aef24d58Smrg          } ||
481aef24d58Smrg          { echo "$0: cannot unlink or rename $dst" >&2
482aef24d58Smrg            (exit 1); exit 1
483aef24d58Smrg          }
484aef24d58Smrg        } &&
485aef24d58Smrg
486aef24d58Smrg        # Now rename the file to the real destination.
487aef24d58Smrg        $doit $mvcmd "$dsttmp" "$dst"
488e2264b6dSmrg      }
489e2264b6dSmrg    fi || exit 1
490e2264b6dSmrg
491e2264b6dSmrg    trap '' 0
492e2264b6dSmrg  fi
493e2264b6dSmrgdone
494c9e2be55Smrg
495e2264b6dSmrg# Local variables:
496e2264b6dSmrg# eval: (add-hook 'write-file-hooks 'time-stamp)
497e2264b6dSmrg# time-stamp-start: "scriptversion="
498e2264b6dSmrg# time-stamp-format: "%:y-%02m-%02d.%02H"
499e2264b6dSmrg# time-stamp-time-zone: "UTC"
500e2264b6dSmrg# time-stamp-end: "; # UTC"
501e2264b6dSmrg# End:
502