IOCCC image by Matt Zucker

The International Obfuscated C Code Contest

2013/misaka - Most catty

horizontal or vertical file concat

Author:

To build:

    make

This will build a bunch of versions of the code. See the author’s details for information on all of these builds.

To use:

    ./horizontal_cat [files...] > [output]
    ./vertical_cat [files...] > [output]

Try:

To understand the various intermediate source files that are built, try:

    make understanding

Also try:

    ./try.sh

NOTE: Use - for standard input. For example:

    ./vertical_cat - -

Judges’ remarks:

There is more to cat than mere cats. Be they tall, fat, long or squat, this source code is sure to amuse.

Slight diversion

While one of the judges was reviewing this entry on the stern of the Island Sky in the middle of the Atlantic Ocean waiting for the total solar eclipse of 2013 the judge was struck by a flying fish. While the result of the impact was that the Port from Portugal was lost, the Grog from Cape Verde and the laptop containing this entry was saved.

While I’m sure most cats would have preferred otherwise, the flying fish was returned to swim with the fishes. :-) No cats were harmed in the process of judging this entry.

Author’s remarks:

Overview

Misaka is a file concatenation utility, with at least two modes of operation:

    cc misaka.c -o horizontal_cat
    ./horizontal_cat [files...] > [output]

    ./horizontal_cat misaka.c misaka.c > misaka2.c
    cc misaka2.c -o vertical_cat
    ./vertical_cat [files...] > [output]

Where [files...] is a list of text file names. Use - to read from stdin.

Details

Horizontal cat

One of my favorite unix utilities is cat. The best thing about it was that it was named “cat”. That, and it’s useful for quickly showing contents of a file. Though it was primary meant for concatenating files, that function only seem to work if I wanted to concatenate files vertically.

I thought the lack of horizontal concatenation must have been an oversight, so I implemented this utility:

    cc misaka.c -o horizontal_cat
    ./horizontal_cat files...

horizontal_cat concatenates files horizontally and write the output to stdout. Each input file is padded with spaces on the right so that the original text alignments are preserved.

If - is specified as a file name, horizontal_cat will read from stdin. Unlike cat, horizontal_cat loads all input to memory first. Thus you can specify - multiple times to get stdin multiplied horizontally. For example, if you have seq(1) in your shell, you can add line numbers to both sides of misaka.c like this:

    seq -f '  %.0f  ' 45 | ./horizontal_cat - misaka.c -

Vertical cat

Because horizontal_cat must know the maximum width of all files before writing any output, all files must be processed at least twice. To support stdin, file must be buffered to memory. This lead to the feature that horizontal_cat can be used to duplicate stdin.

Seems like the stdin doubling feature might be useful even for concatenating files vertically, so I included a vertical mode. But supporting vertical mode with command line options would be no fun. Instead, vertical mode is enabled by concatenating the source code horizontally:

    ./horizontal_cat misaka.c misaka.c > misaka2.c
    gcc misaka2.c -o vertical_cat
    ./vertical_cat files...

vertical_cat works more or less like cat, except you can use vertical_cat to duplicate stdin:

    ./vertical_cat - -

Long cat

After horizontal_cat and vertical_cat, I thought, maybe all I really wanted was just more cats. So I implemented one more mode, this one is enabled by concatenating misaka.c vertically:

    ./vertical_cat misaka.c misaka.c > misaka3.c
    gcc misaka3.c -o long_cat
    ./long_cat

long_cat outputs ASCII art of a cat to stdout. You can make this cat exponentially longer by concatenating more files vertically (up to 31 levels high, depending on sizeof(int) for your compiler):

    ./vertical_cat misaka.c misaka.c misaka.c > misaka4.c
    gcc misaka4.c -o loong_cat

    ./vertical_cat misaka.c misaka.c misaka.c misaka.c > misaka5.c
    gcc misaka5.c -o loooong_cat

    ./vertical_cat misaka.c misaka.c misaka.c misaka.c misaka.c > misaka6.c
    gcc misaka6.c -o loooooooong_cat

If your terminal has really thin fonts, you can also make this output fatter by concatenating files horizontally:

    ./vertical_cat misaka.c misaka.c | ./horizontal_cat - - > misaka7.c
    gcc misaka7.c -o long_fat_cat

    ./vertical_cat misaka.c misaka.c | ./horizontal_cat - - - > misaka8.c
    gcc misaka8.c -o long_faat_cat

Output width is determined by the first level of stacked programs, so a triangle like the following will not have horizontally expanded output:

    ./horizontal_cat misaka.c misaka.c | ./vertical_cat misaka.c - > misaka9.c
    gcc misaka9.c -o same_as_long_cat

Finally, if you lost track of how many misaka.c you have stacked together, you can feed the source to a brainfuck interpreter to get an overview of how the programs are stacked. Example:

    perl bf.pl misaka9.c

This outputs:

    MISAKA
    MISAKA MISAKA

Return value

horizontal_cat and vertical_cat will exit with zero status on success.

If any input file fails to open, horizontal_cat and vertical_cat will report the offending file name to stdout, and exit with nonzero status.

If horizontal_cat and vertical_cat ran out of memory, they will exit with nonzero status without outputting anything.

Compatibility

horizontal_cat makes the following assumptions about input files:

Misaka has been verified to work on these following compiler+OS combinations:

Misaka requires a C99 compiler due to the use of single line comments. Misaka does not depend on any other C99 features.

Obfuscation

Main obfuscation is in having a C program that compiles when tiled horizontally and vertically, while using single line comments in less than half of the lines and maintaining a meaningful layout while doing that.

There are other challenges too, of course, like getting a Brainfuck program to tile horizontally and vertically. Really, tiling code of any sort horizontally is an interesting exercise, I encourage everyone to try this once.

Other features to look for:

gcc -Wall should provide a hint to where the mode switch happens, it does not output any irrelevant other warnings (verified on 4.6.3).

Extra files

Extra files included in my submission are informational only, they are not needed for the program to work.

About Misaka

The name, layout, and functionality of Misaka is inspired by a particular stackable figure:

Inventory for 2013/misaka

Primary files

Secondary files


Jump to: top