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