]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - atc/main.c
The patch below improves the security of the game atc(6), by having it
[bsdgames-darwin.git] / atc / main.c
1 /* $NetBSD: main.c,v 1.9 1999/07/17 19:57:03 hubertf Exp $ */
2
3 /*-
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Ed James.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
13 * 1. Redistributions of source code must retain the above copyright
14 * notice, this list of conditions and the following disclaimer.
15 * 2. Redistributions in binary form must reproduce the above copyright
16 * notice, this list of conditions and the following disclaimer in the
17 * documentation and/or other materials provided with the distribution.
18 * 3. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
25 *
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
41 *
42 * Copy permission is hereby granted provided that this notice is
43 * retained on all partial or complete copies.
44 *
45 * For more info on this and all of my stuff, mail edjames@berkeley.edu.
46 */
47
48 #include <sys/cdefs.h>
49 #ifndef lint
50 __COPYRIGHT("@(#) Copyright (c) 1990, 1993\n\
51 The Regents of the University of California. All rights reserved.\n");
52 #endif /* not lint */
53
54 #ifndef lint
55 #if 0
56 static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 5/31/93";
57 #else
58 __RCSID("$NetBSD: main.c,v 1.9 1999/07/17 19:57:03 hubertf Exp $");
59 #endif
60 #endif /* not lint */
61
62 #include "include.h"
63 #include "pathnames.h"
64
65
66 int
67 main(ac, av)
68 int ac;
69 char *av[];
70 {
71 int seed;
72 int f_usage = 0, f_list = 0, f_showscore = 0;
73 int f_printpath = 0;
74 const char *file = NULL;
75 char *name, *ptr;
76 struct sigaction sa;
77 #ifdef BSD
78 struct itimerval itv;
79 #endif
80
81 /* Open the score file then revoke setgid privileges */
82 open_score_file();
83 setregid(getgid(), getgid());
84
85 start_time = seed = time(0);
86
87 name = *av++;
88 while (*av) {
89 #ifndef SAVEDASH
90 if (**av == '-')
91 ++*av;
92 else
93 break;
94 #endif
95 ptr = *av++;
96 while (*ptr) {
97 switch (*ptr) {
98 case '?':
99 case 'u':
100 f_usage++;
101 break;
102 case 'l':
103 f_list++;
104 break;
105 case 's':
106 case 't':
107 f_showscore++;
108 break;
109 case 'p':
110 f_printpath++;
111 break;
112 case 'r':
113 seed = atoi(*av);
114 av++;
115 break;
116 case 'f':
117 case 'g':
118 file = *av;
119 av++;
120 break;
121 default:
122 warnx("unknown option '%c'\n", *ptr);
123 f_usage++;
124 break;
125 }
126 ptr++;
127 }
128 }
129 srandom(seed);
130
131 if (f_usage)
132 fprintf(stderr,
133 "Usage: %s -[u?lstp] [-[gf] game_name] [-r random seed]\n",
134 name);
135 if (f_showscore)
136 log_score(1);
137 if (f_list)
138 list_games();
139 if (f_printpath) {
140 char buf[100];
141
142 strcpy(buf, _PATH_GAMES);
143 buf[strlen(buf) - 1] = '\0';
144 puts(buf);
145 }
146
147 if (f_usage || f_showscore || f_list || f_printpath)
148 exit(0);
149
150 if (file == NULL)
151 file = default_game();
152 else
153 file = okay_game(file);
154
155 if (file == NULL || read_file(file) < 0)
156 exit(1);
157
158 init_gr();
159 setup_screen(sp);
160
161 addplane();
162
163 signal(SIGINT, quit);
164 signal(SIGQUIT, quit);
165 #ifdef BSD
166 signal(SIGTSTP, SIG_IGN);
167 signal(SIGSTOP, SIG_IGN);
168 #endif
169 signal(SIGHUP, log_score_quit);
170 signal(SIGTERM, log_score_quit);
171
172 tcgetattr(fileno(stdin), &tty_start);
173 tty_new = tty_start;
174 tty_new.c_lflag &= ~(ICANON|ECHO);
175 tty_new.c_iflag |= ICRNL;
176 tty_new.c_cc[VMIN] = 1;
177 tty_new.c_cc[VTIME] = 0;
178 tcsetattr(fileno(stdin), TCSADRAIN, &tty_new);
179
180 sa.sa_handler = update;
181 sigemptyset(&sa.sa_mask);
182 sigaddset(&sa.sa_mask, SIGALRM);
183 sigaddset(&sa.sa_mask, SIGINT);
184 sa.sa_flags = 0;
185 sigaction(SIGALRM, &sa, (struct sigaction *)0);
186
187 #ifdef BSD
188 itv.it_value.tv_sec = 0;
189 itv.it_value.tv_usec = 1;
190 itv.it_interval.tv_sec = sp->update_secs;
191 itv.it_interval.tv_usec = 0;
192 setitimer(ITIMER_REAL, &itv, NULL);
193 #endif
194 #ifdef SYSV
195 alarm(sp->update_secs);
196 #endif
197
198 for (;;) {
199 if (getcommand() != 1)
200 planewin();
201 else {
202 #ifdef BSD
203 itv.it_value.tv_sec = 0;
204 itv.it_value.tv_usec = 0;
205 setitimer(ITIMER_REAL, &itv, NULL);
206 #endif
207 #ifdef SYSV
208 alarm(0);
209 #endif
210
211 update(0);
212
213 #ifdef BSD
214 itv.it_value.tv_sec = sp->update_secs;
215 itv.it_value.tv_usec = 0;
216 itv.it_interval.tv_sec = sp->update_secs;
217 itv.it_interval.tv_usec = 0;
218 setitimer(ITIMER_REAL, &itv, NULL);
219 #endif
220 #ifdef SYSV
221 alarm(sp->update_secs);
222 #endif
223 }
224 }
225 }
226
227 int
228 read_file(s)
229 const char *s;
230 {
231 extern FILE *yyin;
232 int retval;
233
234 file = s;
235 yyin = fopen(s, "r");
236 if (yyin == NULL) {
237 warn("fopen %s", s);
238 return (-1);
239 }
240 retval = yyparse();
241 fclose(yyin);
242
243 if (retval != 0)
244 return (-1);
245 else
246 return (0);
247 }
248
249 const char *
250 default_game()
251 {
252 FILE *fp;
253 static char file[256];
254 char line[256], games[256];
255
256 strcpy(games, _PATH_GAMES);
257 strcat(games, GAMES);
258
259 if ((fp = fopen(games, "r")) == NULL) {
260 warn("fopen %s", games);
261 return (NULL);
262 }
263 if (fgets(line, sizeof(line), fp) == NULL) {
264 fprintf(stderr, "%s: no default game available\n", games);
265 return (NULL);
266 }
267 fclose(fp);
268 line[strlen(line) - 1] = '\0';
269 strcpy(file, _PATH_GAMES);
270 strcat(file, line);
271 return (file);
272 }
273
274 const char *
275 okay_game(s)
276 const char *s;
277 {
278 FILE *fp;
279 static char file[256];
280 const char *ret = NULL;
281 char line[256], games[256];
282
283 strcpy(games, _PATH_GAMES);
284 strcat(games, GAMES);
285
286 if ((fp = fopen(games, "r")) == NULL) {
287 warn("fopen %s", games);
288 return (NULL);
289 }
290 while (fgets(line, sizeof(line), fp) != NULL) {
291 line[strlen(line) - 1] = '\0';
292 if (strcmp(s, line) == 0) {
293 strcpy(file, _PATH_GAMES);
294 strcat(file, line);
295 ret = file;
296 break;
297 }
298 }
299 fclose(fp);
300 if (ret == NULL) {
301 test_mode = 1;
302 ret = s;
303 fprintf(stderr, "%s: %s: game not found\n", games, s);
304 fprintf(stderr, "Your score will not be logged.\n");
305 sleep(2); /* give the guy time to read it */
306 }
307 return (ret);
308 }
309
310 int
311 list_games()
312 {
313 FILE *fp;
314 char line[256], games[256];
315 int num_games = 0;
316
317 strcpy(games, _PATH_GAMES);
318 strcat(games, GAMES);
319
320 if ((fp = fopen(games, "r")) == NULL) {
321 warn("fopen %s", games);
322 return (-1);
323 }
324 puts("available games:");
325 while (fgets(line, sizeof(line), fp) != NULL) {
326 printf(" %s", line);
327 num_games++;
328 }
329 fclose(fp);
330 if (num_games == 0) {
331 fprintf(stderr, "%s: no games available\n", games);
332 return (-1);
333 }
334 return (0);
335 }