File: uimg

package info (click to toggle)
avfs 1.2.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 3,864 kB
  • sloc: ansic: 31,321; sh: 6,482; perl: 1,916; makefile: 351
file content (220 lines) | stat: -rw-r--r-- 5,697 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
#!/bin/bash

# This is a parser for DOS/Atari disk images in Midnight Commander. You 
# need the GPL mtools program (version >= 3.9.9) written by Emmet P. Gray, 
# Viktor Dukhovni, Alain Knaff and David Niemi.

# Author: Guus Jansman

# Limitations:
# - Only the 1st primary harddisk partition and floppy images can be read
# - File attributes are not preserved.
# - Filenames containing non-ascii characters may cause problems (check mtools info).

# Alternative programs (not supported by this script):
# + Mounting a loop device (one has to have proper rights though).
# + XMess's imgtool (less powerfull).
# + Gilles Vollant's extract (DOS program, less powerful)
# + bximage (disk image creation, part of Bochs)

# Settings:
SIZE=1.44m      # Only needed for creating a disk image
TEMPRCFILE=/tmp/mctmpfile-uimg.$USER.${RANDOM}
LISTIMG="mdir -/ -a -f"
ATTRIBIMG="mattrib"
ADDIMG="mcopy -pm -D o"
DELETEIMG="mdel"
EXTRACTIMG="mcopy"
MKDIRIMG="mmd"
RMDIRIMG="mrd"
TESTIMG="minfo"
CREATEIMG="mformat -C"
CONFIGIMG="mtoolstest"

case "$SIZE" in
    160*)   CREATEIMG="$CREATEIMG -f 160"  # 12/40/1/8
            ;;
    180*)   CREATEIMG="$CREATEIMG -f 180"  # 12/40/1/9
            ;;
    320*)   CREATEIMG="$CREATEIMG -f 320"  # 12/40/2/8
            ;;
    360*)   CREATEIMG="$CREATEIMG -f 360"  # 12/40/2/9
            ;;
    720*)   CREATEIMG="$CREATEIMG -f 720"  # 12/80/2/9
            ;;
    800*)   CREATEIMG="$CREATEIMG -t 80 -h 2 -n 10" # 12/80/2/10
            ;;
    1.2*)   CREATEIMG="$CREATEIMG -f 1200" # 12/80/2/15
            ;;
    1.4*)   CREATEIMG="$CREATEIMG -f 1440" # 12/80/2/18
            ;;
    2.8*)   CREATEIMG="$CREATEIMG -f 2880" # 12/80/2/36
            ;;
    *)      CREATEIMG="$CREATEIMG -f 1440" # 12/80/2/18
            ;;
esac

drives="ABCDEFGHIJKLMNOPQRSTUVWXYZ"

createmtoolsrc ()
{
#   Find a free drive letter
    useddrives="`$CONFIGIMG | grep -e '^drive [A-Z]:.*' | sed -e 's/drive \(.\):.*/\1/'`"
    for (( drivenum=2 ; drivenum != 25 ; drivenum++ )) ; do
        DRIVE=${drives:drivenum:1}
        if [[ $useddrives == ${useddrives/$DRIVE/1} ]]; then
            break;  # Found a free drive letter
        fi
    done
    if [[ drivenum == 25 ]]; then
        exit 1;     # Could not find a free drive letter
    fi
    DRIVE="${DRIVE}:"

#   Create temporary settings file
    rm -f $TEMPRCFILE ; touch $TEMPRCFILE
    echo MTOOLS_LOWER_CASE=0 >> $TEMPRCFILE
    echo MTOOLS_TWENTY_FOUR_HOUR_CLOCK=1 >> $TEMPRCFILE
    echo "drive $DRIVE" >> $TEMPRCFILE
    echo "  file=\"$1\"" >> $TEMPRCFILE
    echo "  mformat_only" >> $TEMPRCFILE
    if [[ -e "$1" && "`wc -c "$1" | sed -e 's/[ ]*\([^ ]*\).*$/\1/'`" -gt "4194304" ]]; then
        echo "  partition=1" >> $TEMPRCFILE
    fi

#   Set environment variable
    export MTOOLSRC=$TEMPRCFILE
}

