Self reproducing programs
Self reproducing programs of the first kind: quines
Self reproducing programs are programs that reproduce the source they originate from, see also quine
A very simple example:
#!/bin/bash
# this is a self reproducing program
cat $0
In fact, this example cheats, it is making use of the fact the the source is available, and it asks the operating system to produce a copy of that source to standard output.
On this page some examples of real self reproducing shell scripts is available, for example this one:
#! /bin/sh
q="'" qq='echo \#! /bin/sh;echo q=\"$q\" qq=${q}$qq$q;echo eval $qq'
eval $qq
A nice story about self reproducing zips is here .
A comprehensive article on self reproducing programs is here .
I guess, that the mathematicians among us can read about Kleene's recursion theorem but that article is far beyond my capabilities.
In the year 2004 I played with self reproducing programs, telling my boss that that was a very important thing to do: With a self reproducing program, the changes that the source gets lost are minimized.
Here I present a recipe that can be used to convert practically any program written in practically any language into a self-reproducing program.
Self reproducing programs of the second kind
Above is described how to generate a self reproducing program that consists of one source file. In practice, however, most programs consist of more files and folders and it would be nice to make them self-replicating also.
In the examples you will find an example, and you will see that it is easier to make a self replicator of the second kind than making a self replicator of the first kind, in fact: it is almost trivial. Despite the triviality, I never saw an example so here is one.
It works as follows:
Create something that is able to generate an representation of the tar-ball of the source tree. This representation should not to go in the tar-ball. The representation must be usable by the language of your choice to produce the tar-ball, be it in the form of a regular file or on standard output. (In stead of a tar-ball you can opt for a zip file or something else that is appropriate). And then, take care that this representation is used to generate the tar-ball when the program is run with a special flag, environment variable, time of day, or whatever you like.
The example will make things more clear.
Btw: the program xsnow is
also a self replicating program of the second kind, there
the result of make dist
(a target generated by
automake) is used to generate the tar-ball.