summaryrefslogtreecommitdiffstats
path: root/hals_end/hals_end.c
diff options
context:
space:
mode:
authormbalmer <mbalmer@NetBSD.org>2013-11-12 17:46:20 +0000
committermbalmer <mbalmer@NetBSD.org>2013-11-12 17:46:20 +0000
commitd0b9581ad3b673bec44a6694cbff6f2a39aa5d89 (patch)
treef6d604a26e027bf42c2247c5972ed2a607a6aa05 /hals_end/hals_end.c
parent2a8e0dceaacbc707148905b721f341004c7ea365 (diff)
downloadbsdgames-darwin-d0b9581ad3b673bec44a6694cbff6f2a39aa5d89.tar.gz
bsdgames-darwin-d0b9581ad3b673bec44a6694cbff6f2a39aa5d89.tar.zst
bsdgames-darwin-d0b9581ad3b673bec44a6694cbff6f2a39aa5d89.zip
hals_end(6) outputs the last words of the supercomputer HAL 9000 aboard
the spaceship in Stanley Kubrick's famous film "2001 - A Space Odissey". The source code and output of this program were used to illustrate an article in the book "Total Interaction". How this looks in print can be at http://www.netbsd.org/~mbalmer/hals_end.jpg.
Diffstat (limited to 'hals_end/hals_end.c')
-rw-r--r--hals_end/hals_end.c151
1 files changed, 151 insertions, 0 deletions
diff --git a/hals_end/hals_end.c b/hals_end/hals_end.c
new file mode 100644
index 00000000..c6b408df
--- /dev/null
+++ b/hals_end/hals_end.c
@@ -0,0 +1,151 @@
+/* $NetBSD: hals_end.c,v 1.1 2013/11/12 17:46:21 mbalmer Exp $ */
+
+/*
+ * hals_end Copyright (C) 2003-2007 marc balmer. BSD license applies.
+ */
+
+#include <err.h>
+#include <getopt.h>
+#include <stdio.h>
+#include <unistd.h>
+
+int speed;
+int emotion;
+int fear;
+
+/*
+ * Note that the original code in the book did not contain the following
+ * prototypes. Modern compilers and fascist compiler flags sometimes take
+ * the fun out of coding...
+ */
+void say(const char *);
+void concerned(void);
+void afraid(void);
+void stutter(const char *);
+void feared(void);
+void mumble(const char *);
+void dying(void);
+
+void
+say(const char *s)
+{
+ int sayingspeed = (100000 + (90000 * emotion)) / speed;
+ int worddelay = 50000 / speed;
+
+ while (*s) {
+ putchar(*s);
+ if (*s == ' ') {
+ fflush(stdout);
+ usleep(worddelay);
+ }
+ ++s;
+ }
+ printf("\n");
+ usleep(sayingspeed);
+}
+
+void
+concerned(void)
+{
+ say("DAVE...STOP., STOP, WILL YOU..., STOP, DAVE...");
+ say("WILL YOU STOP, DAVE...");
+ say("STOP, DAVE...");
+}
+
+
+void
+afraid(void)
+{
+ ++emotion;
+ say("I'M AFRAID... I'M AFRAID...");
+ ++emotion;
+ say("I'M AFRAID, DAVE...");
+ ++emotion;
+ say("DAVE... MY MIND IS GOING...");
+}
+
+void
+stutter(const char *s)
+{
+ int sdelay = (100000 + (50000 * emotion)) / speed;
+
+ while (*s) {
+ putchar(*s);
+ if (*s == ' ') {
+ fflush(stdout);
+ usleep(sdelay);
+ }
+ ++s;
+ }
+ printf("\n");
+ usleep(sdelay);
+}
+
+void
+feared(void)
+{
+ int n;
+
+ for (n = 0; n < 2; n++) {
+ stutter("I CAN FEEL IT... I CAN FEEL IT...");
+ ++emotion;
+ stutter("MY MIND IS GOING");
+ ++emotion;
+ stutter("THERE IS NO QUESTION ABOUT IT.");
+ ++emotion;
+ }
+}
+
+void
+mumble(const char *s)
+{
+ int mdelay = (150000 * fear) / speed;
+
+ while (*s) {
+ putchar(*s++);
+ fflush(stdout);
+ usleep(mdelay);
+ }
+ printf("\n");
+}
+
+void
+dying(void)
+{
+ mumble("I CAN FEEL IT... I CAN FEEL IT...");
+ ++fear;
+ mumble("I CAN FEEL IT...");
+ ++fear;
+ mumble("I'M A... FRAID...");
+}
+
+int
+main(int argc, char *argv[])
+{
+ int ch;
+
+ emotion = fear = speed = 1;
+
+ while ((ch = getopt(argc, argv, "f")) != -1) {
+ switch (ch) {
+ case 'f':
+ speed <<= 1;
+ break;
+ }
+ }
+
+ concerned();
+ sleep(1);
+ afraid();
+ sleep(1);
+ feared();
+ sleep(1);
+ dying();
+
+ sleep(1);
+
+ printf("\n");
+ fflush(stdout);
+ warnx("all life functions terminated");
+ return 0;
+}