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.

Open source vs. witchcraft

In my late night ramblings, I found this quote from Linus Torvalds:

I think, fundamentally, open source does tend to be more stable software. It’s the right way to do things. I compare it to science vs. witchcraft. In science, the whole system builds on people looking at other people’s results and building on top of them. In witchcraft, somebody had a small secret and guarded it — but never allowed others to really understand it and build on it.

Traditional software is like witchcraft. In history, witchcraft just died out. The same will happen in software. When problems get serious enough, you can’t have one person or one company guarding their secrets. You have to have everybody share in knowledge.

The fact that this reality is systematically ignored, is perhaps a part of the reason of why software is in the sorry state we all know. And now I’m going to sleep.

Script that email

To my own future reference, and to whom it might prove useful, I post a little Tcl/expect script, used to (rudimentarily) automate email sending. Usage shouldn’t be too hard to figure out :-)

#! /usr/bin/expect

if { $argc < 2 } {
        puts "Usage: ./$argv0 MTA_IP_addr MTA_port \[rcpt\]"
spawn telnet [lindex $argv 0] [lindex $argv 1]
set rcpt "oscar@foo.tld"
if { $argc == 3 } {
        set rcpt [lindex $argv 2]
expect "220"
send "ehlo a\n"
expect "250 DSN"
send "mail from: <>\n"
expect "250 2.1.0 Ok"
send "rcpt to: <$rcpt>\n"
expect {
        "250 2.1.5 Ok" {
                send "data\n"
                expect "354 End data with <CR><LF>.<CR><LF>"
                set ts [exec "/bin/date" +%s]
                send "Subject: foo $ts\n"
                send "bar\n"
                send ".\n"
                expect "250 2.0.0 Ok"
        "5**" {
                puts "Rcpt addr rejected!\n"
                return -1
send "quit"

EDIT: modified the script to also receive from command line the recipient address


Sometime ago, I wrote about being snared to the dark side of text editors’ universe, viz. Emacs. That foray was not long lasted, though. For only reading through its tutorial (and trying things out while I read) was enough to make me stop. Literally: ten minutes after I started my fingers hurt so much I just had to stop. And never bothered to continue where I’d left. Or think much about it—until I found this list of famous programmers that both used Emacs and developed RSI:

I visited Richard Stallman at MIT and I was shocked to learn that he could no longer type. He was given strict instructions by his doctor to not touch a computer keyboard for 6-12 months, and that if he did, he may lose forever his ability to type. He was a programming pioneer, and at the time, his symptoms were not well known or understood. We all came to understand that it was RSI–repetitive stress injury, exacerbated by the very keystroke combinations that made the Emacs editor such a powerful programming environment. But the root cause was not Emacs–it was the punative design of the QWERTY keyboard, a legacy of the industrial era when complex keyboard mechanisms were not able to keep up with the speed of human fingers.

Regardless of whether QWERTY keyboards are or not to blame, the fact is that Emacs is, in what shortcuts are concerned at least, stuck in the past. The shift key is not used, the documentation still refers to the Alt key by its name in the previous incarnation (Meta), and most shortcuts were indeed thought up to be used with keyboards belonging to said incarnation (which today belong in a museum; if only the same could be applied to shortcuts…).

By now you should be wandering why am I complaining about Emacs again?! (“If you don’t like it, just don’t use it!”) Well, I’ve been reading Pratical Common Lisp, and using vim with Limp for Lisp hacking. The problem is that I’m tired of it, and after all I’ve read, been told, etc, about Emacs, I am going to try it. God bless it’s viper mode

If you know how to code…

… then take a break, go get a nice cup of tea, and enjoy:


A couple of teasers:

// Dear maintainer:
// Once you are done trying to ‘optimize’ this routine,
// and have realized what a terrible mistake that was,
// please increment the following counter as a warning
// to the next guy:
// total_hours_wasted_here = 16

//When I wrote this, only God and I understood what I was doing
//Now, God only knows

// somedev1 – 6/7/02 Adding temporary tracking of Login screen
// somedev2 – 5/22/07 Temporary my ass

// I will give you two of my seventy-two virgins if you can fix this.

Have fun reading.

Two Vim tidbits

The first one is a boon to *nix programmers: how to open a man page from inside vim! Turns out this is rather easy: your vim installation came with the file /usr/share/vim/vim72/ftplugin/man.vim, which defines the command Man (with a capital ‘M’), which you can call like so: :Man _some_manpage_. To make this command available, just add this to your .vimrc:

runtime! ftplugin/man.vim

Now, while editing a C file for instance, you can do a :Man printf and vim will horizontally split the screen, and display the manpage in the upper half. If you’re like me, and prefer to split the screen vertically, do ^Wt^WH.

The second tidbit is about Vim folding. Whatever the fold method used (indent, marker, …), if one is used, when opening a file, all folds present will be collapsed. More often than not, I find this an annoyance, so after setting the indent method, I added another line that expands all the folds, like so (again done in ~/.vimrc):

set foldmethod=indent
set foldminlines=10
autocmd BufRead *.* norm zR

The last line expands all folds; the middle line is actually to prevent folds with less than 10 lines (which is fairly common in source code, given that the fold method is indent).