raganwald
(This is a snapshot of my old weblog. New posts and selected republished essays can be found at raganwald.com.)

Friday, February 22, 2008
  The recursive implementation of /bin/true


Here is the source code to /bin/true in Solaris (via The Daily WTF):

#!/usr/bin/sh
# Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T
# All Rights Reserved

# THIS IS UNPUBLISHED PROPRIETARY SOURCE CODE OF AT&T
# The copyright notice above does not evidence any
# actual or intended publication of such source code.

#ident "@(#)true.sh 1.6 93/01/11 SMI" /* SVr4.0 1.4 */

/bin/true simply exits successfully, and Solaris implements this as a shell script. There’s a lot to laugh about on a Friday afternoon, but a thought struck me: What exactly is the copyright notice copyrighting? Not any implementation code: This “implementation” of /bin/true doesn’t do anything, so it has no executable code. Not the idea of implementing /bin/true as a shell script without implementation code: you’d need a patent to protect that idea.

I conclude that the copyright notice is protecting… The copyright notice! There is a delicious self-reference there in the style of Gödel, Escher, Bach
 

Comments on “The recursive implementation of /bin/true:
As stated at the Daily WTF, the other notable quality of this implementation is that it's up to version 1.6. They'd like to see the change log. I'd like to see the bug reports.
 
Oddly, if I remember correctly, the first line, the one starting with #! is a directive to the shell to start a specific program, in this case sh. The rest is actual comments, but I think what this is doing is calling sh without a terminal or command, so it starts briefly and then exits. If that's true, then there is actually a line of code in this file :-)

Given it probably forks and execs, its a fairly expensive way of assigning a true value. A shell variable would be a lot cheaper :-)

Just a guess...

Paul.
http://theprogrammersparadox.blogspot.com
 
Paul, fortunately speed isn't really an issue here, as most often this command isn't actually called. As GNU's true (which responds to --help and --version, by the way) says:

NOTE: your shell may have its own version of true, which usually supersedes the version described here. Please refer to your shell's documentation for details about the options it supports.

And indeed it does, at least for bash. Calling "true --help" here does nothing, while "`which true` --help" says a few things, including that note I just cited. For the record, GNU true is a 12 KB binary, and claims here (Ubuntu 7.10) to be on version 5.97.
 
12KB for true?!?!

I used to right accounting applications that ran in less than that.
 
s/right/write/

*blush*
 
That is the best code i have seen in years (Reference: Coding Horror: The Best Code is No Code At All)
 




<< Home
Reg Braithwaite


Recent Writing
Homoiconic Technical Writing / raganwald.posterous.com

Books
What I‘ve Learned From Failure / Kestrels, Quirky Birds, and Hopeless Egocentricity

Share
rewrite_rails / andand / unfold.rb / string_to_proc.rb / dsl_and_let.rb / comprehension.rb / lazy_lists.rb

Beauty
IS-STRICTLY-EQUIVALENT-TO-A / Spaghetti-Western Coding / Golf is a good program spoiled / Programming conventions as signals / Not all functions should be object methods

The Not So Big Software Design / Writing programs for people to read / Why Why Functional Programming Matters Matters / But Y would I want to do a thing like this?

Work
The single most important thing you must do to improve your programming career / The Naïve Approach to Hiring People / No Disrespect / Take control of your interview / Three tips for getting a job through a recruiter / My favourite interview question

Management
Exception Handling in Software Development / What if powerful languages and idioms only work for small teams? / Bricks / Which theory fits the evidence? / Still failing, still learning / What I’ve learned from failure

Notation
The unary ampersand in Ruby / (1..100).inject(&:+) / The challenge of teaching yourself a programming language / The significance of the meta-circular interpreter / Block-Structured Javascript / Haskell, Ruby and Infinity / Closures and Higher-Order Functions

Opinion
Why Apple is more expensive than Amazon / Why we are the biggest obstacles to our own growth / Is software the documentation of business process mistakes? / We have lost control of the apparatus / What I’ve Learned From Sales I, II, III

Whimsey
The Narcissism of Small Code Differences / Billy Martin’s Technique for Managing his Manager / Three stories about The Tao / Programming Language Stories / Why You Need a Degree to Work For BigCo

History
06/04 / 07/04 / 08/04 / 09/04 / 10/04 / 11/04 / 12/04 / 01/05 / 02/05 / 03/05 / 04/05 / 06/05 / 07/05 / 08/05 / 09/05 / 10/05 / 11/05 / 01/06 / 02/06 / 03/06 / 04/06 / 05/06 / 06/06 / 07/06 / 08/06 / 09/06 / 10/06 / 11/06 / 12/06 / 01/07 / 02/07 / 03/07 / 04/07 / 05/07 / 06/07 / 07/07 / 08/07 / 09/07 / 10/07 / 11/07 / 12/07 / 01/08 / 02/08 / 03/08 / 04/08 / 05/08 / 06/08 / 07/08 /