2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#) Copyright (c) 1990, 1993 The Regents of the University of California. All rights reserved.
33 * @(#)main.c 8.1 (Berkeley) 5/31/93
34 * $FreeBSD: src/games/atc/main.c,v 1.9 1999/11/30 03:48:21 billf Exp $
35 * $DragonFly: src/games/atc/main.c,v 1.4 2006/10/08 17:11:30 pavalos Exp $
39 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
41 * Copy permission is hereby granted provided that this notice is
42 * retained on all partial or complete copies.
44 * For more info on this and all of my stuff, mail edjames@berkeley.edu.
49 #include "pathnames.h"
52 extern int yyparse(void);
53 static int read_file(const char *);
54 static const char *default_game(void);
55 static const char *okay_game(const char *);
56 static int list_games(void);
59 main(int argc __unused, char *argv[])
62 int f_usage = 0, f_list = 0, f_showscore = 0;
64 const char *file = NULL;
68 /* Open the score file then revoke setgid privileges */
70 setregid(getgid(), getgid());
100 srandom(atoi(*argv));
110 fprintf(stderr, "Unknown option '%c'\n", *ptr);
122 "Usage: %s -[u?lstp] [-[gf] game_name] [-r random seed]\n",
131 strcpy(buf, _PATH_GAMES);
132 buf[strlen(buf) - 1] = '\0';
136 if (f_usage || f_showscore || f_list || f_printpath)
140 file = default_game();
142 file = okay_game(file);
144 if (file == NULL || read_file(file) < 0)
152 signal(SIGINT, (sig_t)quit);
153 signal(SIGQUIT, (sig_t)quit);
154 signal(SIGTSTP, SIG_IGN);
155 signal(SIGSTOP, SIG_IGN);
156 signal(SIGHUP, (sig_t)log_score);
157 signal(SIGTERM, (sig_t)log_score);
159 tcgetattr(fileno(stdin), &tty_start);
160 bcopy(&tty_start, &tty_new, sizeof(tty_new));
161 tty_new.c_lflag &= ~(ICANON|ECHO);
162 tty_new.c_cc[VMIN] = 1;
163 tty_new.c_cc[VTIME] = 0;
164 tcsetattr(fileno(stdin), TCSANOW, &tty_new);
165 signal(SIGALRM, (sig_t)update);
167 itv.it_value.tv_sec = 0;
168 itv.it_value.tv_usec = 1;
169 itv.it_interval.tv_sec = sp->update_secs;
170 itv.it_interval.tv_usec = 0;
171 setitimer(ITIMER_REAL, &itv, NULL);
174 if (getcommand() != 1)
177 itv.it_value.tv_sec = 0;
178 itv.it_value.tv_usec = 0;
179 setitimer(ITIMER_REAL, &itv, NULL);
183 itv.it_value.tv_sec = sp->update_secs;
184 itv.it_value.tv_usec = 0;
185 itv.it_interval.tv_sec = sp->update_secs;
186 itv.it_interval.tv_usec = 0;
187 setitimer(ITIMER_REAL, &itv, NULL);
193 read_file(const char *s)
198 yyin = fopen(s, "r");
216 static char file[256];
217 char line[256], games[256];
219 strcpy(games, _PATH_GAMES);
220 strcat(games, GAMES);
222 if ((fp = fopen(games, "r")) == NULL) {
226 if (fgets(line, sizeof(line), fp) == NULL) {
227 fprintf(stderr, "%s: no default game available\n", games);
231 line[strlen(line) - 1] = '\0';
232 strcpy(file, _PATH_GAMES);
238 okay_game(const char *s)
241 static char file[256];
242 const char *ret = NULL;
243 char line[256], games[256];
245 strcpy(games, _PATH_GAMES);
246 strcat(games, GAMES);
248 if ((fp = fopen(games, "r")) == NULL) {
252 while (fgets(line, sizeof(line), fp) != NULL) {
253 line[strlen(line) - 1] = '\0';
254 if (strcmp(s, line) == 0) {
255 strcpy(file, _PATH_GAMES);
265 fprintf(stderr, "%s: %s: game not found\n", games, s);
266 fprintf(stderr, "Your score will not be logged.\n");
267 sleep(2); /* give the guy time to read it */
276 char line[256], games[256];
279 strcpy(games, _PATH_GAMES);
280 strcat(games, GAMES);
282 if ((fp = fopen(games, "r")) == NULL) {
286 puts("available games:");
287 while (fgets(line, sizeof(line), fp) != NULL) {
292 if (num_games == 0) {
293 fprintf(stderr, "%s: no games available\n", games);