Home | History | Annotate | Line # | Download | only in atc
log.c revision 1.9
      1  1.9  hubertf /*	$NetBSD: log.c,v 1.9 1999/07/17 19:57:03 hubertf Exp $	*/
      2  1.3      cgd 
      3  1.1      cgd /*-
      4  1.3      cgd  * Copyright (c) 1990, 1993
      5  1.3      cgd  *	The Regents of the University of California.  All rights reserved.
      6  1.1      cgd  *
      7  1.1      cgd  * This code is derived from software contributed to Berkeley by
      8  1.1      cgd  * Ed James.
      9  1.1      cgd  *
     10  1.1      cgd  * Redistribution and use in source and binary forms, with or without
     11  1.1      cgd  * modification, are permitted provided that the following conditions
     12  1.1      cgd  * are met:
     13  1.1      cgd  * 1. Redistributions of source code must retain the above copyright
     14  1.1      cgd  *    notice, this list of conditions and the following disclaimer.
     15  1.1      cgd  * 2. Redistributions in binary form must reproduce the above copyright
     16  1.1      cgd  *    notice, this list of conditions and the following disclaimer in the
     17  1.1      cgd  *    documentation and/or other materials provided with the distribution.
     18  1.1      cgd  * 3. All advertising materials mentioning features or use of this software
     19  1.1      cgd  *    must display the following acknowledgement:
     20  1.1      cgd  *	This product includes software developed by the University of
     21  1.1      cgd  *	California, Berkeley and its contributors.
     22  1.1      cgd  * 4. Neither the name of the University nor the names of its contributors
     23  1.1      cgd  *    may be used to endorse or promote products derived from this software
     24  1.1      cgd  *    without specific prior written permission.
     25  1.1      cgd  *
     26  1.1      cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     27  1.1      cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     28  1.1      cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     29  1.1      cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     30  1.1      cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     31  1.1      cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     32  1.1      cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     33  1.1      cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     34  1.1      cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     35  1.1      cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     36  1.1      cgd  * SUCH DAMAGE.
     37  1.1      cgd  */
     38  1.1      cgd 
     39  1.1      cgd /*
     40  1.1      cgd  * Copyright (c) 1987 by Ed James, UC Berkeley.  All rights reserved.
     41  1.1      cgd  *
     42  1.1      cgd  * Copy permission is hereby granted provided that this notice is
     43  1.1      cgd  * retained on all partial or complete copies.
     44  1.1      cgd  *
     45  1.1      cgd  * For more info on this and all of my stuff, mail edjames (at) berkeley.edu.
     46  1.1      cgd  */
     47  1.1      cgd 
     48  1.5    lukem #include <sys/cdefs.h>
     49  1.1      cgd #ifndef lint
     50  1.3      cgd #if 0
     51  1.3      cgd static char sccsid[] = "@(#)log.c	8.1 (Berkeley) 5/31/93";
     52  1.3      cgd #else
     53  1.9  hubertf __RCSID("$NetBSD: log.c,v 1.9 1999/07/17 19:57:03 hubertf Exp $");
     54  1.3      cgd #endif
     55  1.1      cgd #endif not lint
     56  1.1      cgd 
     57  1.1      cgd #include "include.h"
     58  1.1      cgd #include "pathnames.h"
     59  1.1      cgd 
     60  1.9  hubertf static FILE *score_fp;
     61  1.9  hubertf 
     62  1.5    lukem int
     63  1.5    lukem compar(va, vb)
     64  1.5    lukem 	const void *va, *vb;
     65  1.5    lukem {
     66  1.8  hubertf 	const SCORE	*a, *b;
     67  1.5    lukem 
     68  1.8  hubertf 	a = (const SCORE *)va;
     69  1.8  hubertf 	b = (const SCORE *)vb;
     70  1.1      cgd 	if (b->planes == a->planes)
     71  1.1      cgd 		return (b->time - a->time);
     72  1.1      cgd 	else
     73  1.1      cgd 		return (b->planes - a->planes);
     74  1.1      cgd }
     75  1.1      cgd 
     76  1.1      cgd #define SECAMIN		60
     77  1.1      cgd #define MINAHOUR	60
     78  1.1      cgd #define HOURADAY	24
     79  1.1      cgd #define SECAHOUR	(SECAMIN * MINAHOUR)
     80  1.1      cgd #define SECADAY		(SECAHOUR * HOURADAY)
     81  1.1      cgd #define DAY(t)		((t) / SECADAY)
     82  1.1      cgd #define HOUR(t)		(((t) % SECADAY) / SECAHOUR)
     83  1.1      cgd #define MIN(t)		(((t) % SECAHOUR) / SECAMIN)
     84  1.1      cgd #define SEC(t)		((t) % SECAMIN)
     85  1.1      cgd 
     86  1.8  hubertf const char	*
     87  1.1      cgd timestr(t)
     88  1.5    lukem 	int t;
     89  1.1      cgd {
     90  1.1      cgd 	static char	s[80];
     91  1.1      cgd 
     92  1.1      cgd 	if (DAY(t) > 0)
     93  1.1      cgd 		(void)sprintf(s, "%dd+%02dhrs", DAY(t), HOUR(t));
     94  1.1      cgd 	else if (HOUR(t) > 0)
     95  1.1      cgd 		(void)sprintf(s, "%d:%02d:%02d", HOUR(t), MIN(t), SEC(t));
     96  1.1      cgd 	else if (MIN(t) > 0)
     97  1.1      cgd 		(void)sprintf(s, "%d:%02d", MIN(t), SEC(t));
     98  1.1      cgd 	else if (SEC(t) > 0)
     99  1.1      cgd 		(void)sprintf(s, ":%02d", SEC(t));
    100  1.1      cgd 	else
    101  1.1      cgd 		*s = '\0';
    102  1.1      cgd 
    103  1.1      cgd 	return (s);
    104  1.1      cgd }
    105  1.1      cgd 
    106  1.9  hubertf void
    107  1.9  hubertf open_score_file()
    108  1.9  hubertf {
    109  1.9  hubertf 	mode_t old_mask;
    110  1.9  hubertf 	int score_fd;
    111  1.9  hubertf 	int flags;
    112  1.9  hubertf 
    113  1.9  hubertf 	old_mask = umask(0);
    114  1.9  hubertf 	score_fd = open(_PATH_SCORE, O_CREAT|O_RDWR, 0664);
    115  1.9  hubertf 	umask(old_mask);
    116  1.9  hubertf 	if (score_fd < 0) {
    117  1.9  hubertf 		warn("open %s", _PATH_SCORE);
    118  1.9  hubertf 		return;
    119  1.9  hubertf 	}
    120  1.9  hubertf 	if (score_fd < 3)
    121  1.9  hubertf 		exit(1);
    122  1.9  hubertf 	/* Set the close-on-exec flag.  If this fails for any reason, quit
    123  1.9  hubertf 	 * rather than leave the score file open to tampering.  */
    124  1.9  hubertf 	flags = fcntl(score_fd, F_GETFD);
    125  1.9  hubertf 	if (flags < 0)
    126  1.9  hubertf 		err(1, "fcntl F_GETFD");
    127  1.9  hubertf 	flags |= FD_CLOEXEC;
    128  1.9  hubertf 	if (fcntl(score_fd, F_SETFD, flags) == -1)
    129  1.9  hubertf 		err(1, "fcntl F_SETFD");
    130  1.9  hubertf 	/*
    131  1.9  hubertf 	 * This is done to take advantage of stdio, while still
    132  1.9  hubertf 	 * allowing a O_CREAT during the open(2) of the log file.
    133  1.9  hubertf 	 */
    134  1.9  hubertf 	score_fp = fdopen(score_fd, "r+");
    135  1.9  hubertf 	if (score_fp == NULL) {
    136  1.9  hubertf 		warn("fdopen %s", _PATH_SCORE);
    137  1.9  hubertf 		return;
    138  1.9  hubertf 	}
    139  1.9  hubertf }
    140  1.9  hubertf 
    141  1.5    lukem int
    142  1.1      cgd log_score(list_em)
    143  1.5    lukem 	int list_em;
    144  1.1      cgd {
    145  1.9  hubertf 	int		i, num_scores = 0, good, changed = 0, found = 0;
    146  1.1      cgd 	struct passwd	*pw;
    147  1.5    lukem 	char		*cp;
    148  1.1      cgd 	SCORE		score[100], thisscore;
    149  1.1      cgd 	struct utsname	name;
    150  1.9  hubertf 	long		offset;
    151  1.1      cgd 
    152  1.9  hubertf 	if (score_fp == NULL) {
    153  1.9  hubertf 		warnx("no score file available");
    154  1.1      cgd 		return (-1);
    155  1.1      cgd 	}
    156  1.9  hubertf 
    157  1.1      cgd #ifdef BSD
    158  1.9  hubertf 	if (flock(fileno(score_fp), LOCK_EX) < 0)
    159  1.1      cgd #endif
    160  1.1      cgd #ifdef SYSV
    161  1.9  hubertf 	while (lockf(fileno(score_fp), F_LOCK, 1) < 0)
    162  1.1      cgd #endif
    163  1.1      cgd 	{
    164  1.6    lukem 		warn("flock %s", _PATH_SCORE);
    165  1.1      cgd 		return (-1);
    166  1.1      cgd 	}
    167  1.1      cgd 	for (;;) {
    168  1.9  hubertf 		good = fscanf(score_fp, SCORE_SCANF_FMT,
    169  1.1      cgd 			score[num_scores].name,
    170  1.1      cgd 			score[num_scores].host,
    171  1.1      cgd 			score[num_scores].game,
    172  1.1      cgd 			&score[num_scores].planes,
    173  1.1      cgd 			&score[num_scores].time,
    174  1.1      cgd 			&score[num_scores].real_time);
    175  1.1      cgd 		if (good != 6 || ++num_scores >= NUM_SCORES)
    176  1.1      cgd 			break;
    177  1.1      cgd 	}
    178  1.1      cgd 	if (!test_mode && !list_em) {
    179  1.1      cgd 		if ((pw = (struct passwd *) getpwuid(getuid())) == NULL) {
    180  1.1      cgd 			fprintf(stderr,
    181  1.1      cgd 				"getpwuid failed for uid %d.  Who are you?\n",
    182  1.9  hubertf 				(int)getuid());
    183  1.1      cgd 			return (-1);
    184  1.1      cgd 		}
    185  1.1      cgd 		strcpy(thisscore.name, pw->pw_name);
    186  1.1      cgd 		uname(&name);
    187  1.7  hubertf 		strncpy(thisscore.host, name.nodename, sizeof(thisscore.host)-1);
    188  1.5    lukem 		thisscore.host[sizeof(thisscore.host) - 1] = '\0';
    189  1.1      cgd 
    190  1.5    lukem 		cp = strrchr(file, '/');
    191  1.1      cgd 		if (cp == NULL) {
    192  1.1      cgd 			fprintf(stderr, "log: where's the '/' in %s?\n", file);
    193  1.1      cgd 			return (-1);
    194  1.1      cgd 		}
    195  1.1      cgd 		cp++;
    196  1.1      cgd 		strcpy(thisscore.game, cp);
    197  1.1      cgd 
    198  1.1      cgd 		thisscore.time = clck;
    199  1.1      cgd 		thisscore.planes = safe_planes;
    200  1.1      cgd 		thisscore.real_time = time(0) - start_time;
    201  1.1      cgd 
    202  1.1      cgd 		for (i = 0; i < num_scores; i++) {
    203  1.1      cgd 			if (strcmp(thisscore.name, score[i].name) == 0 &&
    204  1.1      cgd 			    strcmp(thisscore.host, score[i].host) == 0 &&
    205  1.1      cgd 			    strcmp(thisscore.game, score[i].game) == 0) {
    206  1.1      cgd 				if (thisscore.time > score[i].time) {
    207  1.1      cgd 					score[i].time = thisscore.time;
    208  1.1      cgd 					score[i].planes = thisscore.planes;
    209  1.1      cgd 					score[i].real_time =
    210  1.1      cgd 						thisscore.real_time;
    211  1.1      cgd 					changed++;
    212  1.1      cgd 				}
    213  1.1      cgd 				found++;
    214  1.1      cgd 				break;
    215  1.1      cgd 			}
    216  1.1      cgd 		}
    217  1.1      cgd 		if (!found) {
    218  1.1      cgd 			for (i = 0; i < num_scores; i++) {
    219  1.1      cgd 				if (thisscore.time > score[i].time) {
    220  1.1      cgd 					if (num_scores < NUM_SCORES)
    221  1.1      cgd 						num_scores++;
    222  1.4      tls 					memcpy(&score[num_scores - 1],
    223  1.4      tls 					       &score[i],
    224  1.4      tls 					       sizeof (score[i]));
    225  1.4      tls 					memcpy(&score[i], &thisscore,
    226  1.4      tls 					       sizeof (score[i]));
    227  1.1      cgd 					changed++;
    228  1.1      cgd 					break;
    229  1.1      cgd 				}
    230  1.1      cgd 			}
    231  1.1      cgd 		}
    232  1.1      cgd 		if (!found && !changed && num_scores < NUM_SCORES) {
    233  1.4      tls 			memcpy(&score[num_scores], &thisscore,
    234  1.4      tls 			       sizeof (score[num_scores]));
    235  1.1      cgd 			num_scores++;
    236  1.1      cgd 			changed++;
    237  1.1      cgd 		}
    238  1.1      cgd 
    239  1.1      cgd 		if (changed) {
    240  1.1      cgd 			if (found)
    241  1.1      cgd 				puts("You beat your previous score!");
    242  1.1      cgd 			else
    243  1.1      cgd 				puts("You made the top players list!");
    244  1.1      cgd 			qsort(score, num_scores, sizeof (*score), compar);
    245  1.9  hubertf 			rewind(score_fp);
    246  1.1      cgd 			for (i = 0; i < num_scores; i++)
    247  1.9  hubertf 				fprintf(score_fp, "%s %s %s %d %d %d\n",
    248  1.1      cgd 					score[i].name, score[i].host,
    249  1.1      cgd 					score[i].game, score[i].planes,
    250  1.1      cgd 					score[i].time, score[i].real_time);
    251  1.9  hubertf 			fflush(score_fp);
    252  1.9  hubertf 			if (ferror(score_fp))
    253  1.9  hubertf 				warn("error writing %s", _PATH_SCORE);
    254  1.9  hubertf 			/* It is just possible that updating an entry could
    255  1.9  hubertf 			 * have reduced the length of the file, so we
    256  1.9  hubertf 			 * truncate it.  The seeks are required for stream/fd
    257  1.9  hubertf 			 * synchronisation by POSIX.1.  */
    258  1.9  hubertf 			offset = ftell(score_fp);
    259  1.9  hubertf 			lseek(fileno(score_fp), 0, SEEK_SET);
    260  1.9  hubertf 			ftruncate(fileno(score_fp), offset);
    261  1.9  hubertf 			rewind(score_fp);
    262  1.1      cgd 		} else {
    263  1.1      cgd 			if (found)
    264  1.1      cgd 				puts("You didn't beat your previous score.");
    265  1.1      cgd 			else
    266  1.1      cgd 				puts("You didn't make the top players list.");
    267  1.1      cgd 		}
    268  1.1      cgd 		putchar('\n');
    269  1.1      cgd 	}
    270  1.1      cgd #ifdef BSD
    271  1.9  hubertf 	flock(fileno(score_fp), LOCK_UN);
    272  1.1      cgd #endif
    273  1.1      cgd #ifdef SYSV
    274  1.1      cgd 	/* lock will evaporate upon close */
    275  1.1      cgd #endif
    276  1.9  hubertf 	fclose(score_fp);
    277  1.1      cgd 	printf("%2s:  %-8s  %-8s  %-18s  %4s  %9s  %4s\n", "#", "name", "host",
    278  1.1      cgd 		"game", "time", "real time", "planes safe");
    279  1.1      cgd 	puts("-------------------------------------------------------------------------------");
    280  1.1      cgd 	for (i = 0; i < num_scores; i++) {
    281  1.5    lukem 		cp = strchr(score[i].host, '.');
    282  1.1      cgd 		if (cp != NULL)
    283  1.1      cgd 			*cp = '\0';
    284  1.1      cgd 		printf("%2d:  %-8s  %-8s  %-18s  %4d  %9s  %4d\n", i + 1,
    285  1.1      cgd 			score[i].name, score[i].host, score[i].game,
    286  1.1      cgd 			score[i].time, timestr(score[i].real_time),
    287  1.1      cgd 			score[i].planes);
    288  1.1      cgd 	}
    289  1.1      cgd 	putchar('\n');
    290  1.1      cgd 	return (0);
    291  1.5    lukem }
    292  1.5    lukem 
    293  1.5    lukem void
    294  1.5    lukem log_score_quit(dummy)
    295  1.5    lukem 	int dummy;
    296  1.5    lukem {
    297  1.5    lukem 	(void)log_score(0);
    298  1.5    lukem 	exit(0);
    299  1.1      cgd }
    300