From: Alfie Costa (agcosta@gis.net)
Date: Fri Feb 18 2000 - 15:15:32 CET
On 15 Feb 00, at 18:10, Michele Andreoli <mulinux@sunsite.auc.dk> wrote:
> The solution! Bravo Alfie: you solved the puzzle. Yes, muLinux "file"
> command is the most incredible script I (n? -ac)ever wrote: it use only "dd"
> to fetch some bytes from the file's head, trying to recognize the file
> content. Instructive, but inefficient is called serially.
Grazie! I've now been ambitiously tweaking mu's rustic "file" to make it
faster, and maybe export some New England style bugs. So far it seems to be
about 40% faster, but this is a guess, based on running "file" and counting
"one two three...". Before it was eight, now it's five. Novice benchmarking
question: is there a timer command in mu that shows how long something takes?
My changes to "file" are:
1) Zapped a few commented out lines, apparently tests left over from when it
was first written. Is doing this bad?
2) Uncommented the 'does this file exist' check, formerly commented out.
Letting it run doesn't seem to have hurt anything, but there's always the
doubt. Good/bad?
3) After some struggle trying to figure out how /bin/ash stores variables, I've
mutated the two functions in "file", TestString() and TestOctal(), so they do
all their work with variables and don't write any temporary files. This is
where the speed-up comes from...
4) Changed the name of a variable or two.
It seems to work, or at least a test run of "cd /bin;file `ls`" looked OK. The
mutant "file" is attached to this post.
If no bugs show up, I'd like to tweak it some more by adding some sort of logic
so that "file" doesn't need to call "dd" as much; unless that turns out to be
too difficult...
#!/bin/ash
# rustic `file` (by M. Andreoli)
# [ with dd
# (Feb 18 7:51 2000 tweaked by A. Costa)
#Syntax
opt=$1
case Z$opt in
Z-h|Z) echo "Usage (mu-file): file [files]" ; exit ;;
*)
esac
# Functions
# compare with string
TestString()
{
f=$1
offset=$2; n=$3 ; string1="$4"
string2=`dd if=$f skip=$offset bs=1c count=$n 2>/dev/null`
test "$string1" = "$string2"
}
# compare with octal \\0x \\0y \\0z ...
TestOctal()
{
f=$1
offset=$2; n=$3
shift 3
string1=
for code in $@
do
string1="$string1$code"
done
string1=`echo -e $string1`
string2=`dd if=$f skip=$offset bs=1c count=$n 2>/dev/null`
test "$string1" = "$string2"
}
f=$1
for f in "$@"
do
# special
[ -d "$f" ] && echo "$f: directory" && continue
[ -L "$f" ] && echo "$f: symbolic link" && continue
[ ! -f "$f" ] && echo "$f: not existent" && continue
# compressed
TestString $f 257 5 ustar && echo "$f: TAR archive" && continue
TestString $f 0 4 'Rar!' && echo "$f: RAR archive data" && continue
TestString $f 0 2 PK && echo "$f: Zip archive data" && continue
TestString $f 0 2 BZ && echo "$f: bzip compressed data" && continue
TestOctal $f 0 2 \\037 \\0213 && echo -e "$f: gzip compress data" && continue
# text
TestString $f 0 5 '%PDF-' && echo "$f: PDF document" && continue
TestString $f 0 2 '%!' \
&& echo "$f: PostScript document text" && continue
TestOctal $f 0 2 \\0367 \002 && echo "$f: TeX DVI file" && continue
# script
TestString $f 0 9 '#!/bin/sh' && echo "$f: Bourne shell script text" && continue
TestString $f 0 11 '#!/bin/bash' \
&& echo "$f: Bash shell script text" && continue
TestString $f 0 10 '#!/bin/csh' && echo "$f: C shell script text" && continue
TestString $f 0 10 '#!/bin/ksh' && echo "$f: Korn shell script text" && continue
TestString $f 0 10 '#!/bin/ash' && echo "$f: ash script text" && continue
TestString $f 0 11 '#!/bin/perl' && echo "$f: perl command text" && continue
TestString $f 0 7 '#!/bin/' && echo "$f: script text" && continue
# Linux
TestString $f 1080 1 'S' \
&& echo "$f: Linux/i386 ext2 filesystem [probable :(]"
TestString $f 4086 10 'SWAP-SPACE' \
&& echo "$f: Linux/i386 swap file" && continue
TestString $f 1 3 ELF \
&& echo -e "$f: ELF executable" && continue
# Audio
TestString $f 0 4 MThd && echo "$f: Standard MIDI data" && continue
TestString $f 0 4 RIFF && echo "$f: Microsoft RIFF" \
&& TestString $f 8 4 WAWE \
&& echo -n ", WAVE audio data" && continue
# image
TestString $f 0 4 GIF8 && echo "$f: GIF image data" && return
TestString $f 0 2 BM && echo "$f: PC bitmap data" && return
TestString $f 0 2 MM \
&& TestOctal $f 2 2 \\0 \\052 \
&& echo "$f: TIFF image data, big-endian" && continue
TestString $f 0 2 II \
&& TestOctal $f 2 2 \\052 \\0 \
&& echo "$f: TIFF image data, little-endian" && continue
TestOctal $f 0 2 \\0377 \\0330 && echo "$f: JPEG image data" && continue
# binary
TestString $f 0 2 'MZ' && echo "$f: MS-DOS executable (EXE)" && continue
# HP48
TestString $f 0 8 'HPHP48-' && echo "$f: HP48 binary" && continue
TestString $f 0 5 '%%HP:' && echo "$f: HP48 text" && continue
echo "$f: ASCII text or data"
done
---------------------------------------------------------------------
To unsubscribe, e-mail: mulinux-unsubscribe@sunsite.auc.dk
For additional commands, e-mail: mulinux-help@sunsite.auc.dk
This archive was generated by hypermail 2.1.6 : Sat Feb 08 2003 - 15:27:13 CET