4.4BSD snapshot (revision 8.1); add 1993 to copyright
[unix-history] / usr / src / games / rain / rain.c
CommitLineData
e0bbfbf9 1/*
2dd6548c
KB
2 * Copyright (c) 1980, 1993
3 * The Regents of the University of California. All rights reserved.
a9361314 4 *
abde58c2 5 * %sccs.include.redist.c%
e0bbfbf9 6 */
d5bda5f3 7
e0bbfbf9 8#ifndef lint
2dd6548c
KB
9static char copyright[] =
10"@(#) Copyright (c) 1980, 1993\n\
11 The Regents of the University of California. All rights reserved.\n";
a9361314 12#endif /* not lint */
e0bbfbf9
DF
13
14#ifndef lint
2dd6548c 15static char sccsid[] = "@(#)rain.c 8.1 (Berkeley) %G%";
a9361314 16#endif /* not lint */
d5bda5f3 17
a9361314
KB
18/*
19 * rain 11/3/1980 EPS/CITHEP
20 * cc rain.c -o rain -O -ltermlib
21 */
4334b5fc 22
a9361314 23#include <sys/types.h>
d5bda5f3 24#include <stdio.h>
4334b5fc
KM
25#ifdef USG
26#include <termio.h>
27#else
d5bda5f3 28#include <sgtty.h>
4334b5fc 29#endif
d5bda5f3 30#include <signal.h>
a9361314
KB
31
32#define cursor(c, r) tputs(tgoto(CM, c, r), 1, fputchar)
33
4334b5fc 34#ifdef USG
a9361314 35static struct termio sg, old_tty;
4334b5fc 36#else
a9361314 37static struct sgttyb sg, old_tty;
4334b5fc 38#endif
a9361314
KB
39
40int fputchar();
41char *LL, *TE, *tgoto();
42
43main(argc, argv)
44 int argc;
45 char **argv;
d5bda5f3 46{
a9361314
KB
47 extern short ospeed;
48 extern char *UP;
49 register int x, y, j;
50 register char *CM, *BC, *DN, *ND, *term;
51 char *TI, *tcp, *mp, tcb[100],
52 *malloc(), *getenv(), *strcpy(), *tgetstr();
53 long cols, lines, random();
786b3585
KB
54 int xpos[5], ypos[5];
55 static void onsig();
4334b5fc 56
a9361314
KB
57 if (!(term = getenv("TERM"))) {
58 fprintf(stderr, "%s: TERM: parameter not set\n", *argv);
59 exit(1);
60 }
61 if (!(mp = malloc((u_int)1024))) {
62 fprintf(stderr, "%s: out of space.\n", *argv);
63 exit(1);
64 }
65 if (tgetent(mp, term) <= 0) {
66 fprintf(stderr, "%s: %s: unknown terminal type\n", *argv, term);
67 exit(1);
68 }
69 tcp = tcb;
70 if (!(CM = tgetstr("cm", &tcp))) {
71 fprintf(stderr, "%s: terminal not capable of cursor motion\n", *argv);
72 exit(1);
73 }
74 if (!(BC = tgetstr("bc", &tcp)))
75 BC = "\b";
76 if (!(DN = tgetstr("dn", &tcp)))
77 DN = "\n";
78 if (!(ND = tgetstr("nd", &tcp)))
79 ND = " ";
80 if ((cols = tgetnum("co")) == -1)
81 cols = 80;
82 if ((lines = tgetnum("li")) == -1)
83 lines = 24;
84 cols -= 4;
85 lines -= 4;
86 TE = tgetstr("te", &tcp);
87 TI = tgetstr("ti", &tcp);
88 UP = tgetstr("up", &tcp);
89 if (!(LL = tgetstr("ll", &tcp))) {
90 if (!(LL = malloc((u_int)10))) {
91 fprintf(stderr, "%s: out of space.\n", *argv);
92 exit(1);
93 }
94 (void)strcpy(LL, tgoto(CM, 0, 23));
95 }
4334b5fc 96#ifdef USG
a9361314
KB
97 ioctl(1, TCGETA, &sg);
98 ospeed = sg.c_cflag&CBAUD;
4334b5fc 99#else
a9361314
KB
100 gtty(1, &sg);
101 ospeed = sg.sg_ospeed;
4334b5fc 102#endif
a9361314
KB
103 (void)signal(SIGHUP, onsig);
104 (void)signal(SIGINT, onsig);
105 (void)signal(SIGQUIT, onsig);
106 (void)signal(SIGSTOP, onsig);
107 (void)signal(SIGTSTP, onsig);
108 (void)signal(SIGTERM, onsig);
4334b5fc 109#ifdef USG
a9361314
KB
110 ioctl(1, TCGETA, &old_tty); /* save tty bits for exit */
111 ioctl(1, TCGETA, &sg);
112 sg.c_iflag &= ~ICRNL;
113 sg.c_oflag &= ~ONLCR;
114 sg.c_lflag &= ~ECHO;
115 ioctl(1, TCSETAW, &sg);
4334b5fc 116#else
a9361314
KB
117 gtty(1, &old_tty); /* save tty bits for exit */
118 gtty(1, &sg);
119 sg.sg_flags &= ~(CRMOD|ECHO);
120 stty(1, &sg);
4334b5fc 121#endif
a9361314
KB
122 if (TI)
123 tputs(TI, 1, fputchar);
124 tputs(tgetstr("cl", &tcp), 1, fputchar);
125 (void)fflush(stdout);
126 for (j = 4; j >= 0; --j) {
127 xpos[j] = random() % cols + 2;
128 ypos[j] = random() % lines + 2;
129 }
130 for (j = 0;;) {
131 x = random() % cols + 2;
132 y = random() % lines + 2;
133 cursor(x, y);
134 fputchar('.');
135 cursor(xpos[j], ypos[j]);
136 fputchar('o');
137 if (!j--)
138 j = 4;
139 cursor(xpos[j], ypos[j]);
140 fputchar('O');
141 if (!j--)
142 j = 4;
143 cursor(xpos[j], ypos[j] - 1);
144 fputchar('-');
145 tputs(DN, 1, fputchar);
146 tputs(BC, 1, fputchar);
147 tputs(BC, 1, fputchar);
148 fputs("|.|", stdout);
149 tputs(DN, 1, fputchar);
150 tputs(BC, 1, fputchar);
151 tputs(BC, 1, fputchar);
152 fputchar('-');
153 if (!j--)
154 j = 4;
155 cursor(xpos[j], ypos[j] - 2);
156 fputchar('-');
157 tputs(DN, 1, fputchar);
158 tputs(BC, 1, fputchar);
159 tputs(BC, 1, fputchar);
160 fputs("/ \\", stdout);
161 cursor(xpos[j] - 2, ypos[j]);
162 fputs("| O |", stdout);
163 cursor(xpos[j] - 1, ypos[j] + 1);
164 fputs("\\ /", stdout);
165 tputs(DN, 1, fputchar);
166 tputs(BC, 1, fputchar);
167 tputs(BC, 1, fputchar);
168 fputchar('-');
169 if (!j--)
170 j = 4;
171 cursor(xpos[j], ypos[j] - 2);
172 fputchar(' ');
173 tputs(DN, 1, fputchar);
174 tputs(BC, 1, fputchar);
175 tputs(BC, 1, fputchar);
176 fputchar(' ');
177 tputs(ND, 1, fputchar);
178 fputchar(' ');
179 cursor(xpos[j] - 2, ypos[j]);
180 fputchar(' ');
181 tputs(ND, 1, fputchar);
182 fputchar(' ');
183 tputs(ND, 1, fputchar);
184 fputchar(' ');
185 cursor(xpos[j] - 1, ypos[j] + 1);
186 fputchar(' ');
187 tputs(ND, 1, fputchar);
188 fputchar(' ');
189 tputs(DN, 1, fputchar);
190 tputs(BC, 1, fputchar);
191 tputs(BC, 1, fputchar);
192 fputchar(' ');
193 xpos[j] = x;
194 ypos[j] = y;
195 (void)fflush(stdout);
196 }
d5bda5f3 197}
a9361314 198
786b3585 199static void
a9361314 200onsig()
d5bda5f3 201{
a9361314
KB
202 tputs(LL, 1, fputchar);
203 if (TE)
204 tputs(TE, 1, fputchar);
205 (void)fflush(stdout);
4334b5fc 206#ifdef USG
a9361314 207 ioctl(1, TCSETAW, &old_tty);
4334b5fc 208#else
a9361314 209 stty(1, &old_tty);
4334b5fc 210#endif
a9361314 211 exit(0);
d5bda5f3 212}
a9361314 213
308751ef 214int
d5bda5f3 215fputchar(c)
308751ef 216 int c;
d5bda5f3 217{
a9361314 218 putchar(c);
d5bda5f3 219}