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