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