Well I am writing this in english for all people who find programming is an art for itself.
I just submitted an entry to a code obfuscation contest. The challenge was "write a replacement for the unix command 'cat'"
Here is my contribution:
#!/bin/bash
do_stuff()
{
c=`caller|cut -d " " -f 1`
if [ $c -eq 55 ] || [ $c -eq 61 ] ; then
lc=`wc -l "$1"|cut -d " " -f 1`
do_stuff "$1" "$lc"
fi
if [ $c -eq 51 ] ; then
echo "Usage: $1 [-h|--help] [<filename>]" 1>&2
echo "" 1>&2
echo " -h or --help display this help" 1>&2
echo "" 1>&2
echo "If no <filename> is specified $1 will use stdin as default" 1>&2
echo "If you want to use a file named -h or --help use an empty argument" 1>&2
echo "(like '--') as the first argument." 1>&2
exit 0
fi
if [ $c -eq 8 ] ; then
lc="$2"
l=0
exec 3<"$1"
while head -n1 <&3 ; do
l=$(( $l + 1 ))
do_stuff $l $lc
if [ $? -eq 0 ] ; then
return
fi
done
fi
if [ $c -eq 67 ] ; then
sed -e "" <&0
fi
if [ $c -eq 26 ] ; then
[ "$1" != "" ] && [ "$2" != "" ] && if [ $1 -eq $2 ] ; then
return 0
else
return 1
fi
return 1
fi
if [ $c -eq 57 ] || [ $c -eq 63 ] ; then
echo "$1: file '$2' not existing or not readable!" 1>&2
exit 1
fi
}
if [ "$1" != "" ] ; then
if [ "$1" = "-h" ] || [ "$1" = "--help" ] ; then
do_stuff "$0"
fi
if [ "$1" = "--" ] ; then
if [ -r "$2" ] ; then
do_stuff "$2"
else
do_stuff "$0" "$2"
fi
else
if [ -r "$1" ] ; then
do_stuff "$1"
else
do_stuff "$0" "$1"
fi
fi
else
do_stuff
fi
For all those who are brave enough to try it (yes it works flawlessly but very very slooooow) you can download it here (better download it, as one inserted line may break the complete program): http://www.kdemod.ath.cx/~dunkelstern/cat.sh
Please comment on it, and may the most obfuscated program win :)
PS: Contest is here: http://bbs.archlinux.org/viewtopic.php?pid=261892#p261892