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