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