]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - atc/main.c
1 /* $NetBSD: main.c,v 1.24 2015/06/25 05:33:02 dholland Exp $ */
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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. Neither the name of the University nor the names of its contributors
19 * may be used to endorse or promote products derived from this software
20 * without specific prior written permission.
22 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
23 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
24 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
25 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
26 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
27 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
28 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
29 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
30 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
31 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
36 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
38 * Copy permission is hereby granted provided that this notice is
39 * retained on all partial or complete copies.
41 * For more info on this and all of my stuff, mail edjames@berkeley.edu.
44 #include <sys/cdefs.h>
46 __COPYRIGHT("@(#) Copyright (c) 1990, 1993\
47 The Regents of the University of California. All rights reserved.");
52 static char sccsid
[] = "@(#)main.c 8.1 (Berkeley) 5/31/93";
54 __RCSID("$NetBSD: main.c,v 1.24 2015/06/25 05:33:02 dholland Exp $");
66 #include "pathnames.h"
74 static int read_file(const char *);
75 static const char *default_game(void);
76 static const char *okay_game(const char *);
77 static int list_games(void);
78 static void quit(int);
81 main(int argc
, char *argv
[])
84 int f_usage
= 0, f_list
= 0, f_showscore
= 0;
86 const char *file
= NULL
;
93 /* Open the score file then revoke setgid privileges */
95 (void)setgid(getgid());
97 start_time
= time(NULL
);
100 while ((ch
= getopt(argc
, argv
, ":u?lstpg:f:r:")) != -1) {
131 (void)fprintf(stderr
,
132 "Usage: %s -[u?lstp] [-[gf] game_name] [-r random seed]\n",
141 (void)strlcpy(buf
, _PATH_GAMES
, 100);
145 if (f_usage
|| f_showscore
|| f_list
|| f_printpath
)
149 file
= default_game();
151 file
= okay_game(file
);
153 if (file
== NULL
|| read_file(file
) < 0)
161 (void)signal(SIGINT
, quit
);
162 (void)signal(SIGQUIT
, quit
);
164 (void)signal(SIGTSTP
, SIG_IGN
);
166 (void)signal(SIGHUP
, log_score_quit
);
167 (void)signal(SIGTERM
, log_score_quit
);
169 (void)tcgetattr(fileno(stdin
), &tty_start
);
171 tty_new
.c_lflag
&= ~(ICANON
|ECHO
);
172 tty_new
.c_iflag
|= ICRNL
;
173 tty_new
.c_cc
[VMIN
] = 1;
174 tty_new
.c_cc
[VTIME
] = 0;
175 (void)tcsetattr(fileno(stdin
), TCSADRAIN
, &tty_new
);
177 sa
.sa_handler
= update
;
178 (void)sigemptyset(&sa
.sa_mask
);
179 (void)sigaddset(&sa
.sa_mask
, SIGALRM
);
180 (void)sigaddset(&sa
.sa_mask
, SIGINT
);
182 (void)sigaction(SIGALRM
, &sa
, (struct sigaction
*)0);
185 itv
.it_value
.tv_sec
= 0;
186 itv
.it_value
.tv_usec
= 1;
187 itv
.it_interval
.tv_sec
= sp
->update_secs
;
188 itv
.it_interval
.tv_usec
= 0;
189 (void)setitimer(ITIMER_REAL
, &itv
, NULL
);
192 alarm(sp
->update_secs
);
196 if (getcommand() != 1)
200 itv
.it_value
.tv_sec
= 0;
201 itv
.it_value
.tv_usec
= 0;
202 (void)setitimer(ITIMER_REAL
, &itv
, NULL
);
211 itv
.it_value
.tv_sec
= sp
->update_secs
;
212 itv
.it_value
.tv_usec
= 0;
213 itv
.it_interval
.tv_sec
= sp
->update_secs
;
214 itv
.it_interval
.tv_usec
= 0;
215 (void)setitimer(ITIMER_REAL
, &itv
, NULL
);
218 alarm(sp
->update_secs
);
225 read_file(const char *s
)
230 yyin
= fopen(s
, "r");
248 static char file
[256];
249 char line
[256], games
[256];
251 (void)strlcpy(games
, _PATH_GAMES
, 256);
252 (void)strlcat(games
, GAMES
, 256);
254 if ((fp
= fopen(games
, "r")) == NULL
) {
255 warn("fopen %s", games
);
258 if (fgets(line
, sizeof(line
), fp
) == NULL
) {
259 (void)fprintf(stderr
, "%s: no default game available\n", games
);
264 line
[strlen(line
) - 1] = '\0';
265 (void)strlcpy(file
, _PATH_GAMES
, 256);
266 (void)strlcat(file
, line
, 256);
271 okay_game(const char *s
)
274 static char file
[256];
275 const char *ret
= NULL
;
276 char line
[256], games
[256];
278 (void)strlcpy(games
, _PATH_GAMES
, 256);
279 (void)strlcat(games
, GAMES
, 256);
281 if ((fp
= fopen(games
, "r")) == NULL
) {
282 warn("fopen %s", games
);
285 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
286 line
[strlen(line
) - 1] = '\0';
287 if (strcmp(s
, line
) == 0) {
288 (void)strlcpy(file
, _PATH_GAMES
, 256);
289 (void)strlcat(file
, line
, 256);
298 (void)fprintf(stderr
, "%s: %s: game not found\n", games
, s
);
299 (void)fprintf(stderr
, "Your score will not be logged.\n");
300 (void)sleep(2); /* give the guy time to read it */
309 char line
[256], games
[256];
312 (void)strlcpy(games
, _PATH_GAMES
, 256);
313 (void)strlcat(games
, GAMES
, 256);
315 if ((fp
= fopen(games
, "r")) == NULL
) {
316 warn("fopen %s", games
);
319 (void)puts("available games:");
320 while (fgets(line
, sizeof(line
), fp
) != NULL
) {
321 (void)printf(" %s", line
);
325 if (num_games
== 0) {
326 (void)fprintf(stderr
, "%s: no games available\n", games
);
334 quit(int dummy __unused
)
338 struct itimerval itv
;
342 if (c
== EOF
|| c
== 'y') {
345 itv
.it_value
.tv_sec
= 0;
346 itv
.it_value
.tv_usec
= 0;
347 (void)setitimer(ITIMER_REAL
, &itv
, NULL
);