]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - rain/rain.c
Eliminate a compiler warning.
[bsdgames-darwin.git] / rain / rain.c
1 /*
2 * Copyright (c) 1980 Regents of the University of California.
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
13 * 3. All advertising materials mentioning features or use of this software
14 * must display the following acknowledgement:
15 * This product includes software developed by the University of
16 * California, Berkeley and its contributors.
17 * 4. Neither the name of the University nor the names of its contributors
18 * may be used to endorse or promote products derived from this software
19 * without specific prior written permission.
20 *
21 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
22 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
25 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
26 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
27 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
28 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
29 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31 * SUCH DAMAGE.
32 */
33
34 #ifndef lint
35 char copyright[] =
36 "@(#) Copyright (c) 1980 Regents of the University of California.\n\
37 All rights reserved.\n";
38 #endif /* not lint */
39
40 #ifndef lint
41 /*static char sccsid[] = "from: @(#)rain.c 5.6 (Berkeley) 2/28/91";*/
42 static char rcsid[] = "$Id: rain.c,v 1.3 1993/12/08 08:18:22 mycroft Exp $";
43 #endif /* not lint */
44
45 /*
46 * rain 11/3/1980 EPS/CITHEP
47 * cc rain.c -o rain -O -ltermlib
48 */
49
50 #include <sys/types.h>
51 #include <stdio.h>
52 #ifdef USG
53 #include <termio.h>
54 #else
55 #include <sgtty.h>
56 #endif
57 #include <signal.h>
58
59 #define cursor(c, r) tputs(tgoto(CM, c, r), 1, fputchar)
60
61 #ifdef USG
62 static struct termio sg, old_tty;
63 #else
64 static struct sgttyb sg, old_tty;
65 #endif
66
67 static void fputchar();
68 char *LL, *TE, *tgoto();
69
70 main(argc, argv)
71 int argc;
72 char **argv;
73 {
74 extern short ospeed;
75 extern char *UP;
76 register int x, y, j;
77 register char *CM, *BC, *DN, *ND, *term;
78 char *TI, *tcp, *mp, tcb[100],
79 *malloc(), *getenv(), *strcpy(), *tgetstr();
80 long cols, lines, random();
81 int xpos[5], ypos[5];
82 static void onsig();
83
84 if (!(term = getenv("TERM"))) {
85 fprintf(stderr, "%s: TERM: parameter not set\n", *argv);
86 exit(1);
87 }
88 if (!(mp = malloc((u_int)1024))) {
89 fprintf(stderr, "%s: out of space.\n", *argv);
90 exit(1);
91 }
92 if (tgetent(mp, term) <= 0) {
93 fprintf(stderr, "%s: %s: unknown terminal type\n", *argv, term);
94 exit(1);
95 }
96 tcp = tcb;
97 if (!(CM = tgetstr("cm", &tcp))) {
98 fprintf(stderr, "%s: terminal not capable of cursor motion\n", *argv);
99 exit(1);
100 }
101 if (!(BC = tgetstr("bc", &tcp)))
102 BC = "\b";
103 if (!(DN = tgetstr("dn", &tcp)))
104 DN = "\n";
105 if (!(ND = tgetstr("nd", &tcp)))
106 ND = " ";
107 if ((cols = tgetnum("co")) == -1)
108 cols = 80;
109 if ((lines = tgetnum("li")) == -1)
110 lines = 24;
111 cols -= 4;
112 lines -= 4;
113 TE = tgetstr("te", &tcp);
114 TI = tgetstr("ti", &tcp);
115 UP = tgetstr("up", &tcp);
116 if (!(LL = tgetstr("ll", &tcp))) {
117 if (!(LL = malloc((u_int)10))) {
118 fprintf(stderr, "%s: out of space.\n", *argv);
119 exit(1);
120 }
121 (void)strcpy(LL, tgoto(CM, 0, 23));
122 }
123 #ifdef USG
124 ioctl(1, TCGETA, &sg);
125 ospeed = sg.c_cflag&CBAUD;
126 #else
127 gtty(1, &sg);
128 ospeed = sg.sg_ospeed;
129 #endif
130 (void)signal(SIGHUP, onsig);
131 (void)signal(SIGINT, onsig);
132 (void)signal(SIGQUIT, onsig);
133 (void)signal(SIGSTOP, onsig);
134 (void)signal(SIGTSTP, onsig);
135 (void)signal(SIGTERM, onsig);
136 #ifdef USG
137 ioctl(1, TCGETA, &old_tty); /* save tty bits for exit */
138 ioctl(1, TCGETA, &sg);
139 sg.c_iflag &= ~ICRNL;
140 sg.c_oflag &= ~ONLCR;
141 sg.c_lflag &= ~ECHO;
142 ioctl(1, TCSETAW, &sg);
143 #else
144 gtty(1, &old_tty); /* save tty bits for exit */
145 gtty(1, &sg);
146 sg.sg_flags &= ~(CRMOD|ECHO);
147 stty(1, &sg);
148 #endif
149 if (TI)
150 tputs(TI, 1, fputchar);
151 tputs(tgetstr("cl", &tcp), 1, fputchar);
152 (void)fflush(stdout);
153 for (j = 4; j >= 0; --j) {
154 xpos[j] = random() % cols + 2;
155 ypos[j] = random() % lines + 2;
156 }
157 for (j = 0;;) {
158 x = random() % cols + 2;
159 y = random() % lines + 2;
160 cursor(x, y);
161 fputchar('.');
162 cursor(xpos[j], ypos[j]);
163 fputchar('o');
164 if (!j--)
165 j = 4;
166 cursor(xpos[j], ypos[j]);
167 fputchar('O');
168 if (!j--)
169 j = 4;
170 cursor(xpos[j], ypos[j] - 1);
171 fputchar('-');
172 tputs(DN, 1, fputchar);
173 tputs(BC, 1, fputchar);
174 tputs(BC, 1, fputchar);
175 fputs("|.|", stdout);
176 tputs(DN, 1, fputchar);
177 tputs(BC, 1, fputchar);
178 tputs(BC, 1, fputchar);
179 fputchar('-');
180 if (!j--)
181 j = 4;
182 cursor(xpos[j], ypos[j] - 2);
183 fputchar('-');
184 tputs(DN, 1, fputchar);
185 tputs(BC, 1, fputchar);
186 tputs(BC, 1, fputchar);
187 fputs("/ \\", stdout);
188 cursor(xpos[j] - 2, ypos[j]);
189 fputs("| O |", stdout);
190 cursor(xpos[j] - 1, ypos[j] + 1);
191 fputs("\\ /", stdout);
192 tputs(DN, 1, fputchar);
193 tputs(BC, 1, fputchar);
194 tputs(BC, 1, fputchar);
195 fputchar('-');
196 if (!j--)
197 j = 4;
198 cursor(xpos[j], ypos[j] - 2);
199 fputchar(' ');
200 tputs(DN, 1, fputchar);
201 tputs(BC, 1, fputchar);
202 tputs(BC, 1, fputchar);
203 fputchar(' ');
204 tputs(ND, 1, fputchar);
205 fputchar(' ');
206 cursor(xpos[j] - 2, ypos[j]);
207 fputchar(' ');
208 tputs(ND, 1, fputchar);
209 fputchar(' ');
210 tputs(ND, 1, fputchar);
211 fputchar(' ');
212 cursor(xpos[j] - 1, ypos[j] + 1);
213 fputchar(' ');
214 tputs(ND, 1, fputchar);
215 fputchar(' ');
216 tputs(DN, 1, fputchar);
217 tputs(BC, 1, fputchar);
218 tputs(BC, 1, fputchar);
219 fputchar(' ');
220 xpos[j] = x;
221 ypos[j] = y;
222 (void)fflush(stdout);
223 }
224 }
225
226 static void
227 onsig()
228 {
229 tputs(LL, 1, fputchar);
230 if (TE)
231 tputs(TE, 1, fputchar);
232 (void)fflush(stdout);
233 #ifdef USG
234 ioctl(1, TCSETAW, &old_tty);
235 #else
236 stty(1, &old_tty);
237 #endif
238 exit(0);
239 }
240
241 static void
242 fputchar(c)
243 char c;
244 {
245 putchar(c);
246 }