Home | History | Annotate | Line # | Download | only in atc
log.c revision 1.14
      1 /*	$NetBSD: log.c,v 1.14 2005/07/01 00:48:34 jmc 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.14 2005/07/01 00:48:34 jmc 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 	score_fd = open(_PATH_SCORE, O_CREAT|O_RDWR, 0664);
    109 	umask(old_mask);
    110 	if (score_fd < 0) {
    111 		warn("open %s", _PATH_SCORE);
    112 		return;
    113 	}
    114 	if (score_fd < 3)
    115 		exit(1);
    116 	/* Set the close-on-exec flag.  If this fails for any reason, quit
    117 	 * rather than leave the score file open to tampering.  */
    118 	flags = fcntl(score_fd, F_GETFD);
    119 	if (flags < 0)
    120 		err(1, "fcntl F_GETFD");
    121 	flags |= FD_CLOEXEC;
    122 	if (fcntl(score_fd, F_SETFD, flags) == -1)
    123 		err(1, "fcntl F_SETFD");
    124 	/*
    125 	 * This is done to take advantage of stdio, while still
    126 	 * allowing a O_CREAT during the open(2) of the log file.
    127 	 */
    128 	score_fp = fdopen(score_fd, "r+");
    129 	if (score_fp == NULL) {
    130 		warn("fdopen %s", _PATH_SCORE);
    131 		return;
    132 	}
    133 }
    134 
    135 int
    136 log_score(int list_em)
    137 {
    138 	int		i, num_scores = 0, good, changed = 0, found = 0;
    139 	struct passwd	*pw;
    140 	char		*cp;
    141 	SCORE		score[100], thisscore;
    142 	struct utsname	lname;
    143 	long		offset;
    144 
    145 	if (score_fp == NULL) {
    146 		warnx("no score file available");
    147 		return (-1);
    148 	}
    149 
    150 #ifdef BSD
    151 	if (flock(fileno(score_fp), LOCK_EX) < 0)
    152 #endif
    153 #ifdef SYSV
    154 	while (lockf(fileno(score_fp), F_LOCK, 1) < 0)
    155 #endif
    156 	{
    157 		warn("flock %s", _PATH_SCORE);
    158 		return (-1);
    159 	}
    160 	for (;;) {
    161 		good = fscanf(score_fp, SCORE_SCANF_FMT,
    162 			score[num_scores].name,
    163 			score[num_scores].host,
    164 			score[num_scores].game,
    165 			&score[num_scores].planes,
    166 			&score[num_scores].time,
    167 			&score[num_scores].real_time);
    168 		if (good != 6 || ++num_scores >= NUM_SCORES)
    169 			break;
    170 	}
    171 	if (!test_mode && !list_em) {
    172 		if ((pw = (struct passwd *) getpwuid(getuid())) == NULL) {
    173 			fprintf(stderr,
    174 				"getpwuid failed for uid %d.  Who are you?\n",
    175 				(int)getuid());
    176 			return (-1);
    177 		}
    178 		strcpy(thisscore.name, pw->pw_name);
    179 		uname(&lname);
    180 		strlcpy(thisscore.host, lname.nodename, sizeof(thisscore.host));
    181 
    182 		cp = strrchr(filename, '/');
    183 		if (cp == NULL) {
    184 			fprintf(stderr, "log: where's the '/' in %s?\n",
    185 			    filename);
    186 			return (-1);
    187 		}
    188 		cp++;
    189 		strcpy(thisscore.game, cp);
    190 
    191 		thisscore.time = clck;
    192 		thisscore.planes = safe_planes;
    193 		thisscore.real_time = time(0) - start_time;
    194 
    195 		for (i = 0; i < num_scores; i++) {
    196 			if (strcmp(thisscore.name, score[i].name) == 0 &&
    197 			    strcmp(thisscore.host, score[i].host) == 0 &&
    198 			    strcmp(thisscore.game, score[i].game) == 0) {
    199 				if (thisscore.time > score[i].time) {
    200 					score[i].time = thisscore.time;
    201 					score[i].planes = thisscore.planes;
    202 					score[i].real_time =
    203 						thisscore.real_time;
    204 					changed++;
    205 				}
    206 				found++;
    207 				break;
    208 			}
    209 		}
    210 		if (!found) {
    211 			for (i = 0; i < num_scores; i++) {
    212 				if (thisscore.time > score[i].time) {
    213 					if (num_scores < NUM_SCORES)
    214 						num_scores++;
    215 					memcpy(&score[num_scores - 1],
    216 					       &score[i],
    217 					       sizeof (score[i]));
    218 					memcpy(&score[i], &thisscore,
    219 					       sizeof (score[i]));
    220 					changed++;
    221 					break;
    222 				}
    223 			}
    224 		}
    225 		if (!found && !changed && num_scores < NUM_SCORES) {
    226 			memcpy(&score[num_scores], &thisscore,
    227 			       sizeof (score[num_scores]));
    228 			num_scores++;
    229 			changed++;
    230 		}
    231 
    232 		if (changed) {
    233 			if (found)
    234 				puts("You beat your previous score!");
    235 			else
    236 				puts("You made the top players list!");
    237 			qsort(score, num_scores, sizeof (*score), compar);
    238 			rewind(score_fp);
    239 			for (i = 0; i < num_scores; i++)
    240 				fprintf(score_fp, "%s %s %s %d %d %d\n",
    241 					score[i].name, score[i].host,
    242 					score[i].game, score[i].planes,
    243 					score[i].time, score[i].real_time);
    244 			fflush(score_fp);
    245 			if (ferror(score_fp))
    246 				warn("error writing %s", _PATH_SCORE);
    247 			/* It is just possible that updating an entry could
    248 			 * have reduced the length of the file, so we
    249 			 * truncate it.  The seeks are required for stream/fd
    250 			 * synchronisation by POSIX.1.  */
    251 			offset = ftell(score_fp);
    252 			lseek(fileno(score_fp), 0, SEEK_SET);
    253 			ftruncate(fileno(score_fp), offset);
    254 			rewind(score_fp);
    255 		} else {
    256 			if (found)
    257 				puts("You didn't beat your previous score.");
    258 			else
    259 				puts("You didn't make the top players list.");
    260 		}
    261 		putchar('\n');
    262 	}
    263 #ifdef BSD
    264 	flock(fileno(score_fp), LOCK_UN);
    265 #endif
    266 #ifdef SYSV
    267 	/* lock will evaporate upon close */
    268 #endif
    269 	fclose(score_fp);
    270 	printf("%2s:  %-8s  %-8s  %-18s  %4s  %9s  %4s\n", "#", "name", "host",
    271 		"game", "time", "real time", "planes safe");
    272 	puts("--------------------------------------------------------------");
    273 	puts("-----------------");
    274 	for (i = 0; i < num_scores; i++) {
    275 		cp = strchr(score[i].host, '.');
    276 		if (cp != NULL)
    277 			*cp = '\0';
    278 		printf("%2d:  %-8s  %-8s  %-18s  %4d  %9s  %4d\n", i + 1,
    279 			score[i].name, score[i].host, score[i].game,
    280 			score[i].time, timestr(score[i].real_time),
    281 			score[i].planes);
    282 	}
    283 	putchar('\n');
    284 	return (0);
    285 }
    286 
    287 void
    288 log_score_quit(int dummy __attribute__((__unused__)))
    289 {
    290 	(void)log_score(0);
    291 	exit(0);
    292 }
    293