2009/06/16

Ioccc best one liner

This is a brief analysis of korn.c, winner of the 1987 IOCCC. Why? Because. And because many people who shot some explanations have many flaws and conceptual mistakes.

#include <stdio.h>
/*
* Review of one of the best IOCCC one liner winners.
* See International Obfuscated C Code Contest (http://www.ioccc.org)
* ...found other reviews with conceptual mistakes, so I wrote mine...
*
* Winners:  http://www.ioccc.org/years.html#1987
* The code: http://www.ioccc.org/1987/korn.c
* Hints:    http://www.ioccc.org/1987/korn.hint
*
* Rodolfo Alcazar Portillo <rodolfoap@gmail.com>
*/

main() {
/* a simple text */
printf("%s\n","unix");

/* or... */
printf("%six\n","un");

/* adding an unnecesary C text terminator does not change output */
printf("%six\n\0","un");

/* replace \n==\012 which is a printable form of an octal value */
printf("%six\012\0","un");

/* the additional X won't print due to +1 offset */
printf("X%six\012\0"+1,"un");

/* nor any other character */
printf("\021%six\012\0"+1,"un");

/* rephrasing syntax as a text array */
printf(&"\021%six\012\0"[1],"un");

/* And the preprocessor said "Let unix==1". Verification
*      $ cpp -dM /dev/null | grep unix
*   #define __unix__ 1
*   #define __unix 1
*   #define unix 1
*
* Therefore it won't run on Windows unless compiled with cygwin or defined in preprocessor as:
* #define unix 1
*
* ...and therefore redefining the unix variable would raise an error.
*/
printf(&"\021%six\012\0"[unix],"un");

/* &text[n]==&n[text] is basic pointer arithmetics, or addition communitivity */
printf(&unix["\021%six\012\0"],"un");

/* 'a'==0x61 then ('a'-0x61)==0 */
printf(&unix["\021%six\012\0"],"un"+'a'-0x61);

/* Then add any char -X- and a offset of 1 (=='a'-0x60) */
printf(&unix["\021%six\012\0"],"Xun"+'a'-0x60);

/* use a double-quoted string, and its offset, which gives an integer value: 'a'=="a"[0] */
printf(&unix["\021%six\012\0"],"Xun"+"a"[0]-0x60);

/* or the same, with an arbitrary text and offset */
printf(&unix["\021%six\012\0"],"Xun"+"XaXX"[1]-0x60);

/* again, replace 1 with the predefined preprocessor unix==1 value...  */
printf(&unix["\021%six\012\0"],"Xun"+"XaXX"[unix]-0x60);

/* rephrasing, array pointer arithmetics, addition is communitive */
printf(&unix["\021%six\012\0"],"Xun"+(unix)["XaXX"]-0x60);

/* ditto */
printf(&unix["\021%six\012\0"],(unix)["XaXX"]+"Xun"-0x60);

/* at last, change ignored chars to something meaningful */
printf(&unix["\021%six\012\0"],(unix)["have"]+"fun"-0x60);
}

2008/03/31

Bug photo (2010/03/31)

2007/01/18

Update: Bash-only Linux

This is an update to this old article, which works on Fedora Core 6.

Copy this script into a file, modify the grub entries with your system parameters, run it and enjoy using a dummy linux where you can only write "help", and some useless commands... swear next it'll include the nvidia driver.

#!/bin/bash -v
# Bash only filesystem on a file - rodolfoap@gmail.com

# Create an empty file
dd if=/dev/zero of=/tmp/embed bs=2k count=4k

# Make an ext2fs
/sbin/mkfs.ext2 -v -F -b 2048 /tmp/embed

# Mount it as a loop
mkdir -p /mnt/embed
mount -v /tmp/embed /mnt/embed -o loop -t ext2
cd /mnt/embed

# Create basic structure and fill it with needed files
mkdir bin dev lib
cp -av /dev/tty /dev/console /dev/ram dev/
cp -v /bin/bash bin/
# to found libs needed by bash, use # ldd /bin/bash
cp -v /lib/libtermcap.so.2 lib/
cp -v /lib/libdl.so.2 lib/
cp -v /lib/libc.so.6 lib/
cp -v /lib/ld-linux.so.2 lib/

# Create boot file
cd /tmp
umount -v /mnt/embed
gzip -v < embed > /boot/embed.gz

# Add entry in grub: change root and kernel line with your parameters
cat >> /etc/grub.conf << "EOF"
title Bash Only Filesystem
root (hd0,0)
kernel /vmlinuz-2.6.18-1.2869.fc6 ro root=/dev/ram ramdisk_blocksize=2048 init=/bin/bash
initrd /embed.gz
EOF

# Manually correct grub parameters
read -p "Press ENTER to start editing grub.conf..."
vi /etc/grub.conf

# End script. Reboot with "init 6" and choose the BASH option in GRUB.
# Toto: sos un mostro, thnx.