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