OSDN Git Service

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