Home | History | Annotate | Line # | Download | only in atc
log.c revision 1.19
      1 /*	$NetBSD: log.c,v 1.19 2007/12/15 19:44:38 perry Exp $	*/
      2 
      3 /*-
      4  * Copyright (c) 1990, 1993
      5  *	The Regents of the University of California.  All rights reserved.
      6  *
      7  * This code is derived from software contributed to Berkeley by
      8  * Ed James.
      9  *
     10  * Redistribution and use in source and binary forms, with or without
     11  * modification, are permitted provided that the following conditions
     12  * are met:
     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.
     21  *
     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
     32  * SUCH DAMAGE.
     33  */
     34 
     35 /*
     36  * Copyright (c) 1987 by Ed James, UC Berkeley.  All rights reserved.
     37  *
     38  * Copy permission is hereby granted provided that this notice is
     39  * retained on all partial or complete copies.
     40  *
     41  * For more info on this and all of my stuff, mail edjames (at) berkeley.edu.
     42  */
     43 
     44 #include <sys/cdefs.h>
     45 #ifndef lint
     46 #if 0
     47 static char sccsid[] = "@(#)log.c	8.1 (Berkeley) 5/31/93";
     48 #else
     49 __RCSID("$NetBSD: log.c,v 1.19 2007/12/15 19:44:38 perry Exp $");
     50 #endif
     51 #endif /* not lint */
     52 
     53 #include "include.h"
     54 #include "pathnames.h"
     55 
     56 static FILE *score_fp;
     57 
     58 int
     59 compar(const void *va, const void *vb)
     60 {
     61 	const SCORE	*a, *b;
     62 
     63 	a = (const SCORE *)va;
     64 	b = (const SCORE *)vb;
     65 	if (b->planes == a->planes)
     66 		return (b->time - a->time);
     67 	else
     68 		return (b->planes - a->planes);
     69 }
     70 
     71 #define SECAMIN		60
     72 #define MINAHOUR	60
     73 #define HOURADAY	24
     74 #define SECAHOUR	(SECAMIN * MINAHOUR)
     75 #define SECADAY		(SECAHOUR * HOURADAY)
     76 #define DAY(t)		((t) / SECADAY)
     77 #define HOUR(t)		(((t) % SECADAY) / SECAHOUR)
     78 #define MIN(t)		(((t) % SECAHOUR) / SECAMIN)
     79 #define SEC(t)		((t) % SECAMIN)
     80 
     81 const char *
     82 timestr(int t)
     83 {
     84 	static char	s[80];
     85 
     86 	if (DAY(t) > 0)
     87 		(void)sprintf(s, "%dd+%02dhrs", DAY(t), HOUR(t));
     88 	else if (HOUR(t) > 0)
     89 		(void)sprintf(s, "%d:%02d:%02d", HOUR(t), MIN(t), SEC(t));
     90 	else if (MIN(t) > 0)
     91 		(void)sprintf(s, "%d:%02d", MIN(t), SEC(t));
     92 	else if (SEC(t) > 0)
     93 		(void)sprintf(s, ":%02d", SEC(t));
     94 	else
     95 		*s = '\0';
     96 
     97 	return (s);
     98 }
     99 
    100 void
    101 open_score_file(void)
    102 {
    103 	mode_t old_mask;
    104 	int score_fd;
    105 	int flags;
    106 
    107 	old_mask = umask(0);
    108 #if defined(O_NOFOLLOW)
    109 	score_fd = open(_PATH_SCORE, O_CREAT|O_RDWR|O_NOFOLLOW, 0664);
    110 #else
    111 	score_fd = open(_PATH_SCORE, O_CREAT|O_RDWR, 0664);
    112 #endif
    113 	(void)umask(old_mask);
    114 	if (score_fd < 0) {
    115 		warn("open %s", _PATH_SCORE);
    116 		return;
    117 	}
    118 	if (score_fd < 3)
    119 		exit(1);
    120 	/* Set the close-on-exec flag.  If this fails for any reason, quit
    121 	 * rather than leave the score file open to tampering.  */
    122 	flags = fcntl(score_fd, F_GETFD);
    123 	if (flags < 0)
    124 		err(1, "fcntl F_GETFD");
    125 	flags |= FD_CLOEXEC;
    126 	if (fcntl(score_fd, F_SETFD, flags) == -1)
    127 		err(1, "fcntl F_SETFD");
    128 	/*
    129 	 * This is done to take advantage of stdio, while still
    130 	 * allowing a O_CREAT during the open(2) of the log file.
    131 	 */
    132 	score_fp = fdopen(score_fd, "r+");
    133 	if (score_fp == NULL) {
    134 		warn("fdopen %s", _PATH_SCORE);
    135 		return;
    136 	}
    137 }
    138 
    139 int
    140 log_score(int list_em)
    141 {
    142 	int		i, num_scores = 0, good, changed = 0, found = 0;
    143 	struct passwd	*pw;
    144 	char		*cp;
    145 	SCORE		score[100], thisscore;
    146 	struct utsname	lname;
    147 	long		offset;
    148 
    149 	if (score_fp == NULL) {
    150 		warnx("no score file available");
    151 		return (-1);
    152 	}
    153 
    154 #ifdef BSD
    155 	if (flock(fileno(score_fp), LOCK_EX) < 0)
    156 #endif
    157 #ifdef SYSV
    158 	if (lockf(fileno(score_fp), F_LOCK, 1) < 0)
    159 #endif
    160 	{
    161 		warn("flock %s", _PATH_SCORE);
    162 		return (-1);
    163 	}
    164 	for (;;) {
    165 		good = fscanf(score_fp, SCORE_SCANF_FMT,
    166 			   score[num_scores].name,
    167 			   score[num_scores].host,
    168 			   score[num_scores].game,
    169 			   &score[num_scores].planes,
    170 			   &score[num_scores].time,
    171 			   &score[num_scores].real_time);
    172 		if (good != 6 || ++num_scores >= NUM_SCORES)
    173 			break;
    174 	}
    175 	if (!test_mode && !list_em) {
    176 		if ((pw = (struct passwd *) getpwuid(getuid())) == NULL) {
    177 			(void)fprintf(stderr,
    178 				"getpwuid failed for uid %d.  Who are you?\n",
    179 				(int)getuid());
    180 			return (-1);
    181 		}
    182 		(void)strlcpy(thisscore.name, pw->pw_name, SCORE_NAME_LEN);
    183 		(void)uname(&lname);
    184 		(void)strlcpy(thisscore.host, lname.nodename,
    185 		    sizeof(thisscore.host));
    186 
    187 		cp = strrchr(filename, '/');
    188 		if (cp == NULL) {
    189 			(void)fprintf(stderr, "log: where's the '/' in %s?\n",
    190 			    filename);
    191 			return (-1);
    192 		}
    193 		cp++;
    194 		(void)strlcpy(thisscore.game, cp, SCORE_GAME_LEN);
    195 
    196 		thisscore.time = clck;
    197 		thisscore.planes = safe_planes;
    198 		thisscore.real_time = time(0) - start_time;
    199 
    200 		for (i = 0; i < num_scores; i++) {
    201 			if (strcmp(thisscore.name, score[i].name) == 0 &&
    202 			    strcmp(thisscore.host, score[i].host) == 0 &&
    203 			    strcmp(thisscore.game, score[i].game) == 0) {
    204 				if (thisscore.time > score[i].time) {
    205 					score[i].time = thisscore.time;
    206 					score[i].planes = thisscore.planes;
    207 					score[i].real_time =
    208 					    thisscore.real_time;
    209 					changed++;
    210 				}
    211 				found++;
    212 				break;
    213 			}
    214 		}
    215 		if (!found) {
    216 			for (i = 0; i < num_scores; i++) {
    217 				if (thisscore.time > score[i].time) {
    218 					if (num_scores < NUM_SCORES)
    219 						num_scores++;
    220 					(void)memcpy(&score[num_scores - 1],
    221 					    &score[i], sizeof (score[i]));
    222 					(void)memcpy(&score[i], &thisscore,
    223 					    sizeof (score[i]));
    224 					changed++;
    225 					break;
    226 				}
    227 			}
    228 		}
    229 		if (!found && !changed && num_scores < NUM_SCORES) {
    230 			(void)memcpy(&score[num_scores], &thisscore,
    231 			    sizeof (score[num_scores]));
    232 			num_scores++;
    233 			changed++;
    234 		}
    235 
    236 		if (changed) {
    237 			if (found)
    238 				(void)puts("You beat your previous score!");
    239 			else
    240 				(void)puts("You made the top players list!");
    241 			qsort(score, (size_t)num_scores, sizeof (*score),
    242 			    compar);
    243 			rewind(score_fp);
    244 			for (i = 0; i < num_scores; i++)
    245 				(void)fprintf(score_fp, "%s %s %s %d %d %d\n",
    246 				    score[i].name, score[i].host,
    247 				    score[i].game, score[i].planes,
    248 				    score[i].time, score[i].real_time);
    249 			(void)fflush(score_fp);
    250 			if (ferror(score_fp))
    251 				warn("error writing %s", _PATH_SCORE);
    252 			/* It is just possible that updating an entry could
    253 			 * have reduced the length of the file, so we
    254 			 * truncate it.  The seeks are required for stream/fd
    255 			 * synchronisation by POSIX.1.  */
    256 			offset = ftell(score_fp);
    257 			(void)lseek(fileno(score_fp), (off_t)0, SEEK_SET);
    258 			(void)ftruncate(fileno(score_fp), (off_t)offset);
    259 			rewind(score_fp);
    260 		} else {
    261 			if (found)
    262 				(void)puts(
    263 				    "You didn't beat your previous score.");
    264 			else
    265 				(void)puts(
    266 				    "You didn't make the top players list.");
    267 		}
    268 		(void)putchar('\n');
    269 	}
    270 #ifdef BSD
    271 	(void)flock(fileno(score_fp), LOCK_UN);
    272 #endif
    273 #ifdef SYSV
    274 	/* lock will evaporate upon close */
    275 #endif
    276 	(void)fclose(score_fp);
    277 	(void)printf("%2s:  %-8s  %-8s  %-18s  %4s  %9s  %4s\n", "#", "name",
    278 	    "host", "game", "time", "real time", "planes safe");
    279 	(void)printf("-------------------------------------------------------");
    280 	(void)printf("-------------------------\n");
    281 	for (i = 0; i < num_scores; i++) {
    282 		cp = strchr(score[i].host, '.');
    283 		if (cp != NULL)
    284 			*cp = '\0';
    285 		(void)printf("%2d:  %-8s  %-8s  %-18s  %4d  %9s  %4d\n", i + 1,
    286 		    score[i].name, score[i].host, score[i].game,
    287 		    score[i].time, timestr(score[i].real_time),
    288 		    score[i].planes);
    289 	}
    290 	(void)putchar('\n');
    291 	return (0);
    292 }
    293 
    294 /* ARGSUSED */
    295 void
    296 log_score_quit(int dummy __unused)
    297 {
    298 	(void)log_score(0);
    299 	exit(0);
    300 }
    301