Home | History | Annotate | Line # | Download | only in atc
log.c revision 1.1
      1  1.1  cgd /*-
      2  1.1  cgd  * Copyright (c) 1990 The Regents of the University of California.
      3  1.1  cgd  * All rights reserved.
      4  1.1  cgd  *
      5  1.1  cgd  * This code is derived from software contributed to Berkeley by
      6  1.1  cgd  * Ed James.
      7  1.1  cgd  *
      8  1.1  cgd  * Redistribution and use in source and binary forms, with or without
      9  1.1  cgd  * modification, are permitted provided that the following conditions
     10  1.1  cgd  * are met:
     11  1.1  cgd  * 1. Redistributions of source code must retain the above copyright
     12  1.1  cgd  *    notice, this list of conditions and the following disclaimer.
     13  1.1  cgd  * 2. Redistributions in binary form must reproduce the above copyright
     14  1.1  cgd  *    notice, this list of conditions and the following disclaimer in the
     15  1.1  cgd  *    documentation and/or other materials provided with the distribution.
     16  1.1  cgd  * 3. All advertising materials mentioning features or use of this software
     17  1.1  cgd  *    must display the following acknowledgement:
     18  1.1  cgd  *	This product includes software developed by the University of
     19  1.1  cgd  *	California, Berkeley and its contributors.
     20  1.1  cgd  * 4. Neither the name of the University nor the names of its contributors
     21  1.1  cgd  *    may be used to endorse or promote products derived from this software
     22  1.1  cgd  *    without specific prior written permission.
     23  1.1  cgd  *
     24  1.1  cgd  * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
     25  1.1  cgd  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
     26  1.1  cgd  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
     27  1.1  cgd  * ARE DISCLAIMED.  IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
     28  1.1  cgd  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
     29  1.1  cgd  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
     30  1.1  cgd  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
     31  1.1  cgd  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
     32  1.1  cgd  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
     33  1.1  cgd  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     34  1.1  cgd  * SUCH DAMAGE.
     35  1.1  cgd  */
     36  1.1  cgd 
     37  1.1  cgd /*
     38  1.1  cgd  * Copyright (c) 1987 by Ed James, UC Berkeley.  All rights reserved.
     39  1.1  cgd  *
     40  1.1  cgd  * Copy permission is hereby granted provided that this notice is
     41  1.1  cgd  * retained on all partial or complete copies.
     42  1.1  cgd  *
     43  1.1  cgd  * For more info on this and all of my stuff, mail edjames (at) berkeley.edu.
     44  1.1  cgd  */
     45  1.1  cgd 
     46  1.1  cgd #ifndef lint
     47  1.1  cgd static char sccsid[] = "@(#)log.c	5.7 (Berkeley) 10/30/90";
     48  1.1  cgd #endif not lint
     49  1.1  cgd 
     50  1.1  cgd #include "include.h"
     51  1.1  cgd #include "pathnames.h"
     52  1.1  cgd 
     53  1.1  cgd compar(a, b)
     54  1.1  cgd 	SCORE	*a, *b;
     55  1.1  cgd {
     56  1.1  cgd 	if (b->planes == a->planes)
     57  1.1  cgd 		return (b->time - a->time);
     58  1.1  cgd 	else
     59  1.1  cgd 		return (b->planes - a->planes);
     60  1.1  cgd }
     61  1.1  cgd 
     62  1.1  cgd #define SECAMIN		60
     63  1.1  cgd #define MINAHOUR	60
     64  1.1  cgd #define HOURADAY	24
     65  1.1  cgd #define SECAHOUR	(SECAMIN * MINAHOUR)
     66  1.1  cgd #define SECADAY		(SECAHOUR * HOURADAY)
     67  1.1  cgd #define DAY(t)		((t) / SECADAY)
     68  1.1  cgd #define HOUR(t)		(((t) % SECADAY) / SECAHOUR)
     69  1.1  cgd #define MIN(t)		(((t) % SECAHOUR) / SECAMIN)
     70  1.1  cgd #define SEC(t)		((t) % SECAMIN)
     71  1.1  cgd 
     72  1.1  cgd char	*
     73  1.1  cgd timestr(t)
     74  1.1  cgd {
     75  1.1  cgd 	static char	s[80];
     76  1.1  cgd 
     77  1.1  cgd 	if (DAY(t) > 0)
     78  1.1  cgd 		(void)sprintf(s, "%dd+%02dhrs", DAY(t), HOUR(t));
     79  1.1  cgd 	else if (HOUR(t) > 0)
     80  1.1  cgd 		(void)sprintf(s, "%d:%02d:%02d", HOUR(t), MIN(t), SEC(t));
     81  1.1  cgd 	else if (MIN(t) > 0)
     82  1.1  cgd 		(void)sprintf(s, "%d:%02d", MIN(t), SEC(t));
     83  1.1  cgd 	else if (SEC(t) > 0)
     84  1.1  cgd 		(void)sprintf(s, ":%02d", SEC(t));
     85  1.1  cgd 	else
     86  1.1  cgd 		*s = '\0';
     87  1.1  cgd 
     88  1.1  cgd 	return (s);
     89  1.1  cgd }
     90  1.1  cgd 
     91  1.1  cgd log_score(list_em)
     92  1.1  cgd {
     93  1.1  cgd 	register int	i, fd, num_scores = 0, good, changed = 0, found = 0;
     94  1.1  cgd 	struct passwd	*pw;
     95  1.1  cgd 	FILE		*fp;
     96  1.1  cgd 	char		*cp, *index(), *rindex();
     97  1.1  cgd 	SCORE		score[100], thisscore;
     98  1.1  cgd #ifdef SYSV
     99  1.1  cgd 	struct utsname	name;
    100  1.1  cgd #endif
    101  1.1  cgd 
    102  1.1  cgd 	umask(0);
    103  1.1  cgd 	fd = open(_PATH_SCORE, O_CREAT|O_RDWR, 0644);
    104  1.1  cgd 	if (fd < 0) {
    105  1.1  cgd 		perror(_PATH_SCORE);
    106  1.1  cgd 		return (-1);
    107  1.1  cgd 	}
    108  1.1  cgd 	/*
    109  1.1  cgd 	 * This is done to take advantage of stdio, while still
    110  1.1  cgd 	 * allowing a O_CREAT during the open(2) of the log file.
    111  1.1  cgd 	 */
    112  1.1  cgd 	fp = fdopen(fd, "r+");
    113  1.1  cgd 	if (fp == NULL) {
    114  1.1  cgd 		perror(_PATH_SCORE);
    115  1.1  cgd 		return (-1);
    116  1.1  cgd 	}
    117  1.1  cgd #ifdef BSD
    118  1.1  cgd 	if (flock(fileno(fp), LOCK_EX) < 0)
    119  1.1  cgd #endif
    120  1.1  cgd #ifdef SYSV
    121  1.1  cgd 	while (lockf(fileno(fp), F_LOCK, 1) < 0)
    122  1.1  cgd #endif
    123  1.1  cgd 	{
    124  1.1  cgd 		perror("flock");
    125  1.1  cgd 		return (-1);
    126  1.1  cgd 	}
    127  1.1  cgd 	for (;;) {
    128  1.1  cgd 		good = fscanf(fp, "%s %s %s %d %d %d",
    129  1.1  cgd 			score[num_scores].name,
    130  1.1  cgd 			score[num_scores].host,
    131  1.1  cgd 			score[num_scores].game,
    132  1.1  cgd 			&score[num_scores].planes,
    133  1.1  cgd 			&score[num_scores].time,
    134  1.1  cgd 			&score[num_scores].real_time);
    135  1.1  cgd 		if (good != 6 || ++num_scores >= NUM_SCORES)
    136  1.1  cgd 			break;
    137  1.1  cgd 	}
    138  1.1  cgd 	if (!test_mode && !list_em) {
    139  1.1  cgd 		if ((pw = (struct passwd *) getpwuid(getuid())) == NULL) {
    140  1.1  cgd 			fprintf(stderr,
    141  1.1  cgd 				"getpwuid failed for uid %d.  Who are you?\n",
    142  1.1  cgd 				getuid());
    143  1.1  cgd 			return (-1);
    144  1.1  cgd 		}
    145  1.1  cgd 		strcpy(thisscore.name, pw->pw_name);
    146  1.1  cgd #ifdef BSD
    147  1.1  cgd 		if (gethostname(thisscore.host, sizeof (thisscore.host)) < 0) {
    148  1.1  cgd 			perror("gethostname");
    149  1.1  cgd 			return (-1);
    150  1.1  cgd 		}
    151  1.1  cgd #endif
    152  1.1  cgd #ifdef SYSV
    153  1.1  cgd 		uname(&name);
    154  1.1  cgd 		strcpy(thisscore.host, name.sysname);
    155  1.1  cgd #endif
    156  1.1  cgd 
    157  1.1  cgd 		cp = rindex(file, '/');
    158  1.1  cgd 		if (cp == NULL) {
    159  1.1  cgd 			fprintf(stderr, "log: where's the '/' in %s?\n", file);
    160  1.1  cgd 			return (-1);
    161  1.1  cgd 		}
    162  1.1  cgd 		cp++;
    163  1.1  cgd 		strcpy(thisscore.game, cp);
    164  1.1  cgd 
    165  1.1  cgd 		thisscore.time = clck;
    166  1.1  cgd 		thisscore.planes = safe_planes;
    167  1.1  cgd 		thisscore.real_time = time(0) - start_time;
    168  1.1  cgd 
    169  1.1  cgd 		for (i = 0; i < num_scores; i++) {
    170  1.1  cgd 			if (strcmp(thisscore.name, score[i].name) == 0 &&
    171  1.1  cgd 			    strcmp(thisscore.host, score[i].host) == 0 &&
    172  1.1  cgd 			    strcmp(thisscore.game, score[i].game) == 0) {
    173  1.1  cgd 				if (thisscore.time > score[i].time) {
    174  1.1  cgd 					score[i].time = thisscore.time;
    175  1.1  cgd 					score[i].planes = thisscore.planes;
    176  1.1  cgd 					score[i].real_time =
    177  1.1  cgd 						thisscore.real_time;
    178  1.1  cgd 					changed++;
    179  1.1  cgd 				}
    180  1.1  cgd 				found++;
    181  1.1  cgd 				break;
    182  1.1  cgd 			}
    183  1.1  cgd 		}
    184  1.1  cgd 		if (!found) {
    185  1.1  cgd 			for (i = 0; i < num_scores; i++) {
    186  1.1  cgd 				if (thisscore.time > score[i].time) {
    187  1.1  cgd 					if (num_scores < NUM_SCORES)
    188  1.1  cgd 						num_scores++;
    189  1.1  cgd 					bcopy(&score[i],
    190  1.1  cgd 						&score[num_scores - 1],
    191  1.1  cgd 						sizeof (score[i]));
    192  1.1  cgd 					bcopy(&thisscore, &score[i],
    193  1.1  cgd 						sizeof (score[i]));
    194  1.1  cgd 					changed++;
    195  1.1  cgd 					break;
    196  1.1  cgd 				}
    197  1.1  cgd 			}
    198  1.1  cgd 		}
    199  1.1  cgd 		if (!found && !changed && num_scores < NUM_SCORES) {
    200  1.1  cgd 			bcopy(&thisscore, &score[num_scores],
    201  1.1  cgd 				sizeof (score[num_scores]));
    202  1.1  cgd 			num_scores++;
    203  1.1  cgd 			changed++;
    204  1.1  cgd 		}
    205  1.1  cgd 
    206  1.1  cgd 		if (changed) {
    207  1.1  cgd 			if (found)
    208  1.1  cgd 				puts("You beat your previous score!");
    209  1.1  cgd 			else
    210  1.1  cgd 				puts("You made the top players list!");
    211  1.1  cgd 			qsort(score, num_scores, sizeof (*score), compar);
    212  1.1  cgd 			rewind(fp);
    213  1.1  cgd 			for (i = 0; i < num_scores; i++)
    214  1.1  cgd 				fprintf(fp, "%s %s %s %d %d %d\n",
    215  1.1  cgd 					score[i].name, score[i].host,
    216  1.1  cgd 					score[i].game, score[i].planes,
    217  1.1  cgd 					score[i].time, score[i].real_time);
    218  1.1  cgd 		} else {
    219  1.1  cgd 			if (found)
    220  1.1  cgd 				puts("You didn't beat your previous score.");
    221  1.1  cgd 			else
    222  1.1  cgd 				puts("You didn't make the top players list.");
    223  1.1  cgd 		}
    224  1.1  cgd 		putchar('\n');
    225  1.1  cgd 	}
    226  1.1  cgd #ifdef BSD
    227  1.1  cgd 	flock(fileno(fp), LOCK_UN);
    228  1.1  cgd #endif
    229  1.1  cgd #ifdef SYSV
    230  1.1  cgd 	/* lock will evaporate upon close */
    231  1.1  cgd #endif
    232  1.1  cgd 	fclose(fp);
    233  1.1  cgd 	printf("%2s:  %-8s  %-8s  %-18s  %4s  %9s  %4s\n", "#", "name", "host",
    234  1.1  cgd 		"game", "time", "real time", "planes safe");
    235  1.1  cgd 	puts("-------------------------------------------------------------------------------");
    236  1.1  cgd 	for (i = 0; i < num_scores; i++) {
    237  1.1  cgd 		cp = index(score[i].host, '.');
    238  1.1  cgd 		if (cp != NULL)
    239  1.1  cgd 			*cp = '\0';
    240  1.1  cgd 		printf("%2d:  %-8s  %-8s  %-18s  %4d  %9s  %4d\n", i + 1,
    241  1.1  cgd 			score[i].name, score[i].host, score[i].game,
    242  1.1  cgd 			score[i].time, timestr(score[i].real_time),
    243  1.1  cgd 			score[i].planes);
    244  1.1  cgd 	}
    245  1.1  cgd 	putchar('\n');
    246  1.1  cgd 	return (0);
    247  1.1  cgd }
    248