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