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