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