deletemtoolsrc ()
{
    rm -f $TEMPRCFILE
}

mcimgfs_list ()
{
    createmtoolsrc "$1"
    $LISTIMG $DRIVE/ | gawk -v uid=${UID-0} '
    BEGIN { direntry=0; date="JanFebMarAprMayJunJulAugSepOctNovDec" }
    /^Directory for / { direntry=1 }
    /[ ]*[0-9][0-9]* file/ { direntry=0 ; next }
    /^$/ { next }
    /^\./ { next }
    // { if (direntry == 0) next }
    {
        if (index($0, "Directory for ") == 1)
        {
            curdir=substr($0, 18)
        }
        else
        {
            tm=substr($0, 36, 5)
            year=substr($0, 24, 4)
            month=substr(date, (substr($0, 29, 2)-1)*3 + 1, 3)
            day=substr($0, 32, 2)
            if (length($0) > 43)
            {
                str=substr($0, 43)
            }
            else
            {
                str=substr($0, 1, 8)
                while (substr(str, length(str), 1) == " ")
                {
                    str=substr(str, 1, length(str) - 1)
                }
                if (substr($0, 10, 3) != "   ")
                {
                    str=(str "." $2)
                }
            }
            if (length(curdir) != 0)
            {
                str=(curdir "/" str)
            }
            if (substr($0, 14, 5) == "<DIR>")
            {
                perm="drwxr-xr-x"
                sz=0
            }
            else
            {
                perm="-rw-r--r--"
                sz=substr($0, 14, 9)
            }
            printf "%s    1 %-8d %-8d %8d %3s %2d %4d %s %s\n", perm, uid, 0, sz, month, day, year, tm, str
        }
    }'
    deletemtoolsrc
}

mcimgfs_copyin ()
{
    createmtoolsrc "$1"
    $ADDIMG "$3" "$DRIVE/$2" >/dev/null
    deletemtoolsrc
}

mcimgfs_copyout ()
{
    createmtoolsrc "$1"
    $EXTRACTIMG "$DRIVE/$2" - > "$3" 2>/dev/null
    deletemtoolsrc
}

mcimgfs_rm ()
{
    createmtoolsrc "$1"
    $ATTRIBIMG -r "$DRIVE/$2" >/dev/null
    $DELETEIMG "$DRIVE/$2" >/dev/null
    deletemtoolsrc "$1"
}

mcimgfs_mkdir ()
{
    createmtoolsrc "$1"
    $MKDIRIMG "$DRIVE/$2" >/dev/null 2>/dev/null
    deletemtoolsrc
}

mcimgfs_rmdir ()
{
    createmtoolsrc "$1"
    $RMDIRIMG "$DRIVE/$2" >/dev/null 2>/dev/null
    deletemtoolsrc
}

mcimgfs_test ()
{
    createmtoolsrc "$1"
    if $TESTIMG $DRIVE >/dev/null 2>&1; then
        echo "OK"
    else
        echo "UNKNOWN"
    fi
    deletemtoolsrc
}

mcimgfs_create ()
{
    createmtoolsrc "$1"
    $CREATEIMG $DRIVE >/dev/null
    deletemtoolsrc
}

umask 077

cmd="$1"
shift
case "$cmd" in
    list)    mcimgfs_list    "$@" ;;
    rm)      mcimgfs_rm      "$@" ;;
    copyin)  mcimgfs_copyin  "$@" ;;
    copyout) mcimgfs_copyout "$@" ;;
    mkdir)   mcimgfs_mkdir   "$@" ;;
    rmdir)   mcimgfs_rmdir   "$@" ;;
#    test)    mcimgfs_test    "$@" ;;        # Not supported by MC extfs
#    create)  mcimgfs_create  "$@" ;;        # Not supported by MC extfs
    *)       exit 1 ;;
esac
exit 0