| 1 | Quine |
| 2 | ~~~~~ |
| 3 | |
| 4 | The `Jargon File' (well, my GNU Info edition) defines a `quine' as: |
| 5 | |
| 6 | :quine: /kwi:n/ n. [from the name of the logician Willard |
| 7 | van Orman Quine, via Douglas Hofstadter] A program that generates a |
| 8 | copy of its own source text as its complete output. Devising the |
| 9 | shortest possible quine in some given programming language is a |
| 10 | common hackish amusement. Here is one classic quine: |
| 11 | |
| 12 | ((lambda (x) |
| 13 | (list x (list (quote quote) x))) |
| 14 | (quote |
| 15 | (lambda (x) |
| 16 | (list x (list (quote quote) x))))) |
| 17 | |
| 18 | This one works in LISP or Scheme. It's relatively easy to write |
| 19 | quines in other languages such as Postscript which readily handle |
| 20 | programs as data; much harder (and thus more challenging!) in |
| 21 | languages like C which do not. Here is a classic C quine for ASCII |
| 22 | machines: |
| 23 | |
| 24 | char*f="char*f=%c%s%c;main() |
| 25 | {printf(f,34,f,34,10);}%c"; |
| 26 | main(){printf(f,34,f,34,10);} |
| 27 | |
| 28 | For excruciatingly exact quinishness, remove the interior line |
| 29 | breaks. Some infamous *note Obfuscated C Contest:: entries have been |
| 30 | quines that reproduced in exotic ways. |
| 31 | |
| 32 | Small quines, like the one above (or the author's strictly conformant |
| 33 | ANSI C version) have been around for ages. However, I'm not aware of |
| 34 | any programs which provide quine support for arbitrary pieces of |
| 35 | software. The `quine' program is intended to remedy this omission. |
| 36 | |
| 37 | Building the package is easy. Run the `configure' script, and then say |
| 38 | `make'. This then gowes through the rather convoluted build process |
| 39 | automatically, and produces a binary `quine'. |
| 40 | |
| 41 | As an example of `quine's ability to enquine programs, it is (of course) |
| 42 | a quine itself. To demonstrate this, run the command |
| 43 | |
| 44 | ./quine --quine |
| 45 | |
| 46 | It should (very quickly) create a `quine' source distribution in the |
| 47 | current directory. |
| 48 | |
| 49 | Conferring quineishness upon other programs isn't hard. You require the |
| 50 | `quine.h' file from the source distribution (`make install' will install |
| 51 | it in some standard place for you), and the `quine' binary. |
| 52 | |
| 53 | The `quine' program works by writing a file describing the contents of |
| 54 | your source files, and the code necessary to recreate them. It reads |
| 55 | the names of your sources from standard input and writes (by default) to |
| 56 | `qqout.c'; thus an incantation like |
| 57 | |
| 58 | find . -type f -print | quine |
| 59 | |
| 60 | should be sufficient to build the `qqout.c' file. Then all you need to |
| 61 | do to your code to output the source code is call `qq_create' to build a |
| 62 | distribution directory, or `qq_dump(STREAM)' to dump the source code to |
| 63 | the output stream STREAM. Easy, no? |
| 64 | |
| 65 | The author's own preference for creating source distributions is to |
| 66 | create a distribution directory (with `make distdir') and use `find' |
| 67 | within that to build the `qqout.c' file. See the `Makefile.am' to see |
| 68 | how this was achieved for `quine' itself. (Note that `quine' has to use |
| 69 | itself to build its `qqout.c' file, which makes the whole procedure a |
| 70 | little more interesting.) |
| 71 | |
| 72 | I'll admit it: this isn't what you'd call a `useful' program. |