Fix bug#5395: typing '#' in an empty C buffer throws "args out of range".
[emacs.git] / mkinstalldirs
blobbe98de6be01a1ec467902370b497291cb5d89596
1 #! /bin/sh
2 # mkinstalldirs --- make directory hierarchy
4 scriptversion=2006-05-11.19
6 # Original author: Noah Friedman <friedman@prep.ai.mit.edu>
7 # Created: 1993-05-16
8 # Public domain.
10 # This file is maintained in Automake, please report
11 # bugs to <bug-automake@gnu.org> or send patches to
12 # <automake-patches@gnu.org>.
14 nl='
16 IFS=" "" $nl"
17 errstatus=0
18 dirmode=
20 usage="\
21 Usage: mkinstalldirs [-h] [--help] [--version] [-m MODE] DIR ...
23 Create each directory DIR (with mode MODE, if specified), including all
24 leading file name components.
26 Report bugs to <bug-automake@gnu.org>."
28 # process command line arguments
29 while test $# -gt 0 ; do
30 case $1 in
31 -h | --help | --h*) # -h for help
32 echo "$usage"
33 exit $?
35 -m) # -m PERM arg
36 shift
37 test $# -eq 0 && { echo "$usage" 1>&2; exit 1; }
38 dirmode=$1
39 shift
41 --version)
42 echo "$0 $scriptversion"
43 exit $?
45 --) # stop option processing
46 shift
47 break
49 -*) # unknown option
50 echo "$usage" 1>&2
51 exit 1
53 *) # first non-opt arg
54 break
56 esac
57 done
59 for file
61 if test -d "$file"; then
62 shift
63 else
64 break
66 done
68 case $# in
69 0) exit 0 ;;
70 esac
72 # Solaris 8's mkdir -p isn't thread-safe. If you mkdir -p a/b and
73 # mkdir -p a/c at the same time, both will detect that a is missing,
74 # one will create a, then the other will try to create a and die with
75 # a "File exists" error. This is a problem when calling mkinstalldirs
76 # from a parallel make. We use --version in the probe to restrict
77 # ourselves to GNU mkdir, which is thread-safe.
78 case $dirmode in
79 '')
80 if mkdir -p --version . >/dev/null 2>&1 && test ! -d ./--version; then
81 echo "mkdir -p -- $*"
82 exec mkdir -p -- "$@"
83 else
84 # On NextStep and OpenStep, the `mkdir' command does not
85 # recognize any option. It will interpret all options as
86 # directories to create, and then abort because `.' already
87 # exists.
88 test -d ./-p && rmdir ./-p
89 test -d ./--version && rmdir ./--version
93 if mkdir -m "$dirmode" -p --version . >/dev/null 2>&1 &&
94 test ! -d ./--version; then
95 echo "mkdir -m $dirmode -p -- $*"
96 exec mkdir -m "$dirmode" -p -- "$@"
97 else
98 # Clean up after NextStep and OpenStep mkdir.
99 for d in ./-m ./-p ./--version "./$dirmode";
101 test -d $d && rmdir $d
102 done
105 esac
107 for file
109 case $file in
110 /*) pathcomp=/ ;;
111 *) pathcomp= ;;
112 esac
113 oIFS=$IFS
114 IFS=/
115 set fnord $file
116 shift
117 IFS=$oIFS
119 for d
121 test "x$d" = x && continue
123 pathcomp=$pathcomp$d
124 case $pathcomp in
125 -*) pathcomp=./$pathcomp ;;
126 esac
128 if test ! -d "$pathcomp"; then
129 echo "mkdir $pathcomp"
131 mkdir "$pathcomp" || lasterr=$?
133 if test ! -d "$pathcomp"; then
134 errstatus=$lasterr
135 else
136 if test ! -z "$dirmode"; then
137 echo "chmod $dirmode $pathcomp"
138 lasterr=
139 chmod "$dirmode" "$pathcomp" || lasterr=$?
141 if test ! -z "$lasterr"; then
142 errstatus=$lasterr
148 pathcomp=$pathcomp/
149 done
150 done
152 exit $errstatus