C self reproducing program

Inspired by the classic Ken Thompson’s “Trusting Trust” speech, I decided to try to write my own self-replicating program. I took as the basis for the (rather incomplete) sample of code shown in the above speech. I wanted to avoid approaches like having the program open it’s source file and output its contents. The result is a 387 line C file. Doing it makes you think and rethink about the true power of quoting, and how to abuse it (and made think about a Lisp, but that’s just me, I guess…). Should you decide to try to read it, have fun! (but be warned that the first ~350 lines are rather boring! 😉 )

When running it and comparing its output with its source, beware of spaces, specially if you copied the file from the web browser and pasted into the text editor. Either download the file or when doing the diff, use:

diff --ignore-all-space

And an interesting thing I found when doing this, is that vim by default always adds a newline at the end of file (you can see this for yourself by doing “tail -1 | hexdump” to some file_name written in vim. The last character will be an “0A” (hex code for “\n”). This caused some problems with the diff output (it issues a warning if one or more file do not end with a newline), and would be problematic if mixing files written in Unix and Windows, but whatever…

Anyway, talk is cheap, so here’s the code: source. To view, just remove the .pdf extension (it’s there just to fool WordPress’ allowed formats limitation…) and extract the tarball.


Os comentários estão fechados.