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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * @(#) Copyright (c) 1990, 1993 The Regents of the University of California. All rights reserved.
37 * @(#)main.c 8.1 (Berkeley) 5/31/93
38 * $FreeBSD: src/games/atc/main.c,v 1.9 1999/11/30 03:48:21 billf Exp $
39 * $DragonFly: src/games/atc/main.c,v 1.2 2003/06/17 04:25:22 dillon Exp $
43 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
45 * Copy permission is hereby granted provided that this notice is
46 * retained on all partial or complete copies.
48 * For more info on this and all of my stuff, mail edjames@berkeley.edu.
53 #include "pathnames.h"
60 int f_usage = 0, f_list = 0, f_showscore = 0;
62 const char *file = NULL;
67 extern const char *default_game(), *okay_game();
68 extern void log_score(), quit(), update();
70 /* Open the score file then revoke setgid privileges */
72 setregid(getgid(), getgid());
112 fprintf(stderr, "Unknown option '%c'\n", *ptr);
124 "Usage: %s -[u?lstp] [-[gf] game_name] [-r random seed]\n",
133 strcpy(buf, _PATH_GAMES);
134 buf[strlen(buf) - 1] = '\0';
138 if (f_usage || f_showscore || f_list || f_printpath)
142 file = default_game();
144 file = okay_game(file);
146 if (file == NULL || read_file(file) < 0)
154 signal(SIGINT, quit);
155 signal(SIGQUIT, quit);
157 signal(SIGTSTP, SIG_IGN);
158 signal(SIGSTOP, SIG_IGN);
160 signal(SIGHUP, log_score);
161 signal(SIGTERM, log_score);
164 ioctl(fileno(stdin), TIOCGETP, &tty_start);
165 bcopy(&tty_start, &tty_new, sizeof(tty_new));
166 tty_new.sg_flags |= CBREAK;
167 tty_new.sg_flags &= ~ECHO;
168 ioctl(fileno(stdin), TIOCSETP, &tty_new);
172 ioctl(fileno(stdin), TCGETA, &tty_start);
173 bcopy(&tty_start, &tty_new, sizeof(tty_new));
174 tty_new.c_lflag &= ~ICANON;
175 tty_new.c_lflag &= ~ECHO;
176 tty_new.c_cc[VMIN] = 1;
177 tty_new.c_cc[VTIME] = 0;
178 ioctl(fileno(stdin), TCSETAW, &tty_new);
181 signal(SIGALRM, update);
184 itv.it_value.tv_sec = 0;
185 itv.it_value.tv_usec = 1;
186 itv.it_interval.tv_sec = sp->update_secs;
187 itv.it_interval.tv_usec = 0;
188 setitimer(ITIMER_REAL, &itv, NULL);
191 alarm(sp->update_secs);
195 if (getcommand() != 1)
199 itv.it_value.tv_sec = 0;
200 itv.it_value.tv_usec = 0;
201 setitimer(ITIMER_REAL, &itv, NULL);
210 itv.it_value.tv_sec = sp->update_secs;
211 itv.it_value.tv_usec = 0;
212 itv.it_interval.tv_sec = sp->update_secs;
213 itv.it_interval.tv_usec = 0;
214 setitimer(ITIMER_REAL, &itv, NULL);
217 alarm(sp->update_secs);
230 yyin = fopen(s, "r");
248 static char file[256];
249 char line[256], games[256];
251 strcpy(games, _PATH_GAMES);
252 strcat(games, GAMES);
254 if ((fp = fopen(games, "r")) == NULL) {
258 if (fgets(line, sizeof(line), fp) == NULL) {
259 fprintf(stderr, "%s: no default game available\n", games);
263 line[strlen(line) - 1] = '\0';
264 strcpy(file, _PATH_GAMES);
274 static char file[256];
275 const char *ret = NULL;
276 char line[256], games[256];
278 strcpy(games, _PATH_GAMES);
279 strcat(games, GAMES);
281 if ((fp = fopen(games, "r")) == NULL) {
285 while (fgets(line, sizeof(line), fp) != NULL) {
286 line[strlen(line) - 1] = '\0';
287 if (strcmp(s, line) == 0) {
288 strcpy(file, _PATH_GAMES);
298 fprintf(stderr, "%s: %s: game not found\n", games, s);
299 fprintf(stderr, "Your score will not be logged.\n");
300 sleep(2); /* give the guy time to read it */
308 char line[256], games[256];
311 strcpy(games, _PATH_GAMES);
312 strcat(games, GAMES);
314 if ((fp = fopen(games, "r")) == NULL) {
318 puts("available games:");
319 while (fgets(line, sizeof(line), fp) != NULL) {
324 if (num_games == 0) {
325 fprintf(stderr, "%s: no games available\n", games);