diff options
author | steve <steve@FreeBSD.org> | 1999-05-03 02:08:31 +0000 |
---|---|---|
committer | steve <steve@FreeBSD.org> | 1999-05-03 02:08:31 +0000 |
commit | cac2154a9646500f9147bc1cf675ffdb4959d1c8 (patch) | |
tree | 9e771c3ccb36de6d9ba45a329e12d4e756cbae45 /games/atc | |
parent | 6c01d880e5a7bed8c215df0862c927511e67029f (diff) | |
download | FreeBSD-src-cac2154a9646500f9147bc1cf675ffdb4959d1c8.zip FreeBSD-src-cac2154a9646500f9147bc1cf675ffdb4959d1c8.tar.gz |
Round two of MetBSD games merges.
Submitted by: Stephen J. Roznowski <sjr@home.net>
Diffstat (limited to 'games/atc')
-rw-r--r-- | games/atc/Makefile | 4 | ||||
-rw-r--r-- | games/atc/log.c | 89 | ||||
-rw-r--r-- | games/atc/main.c | 4 | ||||
-rw-r--r-- | games/atc/struct.h | 2 |
4 files changed, 72 insertions, 27 deletions
diff --git a/games/atc/Makefile b/games/atc/Makefile index d640d73..3c068c8 100644 --- a/games/atc/Makefile +++ b/games/atc/Makefile @@ -5,8 +5,8 @@ CFLAGS+=-DBSD -I${.CURDIR} -I. SRCS= extern.c grammar.y graphics.c input.c lex.l list.c log.c \ main.c tunable.c update.c y.tab.h MAN6= atc.6 -DPADD= ${LIBL} ${LIBM} ${LIBCURSES} ${LIBTERMCAP} ${LIBCOMPAT} -LDADD= -ll -lm -lcurses -ltermcap -lcompat +DPADD= ${LIBL} ${LIBM} ${LIBCURSES} ${LIBTERMCAP} +LDADD= -ll -lm -lcurses -ltermcap GAMES= Game_List Killer crossover default easy game_2 HIDEGAME=hidegame diff --git a/games/atc/log.c b/games/atc/log.c index cd347a4..a713e59 100644 --- a/games/atc/log.c +++ b/games/atc/log.c @@ -51,6 +51,12 @@ static char sccsid[] = "@(#)log.c 8.1 (Berkeley) 5/31/93"; #include "include.h" #include "pathnames.h" +#ifdef SYSV +#include <sys/utsname.h> +#endif + +static FILE *score_fp; + int compar(va, vb) const void *va, *vb; @@ -95,44 +101,68 @@ timestr(t) return (s); } +void +open_score_file() +{ + mode_t old_mask; + int score_fd; + int flags; + + old_mask = umask(0); + score_fd = open(_PATH_SCORE, O_CREAT|O_RDWR, 0664); + umask(old_mask); + if (score_fd < 0) { + warn("open %s", _PATH_SCORE); + return; + } + /* Set the close-on-exec flag. If this fails for any reason, quit + * rather than leave the score file open to tampering. */ + flags = fcntl(score_fd, F_GETFD); + if (flags < 0) + err(1, "fcntl F_GETFD"); + flags |= FD_CLOEXEC; + if (fcntl(score_fd, F_SETFD, flags) == -1) + err(1, "fcntl F_SETFD"); + /* + * This is done to take advantage of stdio, while still + * allowing a O_CREAT during the open(2) of the log file. + */ + score_fp = fdopen(score_fd, "r+"); + if (score_fp == NULL) { + warn("fdopen %s", _PATH_SCORE); + return; + } +} + +int log_score(list_em) + int list_em; { - register int i, fd, num_scores = 0, good, changed = 0, found = 0; + int i, num_scores = 0, good, changed = 0, found = 0; struct passwd *pw; - FILE *fp; - char *cp, *index(), *rindex(); + char *cp; SCORE score[100], thisscore; #ifdef SYSV struct utsname name; #endif - umask(0); - fd = open(_PATH_SCORE, O_CREAT|O_RDWR, 0664); - if (fd < 0) { - perror(_PATH_SCORE); - return (-1); - } - /* - * This is done to take advantage of stdio, while still - * allowing a O_CREAT during the open(2) of the log file. - */ - fp = fdopen(fd, "r+"); - if (fp == NULL) { - perror(_PATH_SCORE); + if (score_fp == NULL) { + warnx("no score file available"); return (-1); } + #ifdef BSD - if (flock(fileno(fp), LOCK_EX) < 0) + if (flock(fileno(score_fp), LOCK_EX) < 0) #endif #ifdef SYSV - while (lockf(fileno(fp), F_LOCK, 1) < 0) + while (lockf(fileno(score_fp), F_LOCK, 1) < 0) #endif { - perror("flock"); + warn("flock %s", _PATH_SCORE); return (-1); } for (;;) { - good = fscanf(fp, "%s %s %s %d %d %d", + good = fscanf(score_fp, SCORE_SCANF_FMT, score[num_scores].name, score[num_scores].host, score[num_scores].game, @@ -146,7 +176,7 @@ log_score(list_em) if ((pw = (struct passwd *) getpwuid(getuid())) == NULL) { fprintf(stderr, "getpwuid failed for uid %d. Who are you?\n", - getuid()); + (int)getuid()); return (-1); } strcpy(thisscore.name, pw->pw_name); @@ -216,12 +246,21 @@ log_score(list_em) else puts("You made the top players list!"); qsort(score, num_scores, sizeof (*score), compar); - rewind(fp); + rewind(score_fp); for (i = 0; i < num_scores; i++) - fprintf(fp, "%s %s %s %d %d %d\n", + fprintf(score_fp, "%s %s %s %d %d %d\n", score[i].name, score[i].host, score[i].game, score[i].planes, score[i].time, score[i].real_time); + fflush(score_fp); + if (ferror(score_fp)) + warn("error writing %s", _PATH_SCORE); + /* It is just possible that updating an entry could + * have reduced the length of the file, so we + * truncate it. The lseek is required for stream/fd + * synchronisation by POSIX.1. */ + lseek(fileno(score_fp), 0, SEEK_END); + ftruncate(fileno(score_fp), ftell(score_fp)); } else { if (found) puts("You didn't beat your previous score."); @@ -231,12 +270,12 @@ log_score(list_em) putchar('\n'); } #ifdef BSD - flock(fileno(fp), LOCK_UN); + flock(fileno(score_fp), LOCK_UN); #endif #ifdef SYSV /* lock will evaporate upon close */ #endif - fclose(fp); + fclose(score_fp); printf("%2s: %-8s %-8s %-18s %4s %9s %4s\n", "#", "name", "host", "game", "time", "real time", "planes safe"); puts("-------------------------------------------------------------------------------"); diff --git a/games/atc/main.c b/games/atc/main.c index 5e225a5..cffe42c 100644 --- a/games/atc/main.c +++ b/games/atc/main.c @@ -72,6 +72,10 @@ main(ac, av) extern const char *default_game(), *okay_game(); extern void log_score(), quit(), update(); + /* Open the score file then revoke setgid privileges */ + open_score_file(); + setregid(getgid(), getgid()); + start_time = time(0); name = *av++; diff --git a/games/atc/struct.h b/games/atc/struct.h index 71fcdca..6034557 100644 --- a/games/atc/struct.h +++ b/games/atc/struct.h @@ -105,6 +105,8 @@ typedef struct { int real_time; } SCORE; +#define SCORE_SCANF_FMT "%9s %255s %255s %d %d %d" + typedef struct displacement { int dx; int dy; |