Alexandre Julliard | ce830a9 | 2002-05-09 04:31:39 +0000 | [diff] [blame] | 1 | #! /bin/sh |
| 2 | # mkinstalldirs --- make directory hierarchy |
| 3 | # Author: Noah Friedman <friedman@prep.ai.mit.edu> |
| 4 | # Created: 1993-05-16 |
| 5 | # Public domain |
| 6 | |
| 7 | errstatus=0 |
| 8 | dirmode="" |
| 9 | |
| 10 | usage="\ |
| 11 | Usage: mkinstalldirs [-h] [--help] [-m mode] dir ..." |
| 12 | |
| 13 | # process command line arguments |
| 14 | while test $# -gt 0 ; do |
| 15 | case "${1}" in |
| 16 | -h | --help | --h* ) # -h for help |
| 17 | echo "${usage}" 1>&2; exit 0 ;; |
| 18 | -m ) # -m PERM arg |
| 19 | shift |
| 20 | test $# -eq 0 && { echo "${usage}" 1>&2; exit 1; } |
| 21 | dirmode="${1}" |
| 22 | shift ;; |
| 23 | -- ) shift; break ;; # stop option processing |
| 24 | -* ) echo "${usage}" 1>&2; exit 1 ;; # unknown option |
| 25 | * ) break ;; # first non-opt arg |
| 26 | esac |
| 27 | done |
| 28 | |
| 29 | for file |
| 30 | do |
| 31 | if test -d "$file"; then |
| 32 | shift |
| 33 | else |
| 34 | break |
| 35 | fi |
| 36 | done |
| 37 | |
| 38 | case $# in |
| 39 | 0) exit 0 ;; |
| 40 | esac |
| 41 | |
| 42 | case $dirmode in |
| 43 | '') |
| 44 | if mkdir -p -- . 2>/dev/null; then |
| 45 | echo "mkdir -p -- $*" |
| 46 | exec mkdir -p -- "$@" |
| 47 | fi ;; |
| 48 | *) |
Francois Gouget | fb7afa4 | 2006-06-15 18:05:07 +0200 | [diff] [blame] | 49 | # We cannot trust mkdir to set the proper permissions on |
| 50 | # parent directories. So create them manually. |
| 51 | ;; |
Alexandre Julliard | ce830a9 | 2002-05-09 04:31:39 +0000 | [diff] [blame] | 52 | esac |
| 53 | |
| 54 | for file |
| 55 | do |
Francois Gouget | fb7afa4 | 2006-06-15 18:05:07 +0200 | [diff] [blame] | 56 | case "$file" in |
| 57 | /* ) pathcomp="/" ;; |
| 58 | -* ) pathcomp="./" ;; |
| 59 | * ) pathcomp="" ;; |
| 60 | esac |
Alexandre Julliard | ce830a9 | 2002-05-09 04:31:39 +0000 | [diff] [blame] | 61 | |
Francois Gouget | fb7afa4 | 2006-06-15 18:05:07 +0200 | [diff] [blame] | 62 | saved_IFS="$IFS" |
| 63 | IFS="/" |
| 64 | for d in $file |
Alexandre Julliard | ce830a9 | 2002-05-09 04:31:39 +0000 | [diff] [blame] | 65 | do |
Francois Gouget | fb7afa4 | 2006-06-15 18:05:07 +0200 | [diff] [blame] | 66 | IFS="$saved_IFS" |
| 67 | if test -n "$d"; then |
| 68 | pathcomp="$pathcomp$d" |
| 69 | if test ! -d "$pathcomp"; then |
| 70 | echo "mkdir $pathcomp" |
| 71 | mkdir "$pathcomp" || lasterr=$? |
Alexandre Julliard | ce830a9 | 2002-05-09 04:31:39 +0000 | [diff] [blame] | 72 | |
Francois Gouget | fb7afa4 | 2006-06-15 18:05:07 +0200 | [diff] [blame] | 73 | if test ! -d "$pathcomp"; then |
| 74 | errstatus=$lasterr |
| 75 | break |
| 76 | elif test -n "$dirmode"; then |
| 77 | echo "chmod $dirmode $pathcomp" |
| 78 | lasterr="" |
| 79 | chmod "$dirmode" "$pathcomp" || lasterr=$? |
| 80 | if test -n "$lasterr"; then |
| 81 | errstatus=$lasterr |
| 82 | break |
| 83 | fi |
| 84 | fi |
| 85 | fi |
| 86 | pathcomp="$pathcomp/" |
| 87 | fi |
Alexandre Julliard | ce830a9 | 2002-05-09 04:31:39 +0000 | [diff] [blame] | 88 | done |
| 89 | done |
| 90 | |
| 91 | exit $errstatus |
| 92 | |
| 93 | # Local Variables: |
| 94 | # mode: shell-script |
| 95 | # sh-indentation: 3 |
| 96 | # End: |
| 97 | # mkinstalldirs ends here |