X-Git-Url: https://git.cameronkatri.com/bsdgames-darwin.git/blobdiff_plain/04f4d7044367099914cf1906ae27ac20c24e44cf..HEAD:/cribbage/crib.c diff --git a/cribbage/crib.c b/cribbage/crib.c index 83a9e422..130c3212 100644 --- a/cribbage/crib.c +++ b/cribbage/crib.c @@ -1,4 +1,4 @@ -/* $NetBSD: crib.c,v 1.11 1999/09/08 21:17:47 jsm Exp $ */ +/* $NetBSD: crib.c,v 1.25 2012/10/13 20:36:06 dholland Exp $ */ /*- * Copyright (c) 1980, 1993 @@ -12,11 +12,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. * @@ -35,20 +31,21 @@ #include #ifndef lint -__COPYRIGHT("@(#) Copyright (c) 1980, 1993\n\ - The Regents of the University of California. All rights reserved.\n"); +__COPYRIGHT("@(#) Copyright (c) 1980, 1993\ + The Regents of the University of California. All rights reserved."); #endif /* not lint */ #ifndef lint #if 0 static char sccsid[] = "@(#)crib.c 8.1 (Berkeley) 5/31/93"; #else -__RCSID("$NetBSD: crib.c,v 1.11 1999/09/08 21:17:47 jsm Exp $"); +__RCSID("$NetBSD: crib.c,v 1.25 2012/10/13 20:36:06 dholland Exp $"); #endif #endif /* not lint */ #include #include +#include #include #include #include @@ -59,16 +56,46 @@ __RCSID("$NetBSD: crib.c,v 1.11 1999/09/08 21:17:47 jsm Exp $"); #include "cribcur.h" #include "pathnames.h" -int main __P((int, char *[])); +static void makeboard(void); +static void gamescore(void); +static void game(void); +static int playhand(BOOLEAN); +static int deal(BOOLEAN); +static void discard(BOOLEAN); +static int cut(BOOLEAN, int); +static void prcrib(BOOLEAN, BOOLEAN); +static int peg(BOOLEAN); +static void prtable(int); +static int score(BOOLEAN); int -main(argc, argv) - int argc; - char *argv[]; +main(int argc, char *argv[]) { BOOLEAN playing; FILE *f; int ch; + int fd; + int flags; + + f = fopen(_PATH_LOG, "a"); + if (f == NULL) + warn("fopen %s", _PATH_LOG); + if (f != NULL && fileno(f) < 3) + exit(1); + + /* Revoke setgid privileges */ + setgid(getgid()); + + /* Set close-on-exec flag on log file */ + if (f != NULL) { + fd = fileno(f); + flags = fcntl(fd, F_GETFD); + if (flags < 0) + err(1, "fcntl F_GETFD"); + flags |= FD_CLOEXEC; + if (fcntl(fd, F_SETFD, flags) == -1) + err(1, "fcntl F_SETFD"); + } while ((ch = getopt(argc, argv, "eqr")) != -1) switch (ch) { @@ -87,9 +114,10 @@ main(argc, argv) exit(1); } - initscr(); - (void)signal(SIGINT, rint); - crmode(); + if (!initscr()) + errx(0, "couldn't initialize screen"); + (void)signal(SIGINT, receive_intr); + cbreak(); noecho(); Playwin = subwin(stdscr, PLAY_Y, PLAY_X, 0, 0); @@ -109,7 +137,7 @@ main(argc, argv) mvcur(0, COLS - 1, LINES - 1, 0); fflush(stdout); instructions(); - crmode(); + cbreak(); noecho(); clear(); refresh(); @@ -129,14 +157,12 @@ main(argc, argv) playing = (getuchar() == 'Y'); } while (playing); - if ((f = fopen(_PATH_LOG, "a")) != NULL) { + if (f != NULL) { (void)fprintf(f, "%s: won %5.5d, lost %5.5d\n", getlogin(), cgames, pgames); (void) fclose(f); } bye(); - if (!f) - errx(1, "can't open %s", _PATH_LOG); exit(0); } @@ -144,8 +170,8 @@ main(argc, argv) * makeboard: * Print out the initial board on the screen */ -void -makeboard() +static void +makeboard(void) { mvaddstr(SCORE_Y + 0, SCORE_X, "+---------------------------------------+"); @@ -172,11 +198,9 @@ makeboard() * gamescore: * Print out the current game score */ -void -gamescore() +static void +gamescore(void) { - extern int Lastscore[]; - if (pgames || cgames) { mvprintw(SCORE_Y + 1, SCORE_X + 28, "Games: %3d", pgames); mvprintw(SCORE_Y + 7, SCORE_X + 28, "Games: %3d", cgames); @@ -190,8 +214,8 @@ gamescore() * Play one game up to glimit points. Actually, we only ASK the * player what card to turn. We do a random one, anyway. */ -void -game() +static void +game(void) { int i, j; BOOLEAN flag; @@ -206,7 +230,7 @@ game() if (!rflag) { /* player cuts deck */ msg(quiet ? "Cut for crib? " : "Cut to see whose crib it is -- low card wins? "); - getline(); + get_line(); } i = (rand() >> 4) % CARDS; /* random cut */ do { /* comp cuts deck */ @@ -284,9 +308,8 @@ game() * playhand: * Do up one hand of the game */ -int -playhand(mycrib) - BOOLEAN mycrib; +static int +playhand(BOOLEAN mycrib) { int deckpos; @@ -316,9 +339,8 @@ playhand(mycrib) /* * deal cards to both players from deck */ -int -deal(mycrib) - BOOLEAN mycrib; +static int +deal(BOOLEAN mycrib) { int i, j; @@ -339,9 +361,8 @@ deal(mycrib) * Handle players discarding into the crib... * Note: we call cdiscard() after prining first message so player doesn't wait */ -void -discard(mycrib) - BOOLEAN mycrib; +static void +discard(BOOLEAN mycrib) { const char *prompt; CARD crd; @@ -369,10 +390,8 @@ discard(mycrib) * Cut the deck and set turnover. Actually, we only ASK the * player what card to turn. We do a random one, anyway. */ -int -cut(mycrib, pos) - BOOLEAN mycrib; - int pos; +static int +cut(BOOLEAN mycrib, int pos) { int i; BOOLEAN win; @@ -382,7 +401,7 @@ cut(mycrib, pos) if (!rflag) { /* random cut */ msg(quiet ? "Cut the deck? " : "How many cards down do you wish to cut the deck? "); - getline(); + get_line(); } i = (rand() >> 4) % (CARDS - pos); turnover = deck[i + pos]; @@ -413,9 +432,8 @@ cut(mycrib, pos) * prcrib: * Print out the turnover card with crib indicator */ -void -prcrib(mycrib, blank) - BOOLEAN mycrib, blank; +static void +prcrib(BOOLEAN mycrib, BOOLEAN blank) { int y, cardx; @@ -442,11 +460,10 @@ prcrib(mycrib, blank) * Handle all the pegging... */ static CARD Table[14]; -static int Tcnt; +static unsigned Tcnt; -int -peg(mycrib) - BOOLEAN mycrib; +static int +peg(BOOLEAN mycrib) { static CARD ch[CINHAND], ph[CINHAND]; int i, j, k; @@ -604,12 +621,11 @@ peg(mycrib) * prtable: * Print out the table with the current score */ -void -prtable(score) - int score; +static void +prtable(int curscore) { prhand(Table, Tcnt, Tablewin, FALSE); - mvwprintw(Tablewin, (Tcnt + 2) * 2, Tcnt + 1, "%2d", score); + mvwprintw(Tablewin, (Tcnt + 2) * 2, Tcnt + 1, "%2d", curscore); wrefresh(Tablewin); } @@ -617,9 +633,8 @@ prtable(score) * score: * Handle the scoring of the hands */ -int -score(mycrib) - BOOLEAN mycrib; +static int +score(BOOLEAN mycrib) { sorthand(crib, CINHAND); if (mycrib) {