| 1 |
#!/bin/bash |
| 2 |
|
| 3 |
# This is a parser for DOS/Atari disk images in Midnight Commander. You |
| 4 |
# need the GPL mtools program (version >= 3.9.9) written by Emmet P. Gray, |
| 5 |
# Viktor Dukhovni, Alain Knaff and David Niemi. |
| 6 |
|
| 7 |
# Author: Guus Jansman |
| 8 |
|
| 9 |
# Limitations: |
| 10 |
# - Only the 1st primary harddisk partition and floppy images can be read |
| 11 |
# - File attributes are not preserved. |
| 12 |
# - Filenames containing non-ascii characters may cause problems (check mtools info). |
| 13 |
|
| 14 |
# Alternative programs (not supported by this script): |
| 15 |
# + Mounting a loop device (one has to have proper rights though). |
| 16 |
# + XMess's imgtool (less powerfull). |
| 17 |
# + Gilles Vollant's extract (DOS program, less powerful) |
| 18 |
# + bximage (disk image creation, part of Bochs) |
| 19 |
|
| 20 |
# Settings: |
| 21 |
SIZE=1.44m # Only needed for creating a disk image |
| 22 |
TEMPRCFILE=/tmp/mctmpfile-uimg.$USER.${RANDOM} |
| 23 |
LISTIMG="mdir -/ -a -f" |
| 24 |
ATTRIBIMG="mattrib" |
| 25 |
ADDIMG="mcopy -pm -D o" |
| 26 |
DELETEIMG="mdel" |
| 27 |
EXTRACTIMG="mcopy" |
| 28 |
MKDIRIMG="mmd" |
| 29 |
RMDIRIMG="mrd" |
| 30 |
TESTIMG="minfo" |
| 31 |
CREATEIMG="mformat -C" |
| 32 |
CONFIGIMG="mtoolstest" |
| 33 |
|
| 34 |
case "$SIZE" in |
| 35 |
160*) CREATEIMG="$CREATEIMG -f 160" # 12/40/1/8 |
| 36 |
;; |
| 37 |
180*) CREATEIMG="$CREATEIMG -f 180" # 12/40/1/9 |
| 38 |
;; |
| 39 |
320*) CREATEIMG="$CREATEIMG -f 320" # 12/40/2/8 |
| 40 |
;; |
| 41 |
360*) CREATEIMG="$CREATEIMG -f 360" # 12/40/2/9 |
| 42 |
;; |
| 43 |
720*) CREATEIMG="$CREATEIMG -f 720" # 12/80/2/9 |
| 44 |
;; |
| 45 |
800*) CREATEIMG="$CREATEIMG -t 80 -h 2 -n 10" # 12/80/2/10 |
| 46 |
;; |
| 47 |
1.2*) CREATEIMG="$CREATEIMG -f 1200" # 12/80/2/15 |
| 48 |
;; |
| 49 |
1.4*) CREATEIMG="$CREATEIMG -f 1440" # 12/80/2/18 |
| 50 |
;; |
| 51 |
2.8*) CREATEIMG="$CREATEIMG -f 2880" # 12/80/2/36 |
| 52 |
;; |
| 53 |
*) CREATEIMG="$CREATEIMG -f 1440" # 12/80/2/18 |
| 54 |
;; |
| 55 |
esac |
| 56 |
|
| 57 |
drives="ABCDEFGHIJKLMNOPQRSTUVWXYZ" |
| 58 |
|
| 59 |
createmtoolsrc () |
| 60 |
{ |
| 61 |
# Find a free drive letter |
| 62 |
useddrives="`$CONFIGIMG | grep -e '^drive [A-Z]:.*' | sed -e 's/drive \(.\):.*/\1/'`" |
| 63 |
for (( drivenum=2 ; drivenum != 25 ; drivenum++ )) ; do |
| 64 |
DRIVE=${drives:drivenum:1} |
| 65 |
if [[ $useddrives == ${useddrives/$DRIVE/1} ]]; then |
| 66 |
break; # Found a free drive letter |
| 67 |
fi |
| 68 |
done |
| 69 |
if [[ drivenum == 25 ]]; then |
| 70 |
exit 1; # Could not find a free drive letter |
| 71 |
fi |
| 72 |
DRIVE="${DRIVE}:" |
| 73 |
|
| 74 |
# Create temporary settings file |
| 75 |
rm -f $TEMPRCFILE ; touch $TEMPRCFILE |
| 76 |
echo MTOOLS_LOWER_CASE=0 >> $TEMPRCFILE |
| 77 |
echo MTOOLS_TWENTY_FOUR_HOUR_CLOCK=1 >> $TEMPRCFILE |
| 78 |
echo "drive $DRIVE" >> $TEMPRCFILE |
| 79 |
echo " file=\"$1\"" >> $TEMPRCFILE |
| 80 |
echo " mformat_only" >> $TEMPRCFILE |
| 81 |
if [[ -e "$1" && "`wc -c "$1" | sed -e 's/[ ]*\([^ ]*\).*$/\1/'`" -gt "4194304" ]]; then |
| 82 |
echo " partition=1" >> $TEMPRCFILE |
| 83 |
fi |
| 84 |
|
| 85 |
# Set environment variable |
| 86 |
export MTOOLSRC=$TEMPRCFILE |
| 87 |
} |
| 88 |
|
| 89 |
deletemtoolsrc () |
| 90 |
{ |
| 91 |
rm -f $TEMPRCFILE |
| 92 |
} |
| 93 |
|
| 94 |
mcimgfs_list () |
| 95 |
{ |
| 96 |
createmtoolsrc "$1" |
| 97 |
$LISTIMG $DRIVE/ | gawk -v uid=${UID-0} ' |
| 98 |
BEGIN { direntry=0; date="JanFebMarAprMayJunJulAugSepOctNovDec" } |
| 99 |
/^Directory for / { direntry=1 } |
| 100 |
/[ ]*[0-9][0-9]* file/ { direntry=0 ; next } |
| 101 |
/^$/ { next } |
| 102 |
/^\./ { next } |
| 103 |
// { if (direntry == 0) next } |
| 104 |
{ |
| 105 |
if (index($0, "Directory for ") == 1) |
| 106 |
{ |
| 107 |
curdir=substr($0, 18) |
| 108 |
} |
| 109 |
else |
| 110 |
{ |
| 111 |
tm=substr($0, 36, 5) |
| 112 |
year=substr($0, 24, 4) |
| 113 |
month=substr(date, (substr($0, 29, 2)-1)*3 + 1, 3) |
| 114 |
day=substr($0, 32, 2) |
| 115 |
if (length($0) > 43) |
| 116 |
{ |
| 117 |
str=substr($0, 43) |
| 118 |
} |
| 119 |
else |
| 120 |
{ |
| 121 |
str=substr($0, 1, 8) |
| 122 |
while (substr(str, length(str), 1) == " ") |
| 123 |
{ |
| 124 |
str=substr(str, 1, length(str) - 1) |
| 125 |
} |
| 126 |
if (substr($0, 10, 3) != " ") |
| 127 |
{ |
| 128 |
str=(str "." $2) |
| 129 |
} |
| 130 |
} |
| 131 |
if (length(curdir) != 0) |
| 132 |
{ |
| 133 |
str=(curdir "/" str) |
| 134 |
} |
| 135 |
if (substr($0, 14, 5) == "<DIR>") |
| 136 |
{ |
| 137 |
perm="drwxr-xr-x" |
| 138 |
sz=0 |
| 139 |
} |
| 140 |
else |
| 141 |
{ |
| 142 |
perm="-rw-r--r--" |
| 143 |
sz=substr($0, 14, 9) |
| 144 |
} |
| 145 |
printf "%s 1 %-8d %-8d %8d %3s %2d %4d %s %s\n", perm, uid, 0, sz, month, day, year, tm, str |
| 146 |
} |
| 147 |
}' |
| 148 |
deletemtoolsrc |
| 149 |
} |
| 150 |
|
| 151 |
mcimgfs_copyin () |
| 152 |
{ |
| 153 |
createmtoolsrc "$1" |
| 154 |
$ADDIMG "$3" "$DRIVE/$2" >/dev/null |
| 155 |
deletemtoolsrc |
| 156 |
} |
| 157 |
|
| 158 |
mcimgfs_copyout () |
| 159 |
{ |
| 160 |
createmtoolsrc "$1" |
| 161 |
$EXTRACTIMG "$DRIVE/$2" - > "$3" 2>/dev/null |
| 162 |
deletemtoolsrc |
| 163 |
} |
| 164 |
|
| 165 |
mcimgfs_rm () |
| 166 |
{ |
| 167 |
createmtoolsrc "$1" |
| 168 |
$ATTRIBIMG -r "$DRIVE/$2" >/dev/null |
| 169 |
$DELETEIMG "$DRIVE/$2" >/dev/null |
| 170 |
deletemtoolsrc "$1" |
| 171 |
} |
| 172 |
|
| 173 |
mcimgfs_mkdir () |
| 174 |
{ |
| 175 |
createmtoolsrc "$1" |
| 176 |
$MKDIRIMG "$DRIVE/$2" >/dev/null 2>/dev/null |
| 177 |
deletemtoolsrc |
| 178 |
} |
| 179 |
|
| 180 |
mcimgfs_rmdir () |
| 181 |
{ |
| 182 |
createmtoolsrc "$1" |
| 183 |
$RMDIRIMG "$DRIVE/$2" >/dev/null 2>/dev/null |
| 184 |
deletemtoolsrc |
| 185 |
} |
| 186 |
|
| 187 |
mcimgfs_test () |
| 188 |
{ |
| 189 |
createmtoolsrc "$1" |
| 190 |
if $TESTIMG $DRIVE >/dev/null 2>&1; then |
| 191 |
echo "OK" |
| 192 |
else |
| 193 |
echo "UNKNOWN" |
| 194 |
fi |
| 195 |
deletemtoolsrc |
| 196 |
} |
| 197 |
|
| 198 |
mcimgfs_create () |
| 199 |
{ |
| 200 |
createmtoolsrc "$1" |
| 201 |
$CREATEIMG $DRIVE >/dev/null |
| 202 |
deletemtoolsrc |
| 203 |
} |
| 204 |
|
| 205 |
umask 077 |
| 206 |
|
| 207 |
cmd="$1" |
| 208 |
shift |
| 209 |
case "$cmd" in |
| 210 |
list) mcimgfs_list "$@" ;; |
| 211 |
rm) mcimgfs_rm "$@" ;; |
| 212 |
copyin) mcimgfs_copyin "$@" ;; |
| 213 |
copyout) mcimgfs_copyout "$@" ;; |
| 214 |
mkdir) mcimgfs_mkdir "$@" ;; |
| 215 |
rmdir) mcimgfs_rmdir "$@" ;; |
| 216 |
# test) mcimgfs_test "$@" ;; # Not supported by MC extfs |
| 217 |
# create) mcimgfs_create "$@" ;; # Not supported by MC extfs |
| 218 |
*) exit 1 ;; |
| 219 |
esac |
| 220 |
exit 0 |