install-sh revision 376c9fa8
19027f4a0Smrg#!/bin/sh
29027f4a0Smrg# install - install a program, script, or datafile
39027f4a0Smrg
4376c9fa8Smrgscriptversion=2013-12-25.23; # UTC
59027f4a0Smrg
69027f4a0Smrg# This originates from X11R5 (mit/util/scripts/install.sh), which was
79027f4a0Smrg# later released in X11R6 (xc/config/util/install.sh) with the
89027f4a0Smrg# following copyright and license.
99027f4a0Smrg#
109027f4a0Smrg# Copyright (C) 1994 X Consortium
119027f4a0Smrg#
129027f4a0Smrg# Permission is hereby granted, free of charge, to any person obtaining a copy
139027f4a0Smrg# of this software and associated documentation files (the "Software"), to
149027f4a0Smrg# deal in the Software without restriction, including without limitation the
159027f4a0Smrg# rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
169027f4a0Smrg# sell copies of the Software, and to permit persons to whom the Software is
179027f4a0Smrg# furnished to do so, subject to the following conditions:
189027f4a0Smrg#
199027f4a0Smrg# The above copyright notice and this permission notice shall be included in
209027f4a0Smrg# all copies or substantial portions of the Software.
219027f4a0Smrg#
229027f4a0Smrg# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
239027f4a0Smrg# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
249027f4a0Smrg# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
259027f4a0Smrg# X CONSORTIUM BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN
269027f4a0Smrg# AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNEC-
279027f4a0Smrg# TION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
289027f4a0Smrg#
299027f4a0Smrg# Except as contained in this notice, the name of the X Consortium shall not
309027f4a0Smrg# be used in advertising or otherwise to promote the sale, use or other deal-
319027f4a0Smrg# ings in this Software without prior written authorization from the X Consor-
329027f4a0Smrg# tium.
339027f4a0Smrg#
349027f4a0Smrg#
359027f4a0Smrg# FSF changes to this file are in the public domain.
369027f4a0Smrg#
379027f4a0Smrg# Calling this script install-sh is preferred over install.sh, to prevent
38fe2ac8d7Smrg# 'make' implicit rules from creating a file called install from it
399027f4a0Smrg# when there is no Makefile.
409027f4a0Smrg#
419027f4a0Smrg# This script is compatible with the BSD install script, but was written
423a72f200Smrg# from scratch.
433a72f200Smrg
44376c9fa8Smrgtab='	'
453a72f200Smrgnl='
463a72f200Smrg'
47376c9fa8SmrgIFS=" $tab$nl"
489027f4a0Smrg
49376c9fa8Smrg# Set DOITPROG to "echo" to test this script.
509027f4a0Smrg
513a72f200Smrgdoit=${DOITPROG-}
52376c9fa8Smrgdoit_exec=${doit:-exec}
539027f4a0Smrg
543a72f200Smrg# Put in absolute file names if you don't have them in your path;
553a72f200Smrg# or use environment vars.
563a72f200Smrg
573a72f200Smrgchgrpprog=${CHGRPPROG-chgrp}
583a72f200Smrgchmodprog=${CHMODPROG-chmod}
593a72f200Smrgchownprog=${CHOWNPROG-chown}
603a72f200Smrgcmpprog=${CMPPROG-cmp}
613a72f200Smrgcpprog=${CPPROG-cp}
623a72f200Smrgmkdirprog=${MKDIRPROG-mkdir}
633a72f200Smrgmvprog=${MVPROG-mv}
643a72f200Smrgrmprog=${RMPROG-rm}
653a72f200Smrgstripprog=${STRIPPROG-strip}
663a72f200Smrg
673a72f200Smrgposix_mkdir=
683a72f200Smrg
693a72f200Smrg# Desired mode of installed file.
703a72f200Smrgmode=0755
719027f4a0Smrg
729027f4a0Smrgchgrpcmd=
733a72f200Smrgchmodcmd=$chmodprog
743a72f200Smrgchowncmd=
753a72f200Smrgmvcmd=$mvprog
769027f4a0Smrgrmcmd="$rmprog -f"
773a72f200Smrgstripcmd=
783a72f200Smrg
799027f4a0Smrgsrc=
809027f4a0Smrgdst=
819027f4a0Smrgdir_arg=
823a72f200Smrgdst_arg=
833a72f200Smrg
843a72f200Smrgcopy_on_change=false
85376c9fa8Smrgis_target_a_directory=possibly
869027f4a0Smrg
873a72f200Smrgusage="\
883a72f200SmrgUsage: $0 [OPTION]... [-T] SRCFILE DSTFILE
899027f4a0Smrg   or: $0 [OPTION]... SRCFILES... DIRECTORY
909027f4a0Smrg   or: $0 [OPTION]... -t DIRECTORY SRCFILES...
919027f4a0Smrg   or: $0 [OPTION]... -d DIRECTORIES...
929027f4a0Smrg
939027f4a0SmrgIn the 1st form, copy SRCFILE to DSTFILE.
949027f4a0SmrgIn the 2nd and 3rd, copy all SRCFILES to DIRECTORY.
959027f4a0SmrgIn the 4th, create DIRECTORIES.
969027f4a0Smrg
979027f4a0SmrgOptions:
983a72f200Smrg     --help     display this help and exit.
993a72f200Smrg     --version  display version info and exit.
1003a72f200Smrg
1013a72f200Smrg  -c            (ignored)
1023a72f200Smrg  -C            install only if different (preserve the last data modification time)
1033a72f200Smrg  -d            create directories instead of installing files.
1043a72f200Smrg  -g GROUP      $chgrpprog installed files to GROUP.
1053a72f200Smrg  -m MODE       $chmodprog installed files to MODE.
1063a72f200Smrg  -o USER       $chownprog installed files to USER.
1073a72f200Smrg  -s            $stripprog installed files.
1083a72f200Smrg  -t DIRECTORY  install into DIRECTORY.
1093a72f200Smrg  -T            report an error if DSTFILE is a directory.
1109027f4a0Smrg
1119027f4a0SmrgEnvironment variables override the default commands:
1123a72f200Smrg  CHGRPPROG CHMODPROG CHOWNPROG CMPPROG CPPROG MKDIRPROG MVPROG
1133a72f200Smrg  RMPROG STRIPPROG
1149027f4a0Smrg"
1159027f4a0Smrg
1163a72f200Smrgwhile test $# -ne 0; do
1179027f4a0Smrg  case $1 in
1183a72f200Smrg    -c) ;;
1193a72f200Smrg
1203a72f200Smrg    -C) copy_on_change=true;;
1219027f4a0Smrg
1223a72f200Smrg    -d) dir_arg=true;;
1239027f4a0Smrg
1249027f4a0Smrg    -g) chgrpcmd="$chgrpprog $2"
125376c9fa8Smrg        shift;;
1269027f4a0Smrg
1279027f4a0Smrg    --help) echo "$usage"; exit $?;;
1289027f4a0Smrg
1293a72f200Smrg    -m) mode=$2
130376c9fa8Smrg        case $mode in
131376c9fa8Smrg          *' '* | *"$tab"* | *"$nl"* | *'*'* | *'?'* | *'['*)
132376c9fa8Smrg            echo "$0: invalid mode: $mode" >&2
133376c9fa8Smrg            exit 1;;
134376c9fa8Smrg        esac
135376c9fa8Smrg        shift;;
1369027f4a0Smrg
1379027f4a0Smrg    -o) chowncmd="$chownprog $2"
138376c9fa8Smrg        shift;;
1399027f4a0Smrg
1403a72f200Smrg    -s) stripcmd=$stripprog;;
1419027f4a0Smrg
142376c9fa8Smrg    -t)
143376c9fa8Smrg        is_target_a_directory=always
144376c9fa8Smrg        dst_arg=$2
145376c9fa8Smrg        # Protect names problematic for 'test' and other utilities.
146376c9fa8Smrg        case $dst_arg in
147376c9fa8Smrg          -* | [=\(\)!]) dst_arg=./$dst_arg;;
148376c9fa8Smrg        esac
149376c9fa8Smrg        shift;;
1509027f4a0Smrg
151376c9fa8Smrg    -T) is_target_a_directory=never;;
1529027f4a0Smrg
1539027f4a0Smrg    --version) echo "$0 $scriptversion"; exit $?;;
1549027f4a0Smrg
155376c9fa8Smrg    --) shift
156376c9fa8Smrg        break;;
1573a72f200Smrg
158376c9fa8Smrg    -*) echo "$0: invalid option: $1" >&2
159376c9fa8Smrg        exit 1;;
1603a72f200Smrg
1613a72f200Smrg    *)  break;;
1629027f4a0Smrg  esac
1633a72f200Smrg  shift
1649027f4a0Smrgdone
1659027f4a0Smrg
166376c9fa8Smrg# We allow the use of options -d and -T together, by making -d
167376c9fa8Smrg# take the precedence; this is for compatibility with GNU install.
168376c9fa8Smrg
169376c9fa8Smrgif test -n "$dir_arg"; then
170376c9fa8Smrg  if test -n "$dst_arg"; then
171376c9fa8Smrg    echo "$0: target directory not allowed when installing a directory." >&2
172376c9fa8Smrg    exit 1
173376c9fa8Smrg  fi
174376c9fa8Smrgfi
175376c9fa8Smrg
1763a72f200Smrgif test $# -ne 0 && test -z "$dir_arg$dst_arg"; then
1773a72f200Smrg  # When -d is used, all remaining arguments are directories to create.
1783a72f200Smrg  # When -t is used, the destination is already specified.
1793a72f200Smrg  # Otherwise, the last argument is the destination.  Remove it from $@.
1803a72f200Smrg  for arg
1813a72f200Smrg  do
1823a72f200Smrg    if test -n "$dst_arg"; then
1833a72f200Smrg      # $@ is not empty: it contains at least $arg.
1843a72f200Smrg      set fnord "$@" "$dst_arg"
1853a72f200Smrg      shift # fnord
1863a72f200Smrg    fi
1873a72f200Smrg    shift # arg
1883a72f200Smrg    dst_arg=$arg
189fe2ac8d7Smrg    # Protect names problematic for 'test' and other utilities.
190fe2ac8d7Smrg    case $dst_arg in
191fe2ac8d7Smrg      -* | [=\(\)!]) dst_arg=./$dst_arg;;
192fe2ac8d7Smrg    esac
1933a72f200Smrg  done
1943a72f200Smrgfi
1953a72f200Smrg
1963a72f200Smrgif test $# -eq 0; then
1979027f4a0Smrg  if test -z "$dir_arg"; then
1989027f4a0Smrg    echo "$0: no input file specified." >&2
1999027f4a0Smrg    exit 1
2009027f4a0Smrg  fi
201fe2ac8d7Smrg  # It's OK to call 'install-sh -d' without argument.
2029027f4a0Smrg  # This can happen when creating conditional directories.
2039027f4a0Smrg  exit 0
2049027f4a0Smrgfi
2059027f4a0Smrg
206376c9fa8Smrgif test -z "$dir_arg"; then
207376c9fa8Smrg  if test $# -gt 1 || test "$is_target_a_directory" = always; then
208376c9fa8Smrg    if test ! -d "$dst_arg"; then
209376c9fa8Smrg      echo "$0: $dst_arg: Is not a directory." >&2
210376c9fa8Smrg      exit 1
211376c9fa8Smrg    fi
212376c9fa8Smrg  fi
213376c9fa8Smrgfi
214376c9fa8Smrg
2153a72f200Smrgif test -z "$dir_arg"; then
216fe2ac8d7Smrg  do_exit='(exit $ret); exit $ret'
217fe2ac8d7Smrg  trap "ret=129; $do_exit" 1
218fe2ac8d7Smrg  trap "ret=130; $do_exit" 2
219fe2ac8d7Smrg  trap "ret=141; $do_exit" 13
220fe2ac8d7Smrg  trap "ret=143; $do_exit" 15
2213a72f200Smrg
2223a72f200Smrg  # Set umask so as not to create temps with too-generous modes.
2233a72f200Smrg  # However, 'strip' requires both read and write access to temps.
2243a72f200Smrg  case $mode in
2253a72f200Smrg    # Optimize common cases.
2263a72f200Smrg    *644) cp_umask=133;;
2273a72f200Smrg    *755) cp_umask=22;;
2283a72f200Smrg
2293a72f200Smrg    *[0-7])
2303a72f200Smrg      if test -z "$stripcmd"; then
231376c9fa8Smrg        u_plus_rw=
2323a72f200Smrg      else
233376c9fa8Smrg        u_plus_rw='% 200'
2343a72f200Smrg      fi
2353a72f200Smrg      cp_umask=`expr '(' 777 - $mode % 1000 ')' $u_plus_rw`;;
2363a72f200Smrg    *)
2373a72f200Smrg      if test -z "$stripcmd"; then
238376c9fa8Smrg        u_plus_rw=
2393a72f200Smrg      else
240376c9fa8Smrg        u_plus_rw=,u+rw
2413a72f200Smrg      fi
2423a72f200Smrg      cp_umask=$mode$u_plus_rw;;
2433a72f200Smrg  esac
2443a72f200Smrgfi
2453a72f200Smrg
2469027f4a0Smrgfor src
2479027f4a0Smrgdo
248fe2ac8d7Smrg  # Protect names problematic for 'test' and other utilities.
2499027f4a0Smrg  case $src in
250fe2ac8d7Smrg    -* | [=\(\)!]) src=./$src;;
2519027f4a0Smrg  esac
2529027f4a0Smrg
2539027f4a0Smrg  if test -n "$dir_arg"; then
2549027f4a0Smrg    dst=$src
2553a72f200Smrg    dstdir=$dst
2563a72f200Smrg    test -d "$dstdir"
2573a72f200Smrg    dstdir_status=$?
2589027f4a0Smrg  else
2593a72f200Smrg
2609027f4a0Smrg    # Waiting for this to be detected by the "$cpprog $src $dsttmp" command
2619027f4a0Smrg    # might cause directories to be created, which would be especially bad
2629027f4a0Smrg    # if $src (and thus $dsttmp) contains '*'.
2639027f4a0Smrg    if test ! -f "$src" && test ! -d "$src"; then
2649027f4a0Smrg      echo "$0: $src does not exist." >&2
2659027f4a0Smrg      exit 1
2669027f4a0Smrg    fi
2679027f4a0Smrg
2683a72f200Smrg    if test -z "$dst_arg"; then
2699027f4a0Smrg      echo "$0: no destination specified." >&2
2709027f4a0Smrg      exit 1
2719027f4a0Smrg    fi
2723a72f200Smrg    dst=$dst_arg
2739027f4a0Smrg
2749027f4a0Smrg    # If destination is a directory, append the input filename; won't work
2759027f4a0Smrg    # if double slashes aren't ignored.
2769027f4a0Smrg    if test -d "$dst"; then
277376c9fa8Smrg      if test "$is_target_a_directory" = never; then
278376c9fa8Smrg        echo "$0: $dst_arg: Is a directory" >&2
279376c9fa8Smrg        exit 1
2809027f4a0Smrg      fi
2813a72f200Smrg      dstdir=$dst
2823a72f200Smrg      dst=$dstdir/`basename "$src"`
2833a72f200Smrg      dstdir_status=0
2843a72f200Smrg    else
285376c9fa8Smrg      dstdir=`dirname "$dst"`
2863a72f200Smrg      test -d "$dstdir"
2873a72f200Smrg      dstdir_status=$?
2889027f4a0Smrg    fi
2899027f4a0Smrg  fi
2909027f4a0Smrg
2913a72f200Smrg  obsolete_mkdir_used=false
2923a72f200Smrg
2933a72f200Smrg  if test $dstdir_status != 0; then
2943a72f200Smrg    case $posix_mkdir in
2953a72f200Smrg      '')
296376c9fa8Smrg        # Create intermediate dirs using mode 755 as modified by the umask.
297376c9fa8Smrg        # This is like FreeBSD 'install' as of 1997-10-28.
298376c9fa8Smrg        umask=`umask`
299376c9fa8Smrg        case $stripcmd.$umask in
300376c9fa8Smrg          # Optimize common cases.
301376c9fa8Smrg          *[2367][2367]) mkdir_umask=$umask;;
302376c9fa8Smrg          .*0[02][02] | .[02][02] | .[02]) mkdir_umask=22;;
303376c9fa8Smrg
304376c9fa8Smrg          *[0-7])
305376c9fa8Smrg            mkdir_umask=`expr $umask + 22 \
306376c9fa8Smrg              - $umask % 100 % 40 + $umask % 20 \
307376c9fa8Smrg              - $umask % 10 % 4 + $umask % 2
308376c9fa8Smrg            `;;
309376c9fa8Smrg          *) mkdir_umask=$umask,go-w;;
310376c9fa8Smrg        esac
311376c9fa8Smrg
312376c9fa8Smrg        # With -d, create the new directory with the user-specified mode.
313376c9fa8Smrg        # Otherwise, rely on $mkdir_umask.
314376c9fa8Smrg        if test -n "$dir_arg"; then
315376c9fa8Smrg          mkdir_mode=-m$mode
316376c9fa8Smrg        else
317376c9fa8Smrg          mkdir_mode=
318376c9fa8Smrg        fi
319376c9fa8Smrg
320376c9fa8Smrg        posix_mkdir=false
321376c9fa8Smrg        case $umask in
322376c9fa8Smrg          *[123567][0-7][0-7])
323376c9fa8Smrg            # POSIX mkdir -p sets u+wx bits regardless of umask, which
324376c9fa8Smrg            # is incompatible with FreeBSD 'install' when (umask & 300) != 0.
325376c9fa8Smrg            ;;
326376c9fa8Smrg          *)
327376c9fa8Smrg            tmpdir=${TMPDIR-/tmp}/ins$RANDOM-$$
328376c9fa8Smrg            trap 'ret=$?; rmdir "$tmpdir/d" "$tmpdir" 2>/dev/null; exit $ret' 0
329376c9fa8Smrg
330376c9fa8Smrg            if (umask $mkdir_umask &&
331376c9fa8Smrg                exec $mkdirprog $mkdir_mode -p -- "$tmpdir/d") >/dev/null 2>&1
332376c9fa8Smrg            then
333376c9fa8Smrg              if test -z "$dir_arg" || {
334376c9fa8Smrg                   # Check for POSIX incompatibilities with -m.
335376c9fa8Smrg                   # HP-UX 11.23 and IRIX 6.5 mkdir -m -p sets group- or
336376c9fa8Smrg                   # other-writable bit of parent directory when it shouldn't.
337376c9fa8Smrg                   # FreeBSD 6.1 mkdir -m -p sets mode of existing directory.
338376c9fa8Smrg                   ls_ld_tmpdir=`ls -ld "$tmpdir"`
339376c9fa8Smrg                   case $ls_ld_tmpdir in
340376c9fa8Smrg                     d????-?r-*) different_mode=700;;
341376c9fa8Smrg                     d????-?--*) different_mode=755;;
342376c9fa8Smrg                     *) false;;
343376c9fa8Smrg                   esac &&
344376c9fa8Smrg                   $mkdirprog -m$different_mode -p -- "$tmpdir" && {
345376c9fa8Smrg                     ls_ld_tmpdir_1=`ls -ld "$tmpdir"`
346376c9fa8Smrg                     test "$ls_ld_tmpdir" = "$ls_ld_tmpdir_1"
347376c9fa8Smrg                   }
348376c9fa8Smrg                 }
349376c9fa8Smrg              then posix_mkdir=:
350376c9fa8Smrg              fi
351376c9fa8Smrg              rmdir "$tmpdir/d" "$tmpdir"
352376c9fa8Smrg            else
353376c9fa8Smrg              # Remove any dirs left behind by ancient mkdir implementations.
354376c9fa8Smrg              rmdir ./$mkdir_mode ./-p ./-- 2>/dev/null
355376c9fa8Smrg            fi
356376c9fa8Smrg            trap '' 0;;
357376c9fa8Smrg        esac;;
3583a72f200Smrg    esac
3599027f4a0Smrg
3603a72f200Smrg    if
3613a72f200Smrg      $posix_mkdir && (
362376c9fa8Smrg        umask $mkdir_umask &&
363376c9fa8Smrg        $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir"
3643a72f200Smrg      )
3653a72f200Smrg    then :
3663a72f200Smrg    else
3679027f4a0Smrg
3683a72f200Smrg      # The umask is ridiculous, or mkdir does not conform to POSIX,
3693a72f200Smrg      # or it failed possibly due to a race condition.  Create the
3703a72f200Smrg      # directory the slow way, step by step, checking for races as we go.
3719027f4a0Smrg
3723a72f200Smrg      case $dstdir in
373376c9fa8Smrg        /*) prefix='/';;
374376c9fa8Smrg        [-=\(\)!]*) prefix='./';;
375376c9fa8Smrg        *)  prefix='';;
3763a72f200Smrg      esac
3779027f4a0Smrg
3783a72f200Smrg      oIFS=$IFS
3793a72f200Smrg      IFS=/
380376c9fa8Smrg      set -f
3813a72f200Smrg      set fnord $dstdir
3829027f4a0Smrg      shift
383376c9fa8Smrg      set +f
3843a72f200Smrg      IFS=$oIFS
3853a72f200Smrg
3863a72f200Smrg      prefixes=
3873a72f200Smrg
3883a72f200Smrg      for d
3893a72f200Smrg      do
390376c9fa8Smrg        test X"$d" = X && continue
391376c9fa8Smrg
392376c9fa8Smrg        prefix=$prefix$d
393376c9fa8Smrg        if test -d "$prefix"; then
394376c9fa8Smrg          prefixes=
395376c9fa8Smrg        else
396376c9fa8Smrg          if $posix_mkdir; then
397376c9fa8Smrg            (umask=$mkdir_umask &&
398376c9fa8Smrg             $doit_exec $mkdirprog $mkdir_mode -p -- "$dstdir") && break
399376c9fa8Smrg            # Don't fail if two instances are running concurrently.
400376c9fa8Smrg            test -d "$prefix" || exit 1
401376c9fa8Smrg          else
402376c9fa8Smrg            case $prefix in
403376c9fa8Smrg              *\'*) qprefix=`echo "$prefix" | sed "s/'/'\\\\\\\\''/g"`;;
404376c9fa8Smrg              *) qprefix=$prefix;;
405376c9fa8Smrg            esac
406376c9fa8Smrg            prefixes="$prefixes '$qprefix'"
407376c9fa8Smrg          fi
408376c9fa8Smrg        fi
409376c9fa8Smrg        prefix=$prefix/
4103a72f200Smrg      done
4113a72f200Smrg
4123a72f200Smrg      if test -n "$prefixes"; then
413376c9fa8Smrg        # Don't fail if two instances are running concurrently.
414376c9fa8Smrg        (umask $mkdir_umask &&
415376c9fa8Smrg         eval "\$doit_exec \$mkdirprog $prefixes") ||
416376c9fa8Smrg          test -d "$dstdir" || exit 1
417376c9fa8Smrg        obsolete_mkdir_used=true
4189027f4a0Smrg      fi
4193a72f200Smrg    fi
4209027f4a0Smrg  fi
4219027f4a0Smrg
4229027f4a0Smrg  if test -n "$dir_arg"; then
4233a72f200Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dst"; } &&
4243a72f200Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dst"; } &&
4253a72f200Smrg    { test "$obsolete_mkdir_used$chowncmd$chgrpcmd" = false ||
4263a72f200Smrg      test -z "$chmodcmd" || $doit $chmodcmd $mode "$dst"; } || exit 1
4279027f4a0Smrg  else
4289027f4a0Smrg
4299027f4a0Smrg    # Make a couple of temp file names in the proper directory.
4309027f4a0Smrg    dsttmp=$dstdir/_inst.$$_
4319027f4a0Smrg    rmtmp=$dstdir/_rm.$$_
4329027f4a0Smrg
4339027f4a0Smrg    # Trap to clean up those temp files at exit.
4349027f4a0Smrg    trap 'ret=$?; rm -f "$dsttmp" "$rmtmp" && exit $ret' 0
4359027f4a0Smrg
4369027f4a0Smrg    # Copy the file name to the temp name.
4373a72f200Smrg    (umask $cp_umask && $doit_exec $cpprog "$src" "$dsttmp") &&
4389027f4a0Smrg
4399027f4a0Smrg    # and set any options; do chmod last to preserve setuid bits.
4409027f4a0Smrg    #
4419027f4a0Smrg    # If any of these fail, we abort the whole thing.  If we want to
4429027f4a0Smrg    # ignore errors from any of these, just make sure not to ignore
4439027f4a0Smrg    # errors from the above "$doit $cpprog $src $dsttmp" command.
4449027f4a0Smrg    #
4453a72f200Smrg    { test -z "$chowncmd" || $doit $chowncmd "$dsttmp"; } &&
4463a72f200Smrg    { test -z "$chgrpcmd" || $doit $chgrpcmd "$dsttmp"; } &&
4473a72f200Smrg    { test -z "$stripcmd" || $doit $stripcmd "$dsttmp"; } &&
4483a72f200Smrg    { test -z "$chmodcmd" || $doit $chmodcmd $mode "$dsttmp"; } &&
4493a72f200Smrg
4503a72f200Smrg    # If -C, don't bother to copy if it wouldn't change the file.
4513a72f200Smrg    if $copy_on_change &&
452376c9fa8Smrg       old=`LC_ALL=C ls -dlL "$dst"     2>/dev/null` &&
453376c9fa8Smrg       new=`LC_ALL=C ls -dlL "$dsttmp"  2>/dev/null` &&
454376c9fa8Smrg       set -f &&
4553a72f200Smrg       set X $old && old=:$2:$4:$5:$6 &&
4563a72f200Smrg       set X $new && new=:$2:$4:$5:$6 &&
457376c9fa8Smrg       set +f &&
4583a72f200Smrg       test "$old" = "$new" &&
4593a72f200Smrg       $cmpprog "$dst" "$dsttmp" >/dev/null 2>&1
4603a72f200Smrg    then
4613a72f200Smrg      rm -f "$dsttmp"
4623a72f200Smrg    else
4633a72f200Smrg      # Rename the file to the real destination.
4643a72f200Smrg      $doit $mvcmd -f "$dsttmp" "$dst" 2>/dev/null ||
4653a72f200Smrg
4663a72f200Smrg      # The rename failed, perhaps because mv can't rename something else
4673a72f200Smrg      # to itself, or perhaps because mv is so ancient that it does not
4683a72f200Smrg      # support -f.
4693a72f200Smrg      {
470376c9fa8Smrg        # Now remove or move aside any old file at destination location.
471376c9fa8Smrg        # We try this two ways since rm can't unlink itself on some
472376c9fa8Smrg        # systems and the destination file might be busy for other
473376c9fa8Smrg        # reasons.  In this case, the final cleanup might fail but the new
474376c9fa8Smrg        # file should still install successfully.
475376c9fa8Smrg        {
476376c9fa8Smrg          test ! -f "$dst" ||
477376c9fa8Smrg          $doit $rmcmd -f "$dst" 2>/dev/null ||
478376c9fa8Smrg          { $doit $mvcmd -f "$dst" "$rmtmp" 2>/dev/null &&
479376c9fa8Smrg            { $doit $rmcmd -f "$rmtmp" 2>/dev/null; :; }
480376c9fa8Smrg          } ||
481376c9fa8Smrg          { echo "$0: cannot unlink or rename $dst" >&2
482376c9fa8Smrg            (exit 1); exit 1
483376c9fa8Smrg          }
484376c9fa8Smrg        } &&
485376c9fa8Smrg
486376c9fa8Smrg        # Now rename the file to the real destination.
487376c9fa8Smrg        $doit $mvcmd "$dsttmp" "$dst"
4883a72f200Smrg      }
4893a72f200Smrg    fi || exit 1
4903a72f200Smrg
4913a72f200Smrg    trap '' 0
4923a72f200Smrg  fi
4939027f4a0Smrgdone
4949027f4a0Smrg
4959027f4a0Smrg# Local variables:
4969027f4a0Smrg# eval: (add-hook 'write-file-hooks 'time-stamp)
4979027f4a0Smrg# time-stamp-start: "scriptversion="
4989027f4a0Smrg# time-stamp-format: "%:y-%02m-%02d.%02H"
4993a72f200Smrg# time-stamp-time-zone: "UTC"
5003a72f200Smrg# time-stamp-end: "; # UTC"
5019027f4a0Smrg# End:
502