Calling a function in C without naming it

11 points by mrnossiom


nathell

This reminds me how, way back when, at one of the classes in my school we were expected to adhere to coding standards provided. Said coding standards prohibited the use of tabs throughout all files (the language expected was C). They further mandated all submissions to include a Makefile.

Taken literally, this would mean that tabs were forbidden in Makefiles, which as everyone knows makes it pretty impossible to do anything. But I found a way to circumvent this and have tab-less Makefiles that did what they were expected to do! Indeed, I wrote a converter from tabful to tabless Makefiles.

Here’s how it worked – it transformed this:

all:
	gcc a.c -o a

to this:

FOO := $(shell echo 'all:\n\tgcc a.c -o a' > GNUmakefile; $(MAKE) >&2)

all:

i.e. a Makefile containing one rule that does nothing, and defining a dummy eagerly-evaluated variable whose evaluation would write the actual makefile content to a file that takes precedence over the original one, and invoke make recursively on that. This took advantage of the fact that GNU make (nobody used anything else) looks at GNUmakefile first, then makefile, then Makefile.

I never got bonus points for this, but hey, now I have an amusing story!