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\]"
        exit
}
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

Emacs

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:

http://stackoverflow.com/questions/184618?sort=votes&page=1#sort-top

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).

Unicode/UTF-8 compulsory reading

This should absolutely be a part of the compulsory reading at any half decent Computer Science undergrad curricula. Quoting:

So I have an announcement to make: if you are a programmer working in 2003 and you don’t know the basics of characters, character sets, encodings, and Unicode, and I catch you, I’m going to punish you by making you peel onions for 6 months in a submarine. I swear I will.

And one more thing:

IT’S NOT THAT HARD.

And as that last phrase says, it really isn’t all that complicated. It takes 20 minutes to read thoroughly, and it WILL save you a LOT of debugging time in the future (I speak out of experience). The more low level/hardcore explanation is here, for those interested.

Programming languages humour

If you’re a computer programmer, do yourself a favor and read this. After that, burst into laughing frenzy. :-)

Incidentally, that site just gave me the most accurate description on to how convey my opinion of Cobol:

1959 – After losing a bet with L. Ron Hubbard, Grace Hopper and several other sadists invent the Capitalization Of Boilerplate Oriented Language (COBOL) . Years later, in a misguided and sexist retaliation against Adm. Hopper’s COBOL work, Ruby conferences frequently feature misogynistic material.

:D

The multitask delusion

Every once in a while, you stumble across that really makes you think. That happened to me some time ago. Browsing Slashdot, as usual, I found a story about this post’s title. It revolves about what could be termed as «multitasking in the information age». How instant messaging, and sms text messaging and TVs and iPods and next generation cell phones all blend in together with the shinning promise of helping boost our productivity (both on a professional and personal level), of making us do more in less time, of making us multi task. And how that inescapably leads us to attain the very same opposite: to do less, and at a higher cost. And how we have (or should have) seen it coming.

To understand why I ended up (re)reading this article so many times, and even storing a printed version, some background is needed about my (quite) late blossom into computer world, and the subsequent years in college, studying—you’ve guessed it—computers.

Although I had been playing with computers for some years before, I got my first computer at the age of 16. Of course I was lured into the realm of computer games, but unlike many colleagues and friends, I thought little of if, and spent very little time playing—back then. I also started making Word (yes MS Word!) notes of some of my high school classes, and I guess that was really the beginning of it. It took me quite a long time to write those notes, and the benefit I had, if some at all, was close to none. I see that now, I should have also seen it back then. But I guess, never having owned a computer before, the hype and the promise were all too attracting.

We all remember the promises. The slogans. They were all about freedom, liberation. Supposedly we were in handcuffs and wanted out of them. The key that dangled in front of us was a microchip.

Sometime after I got that first computer, came the internet (an oldie 56K dial-up connection that monopolized the phone line whenever anyone was “surfing”). And after that, along came college. I had all resources I need, to be ultra-supra-high-efficient. Or so I thought…

Now on top of all that, I was studying computer engineering, a course in which you are expect to work A LOT more and think a lot less (not to mention weird things like, oh I don’t know, sleeping…). So that’s how I started to multitask. The studying, the assignments, loud music all the way (too loud, too along the way), MSN with everyone all the time (cell phone use and the corresponding bill saw a steady decrease during this time, which might well be the only good thing that came out of all that mess). And games! Right, in my sophomore year I got addicted to Unreal Tournament, which undoubtedly helped to boost the party.

The abiding, distinctive feature of all crashes, whether in stock prices, housing values, or hit-TV-show ratings, is that they startle but don’t surprise. When the euphoria subsides, when the volatile graph lines of excitability flatten and then curve down, people realize, collectively and instantly (and not infrequently with some relief), that they’ve been expecting this correction.

In my case, the frenzy lasted three years. By that time I was in my final year, and had long realized that all the effort I was putting in graduating was not yielding as good results as I’d desire, though I was unable to grasp why. Granted, I did not like a lot of subjects that were part of the curricula for the last years (in fact, I actively hated many of them, specially those even remotely related with management). But I wanted to graduate and move out of the city, so I did put a lot of honest effort into it. Ah, but we are going astray…

OK, back to topic. During those years, I distinctively remember a conversation with a friend, that despite not being a computer engineering student, also used to study alongside the computer. Whenever she’d be without an internet connection, she’d complain: “ah I was so used to always clicking on the [tray] icons for MSN and eMule[P2P client that was very popular around here until being overthrown by BitTorrent] and now I miss it!”. In what MSN is concerned at least, I still carry that habit. Not as badly as before, but still way too much.

The crash came in two steps(!). The first forced a slow down, the second a complete standstill. But eventually I graduated, moved on, and got time to think. And precisely in thought, I found out (and had begun to suspect), laid the difference. The nature of my course already got me so overwhelmed with work; computer+msn+music+… complete and stealthy took over what little time I could afford to spare. Study took longer, and became under-productive. Here’s why (I got the chills the first time I read this paragraph):

Multitasking messes with the brain in several ways. At the most basic level, the mental balancing acts that it requires—the constant switching and pivoting—energize regions of the brain that specialize in visual processing and physical coordination and simultaneously appear to shortchange some of the higher areas related to memory and learning. We concentrate on the act of concentration at the expense of whatever it is that we’re supposed to be concentrating on.

And therein laid the rub. If only I could have known this some years early. But I didn’t, I read it now. And despite late, it is infinitely better than never.

Where do you want to go today?” Microsoft asked us.

Now that I no longer confuse freedom with speed, convenience, and mobility, my answer would be: “Away. Just away. Someplace where I can think.”

Anyone who knows me, even if barely, knows I read a lot. But if you ask me what’d recommend for anyone that uses a computer for more than writing one-page letters in MS Word, this would definitely be it. Take it from someone who’s been there… and back.

What kind of programmer are you?

For all of you that code, which one of these are you?

Programmers enjoy a reputation for being peculiar people. In fact, even within the development community, there are certain programmer archetypes that other programmers find strange. Here are 10 types of programmers you are likely to run across. Can you think of any more?

Yours truly over here is either The Ninja or Gandalf… though just moments ago I was discussing this and was just labelled The Theoretician…