X-Git-Url: https://git.cameronkatri.com/bsdgames-darwin.git/blobdiff_plain/462fca95471224f85b85be911e72faee16cd1597..3c48fe193d2fb7c927c2c9d1dbf76707e657a9fd:/atc/main.c?ds=inline diff --git a/atc/main.c b/atc/main.c index 0910da90..a579e89d 100644 --- a/atc/main.c +++ b/atc/main.c @@ -1,6 +1,8 @@ +/* $NetBSD: main.c,v 1.13 2003/08/07 09:36:54 agc Exp $ */ + /*- - * Copyright (c) 1990 The Regents of the University of California. - * All rights reserved. + * Copyright (c) 1990, 1993 + * The Regents of the University of California. All rights reserved. * * This code is derived from software contributed to Berkeley by * Ed James. @@ -13,11 +15,7 @@ * 2. Redistributions in binary form must reproduce the above copyright * notice, this list of conditions and the following disclaimer in the * documentation and/or other materials provided with the distribution. - * 3. All advertising materials mentioning features or use of this software - * must display the following acknowledgement: - * This product includes software developed by the University of - * California, Berkeley and its contributors. - * 4. Neither the name of the University nor the names of its contributors + * 3. Neither the name of the University nor the names of its contributors * may be used to endorse or promote products derived from this software * without specific prior written permission. * @@ -43,85 +41,80 @@ * For more info on this and all of my stuff, mail edjames@berkeley.edu. */ +#include #ifndef lint -char copyright[] = -"@(#) Copyright (c) 1990 The Regents of the University of California.\n\ - All rights reserved.\n"; +__COPYRIGHT("@(#) Copyright (c) 1990, 1993\n\ + The Regents of the University of California. All rights reserved.\n"); #endif /* not lint */ #ifndef lint -/*static char sccsid[] = "from: @(#)main.c 5.4 (Berkeley) 3/5/91";*/ -static char rcsid[] = "$Id: main.c,v 1.2 1993/08/01 18:57:04 mycroft Exp $"; +#if 0 +static char sccsid[] = "@(#)main.c 8.1 (Berkeley) 5/31/93"; +#else +__RCSID("$NetBSD: main.c,v 1.13 2003/08/07 09:36:54 agc Exp $"); +#endif #endif /* not lint */ #include "include.h" #include "pathnames.h" +extern FILE *yyin; + +int main(ac, av) + int ac; char *av[]; { int seed; int f_usage = 0, f_list = 0, f_showscore = 0; int f_printpath = 0; - char *file = NULL; - char *name, *ptr; + const char *file = NULL; + int ch; + struct sigaction sa; #ifdef BSD struct itimerval itv; #endif - extern char *default_game(), *okay_game(); - extern void log_score(), quit(), update(); - start_time = seed = time(0); + /* Open the score file then revoke setgid privileges */ + open_score_file(); + setgid(getgid()); + + start_time = seed = time(NULL); - name = *av++; - while (*av) { -#ifndef SAVEDASH - if (**av == '-') - *++*av; - else + while ((ch = getopt(ac, av, "ulstpg:f:r:")) != -1) { + switch (ch) { + case '?': + case 'u': + default: + f_usage++; + break; + case 'l': + f_list++; + break; + case 's': + case 't': + f_showscore++; + break; + case 'p': + f_printpath++; + break; + case 'r': + seed = atoi(optarg); + break; + case 'f': + case 'g': + file = optarg; break; -#endif - ptr = *av++; - while (*ptr) { - switch (*ptr) { - case '?': - case 'u': - f_usage++; - break; - case 'l': - f_list++; - break; - case 's': - case 't': - f_showscore++; - break; - case 'p': - f_printpath++; - break; - case 'r': - seed = atoi(*av); - av++; - break; - case 'f': - case 'g': - file = *av; - av++; - break; - default: - fprintf(stderr, "Unknown option '%c'\n", *ptr, - name); - f_usage++; - break; - } - ptr++; } } + if (optind < ac) + f_usage++; srandom(seed); if (f_usage) fprintf(stderr, "Usage: %s -[u?lstp] [-[gf] game_name] [-r random seed]\n", - name); + av[0]); if (f_showscore) log_score(1); if (f_list) @@ -156,28 +149,23 @@ main(ac, av) signal(SIGTSTP, SIG_IGN); signal(SIGSTOP, SIG_IGN); #endif - signal(SIGHUP, log_score); - signal(SIGTERM, log_score); - -#ifdef BSD - ioctl(fileno(stdin), TIOCGETP, &tty_start); - bcopy(&tty_start, &tty_new, sizeof(tty_new)); - tty_new.sg_flags |= CBREAK; - tty_new.sg_flags &= ~ECHO; - ioctl(fileno(stdin), TIOCSETP, &tty_new); -#endif + signal(SIGHUP, log_score_quit); + signal(SIGTERM, log_score_quit); -#ifdef SYSV - ioctl(fileno(stdin), TCGETA, &tty_start); - bcopy(&tty_start, &tty_new, sizeof(tty_new)); - tty_new.c_lflag &= ~ICANON; - tty_new.c_lflag &= ~ECHO; + tcgetattr(fileno(stdin), &tty_start); + tty_new = tty_start; + tty_new.c_lflag &= ~(ICANON|ECHO); + tty_new.c_iflag |= ICRNL; tty_new.c_cc[VMIN] = 1; tty_new.c_cc[VTIME] = 0; - ioctl(fileno(stdin), TCSETAW, &tty_new); -#endif + tcsetattr(fileno(stdin), TCSADRAIN, &tty_new); - signal(SIGALRM, update); + sa.sa_handler = update; + sigemptyset(&sa.sa_mask); + sigaddset(&sa.sa_mask, SIGALRM); + sigaddset(&sa.sa_mask, SIGINT); + sa.sa_flags = 0; + sigaction(SIGALRM, &sa, (struct sigaction *)0); #ifdef BSD itv.it_value.tv_sec = 0; @@ -203,7 +191,7 @@ main(ac, av) alarm(0); #endif - update(); + update(0); #ifdef BSD itv.it_value.tv_sec = sp->update_secs; @@ -219,16 +207,16 @@ main(ac, av) } } +int read_file(s) - char *s; + const char *s; { - extern FILE *yyin; int retval; file = s; yyin = fopen(s, "r"); if (yyin == NULL) { - perror(s); + warn("fopen %s", s); return (-1); } retval = yyparse(); @@ -240,7 +228,7 @@ read_file(s) return (0); } -char * +const char * default_game() { FILE *fp; @@ -251,7 +239,7 @@ default_game() strcat(games, GAMES); if ((fp = fopen(games, "r")) == NULL) { - perror(games); + warn("fopen %s", games); return (NULL); } if (fgets(line, sizeof(line), fp) == NULL) { @@ -265,19 +253,20 @@ default_game() return (file); } -char * +const char * okay_game(s) - char *s; + const char *s; { FILE *fp; static char file[256]; - char *ret = NULL, line[256], games[256]; + const char *ret = NULL; + char line[256], games[256]; strcpy(games, _PATH_GAMES); strcat(games, GAMES); if ((fp = fopen(games, "r")) == NULL) { - perror(games); + warn("fopen %s", games); return (NULL); } while (fgets(line, sizeof(line), fp) != NULL) { @@ -300,6 +289,7 @@ okay_game(s) return (ret); } +int list_games() { FILE *fp; @@ -310,7 +300,7 @@ list_games() strcat(games, GAMES); if ((fp = fopen(games, "r")) == NULL) { - perror(games); + warn("fopen %s", games); return (-1); } puts("available games:");