1 /* $OpenBSD: log.c,v 1.18 2014/07/13 13:00:40 tedu 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 #include "include.h" 46 #include "pathnames.h" 47 48 static FILE *score_fp; 49 50 int 51 compar(const void *va, const void *vb) 52 { 53 const SCORE *a, *b; 54 55 a = (const SCORE *)va; 56 b = (const SCORE *)vb; 57 if (b->planes == a->planes) 58 return (b->time - a->time); 59 else 60 return (b->planes - a->planes); 61 } 62 63 #define SECAMIN 60 64 #define MINAHOUR 60 65 #define HOURADAY 24 66 #define SECAHOUR (SECAMIN * MINAHOUR) 67 #define SECADAY (SECAHOUR * HOURADAY) 68 #define DAY(t) ((t) / SECADAY) 69 #define HOUR(t) (((t) % SECADAY) / SECAHOUR) 70 #define MINUTES(t) (((t) % SECAHOUR) / SECAMIN) 71 #define SEC(t) ((t) % SECAMIN) 72 73 const char * 74 timestr(int t) 75 { 76 static char s[80]; 77 78 if (DAY(t) > 0) 79 (void)snprintf(s, sizeof s, "%dd+%02dhrs", DAY(t), HOUR(t)); 80 else if (HOUR(t) > 0) 81 (void)snprintf(s, sizeof s, "%d:%02d:%02d", 82 HOUR(t), MINUTES(t), SEC(t)); 83 else if (MINUTES(t) > 0) 84 (void)snprintf(s, sizeof s, "%d:%02d", MINUTES(t), SEC(t)); 85 else if (SEC(t) > 0) 86 (void)snprintf(s, sizeof s, ":%02d", SEC(t)); 87 else 88 *s = '\0'; 89 90 return (s); 91 } 92 93 int 94 open_score_file(void) 95 { 96 mode_t old_mode; 97 int score_fd; 98 99 old_mode = umask(0); 100 score_fd = open(_PATH_SCORE, O_CREAT|O_RDWR, 0664); 101 if (score_fd < 0) { 102 perror(_PATH_SCORE); 103 return (-1); 104 } 105 /* 106 * This is done to take advantage of stdio, while still 107 * allowing a O_CREAT during the open(2) of the log file. 108 */ 109 score_fp = fdopen(score_fd, "r+"); 110 if (score_fp == NULL) { 111 perror(_PATH_SCORE); 112 return (-1); 113 } 114 umask(old_mode); 115 return (0); 116 } 117 118 int 119 log_score(int list_em) 120 { 121 int i, num_scores = 0, good, changed = 0, found = 0; 122 struct passwd *pw; 123 char *cp; 124 char scanstr[50]; 125 SCORE score[NUM_SCORES], thisscore; 126 127 if (score_fp == NULL) 128 return (-1); 129 if (flock(fileno(score_fp), LOCK_EX) < 0) { 130 perror("flock"); 131 return (-1); 132 } 133 snprintf(scanstr, 50, "%%%zus %%%zus %%d %%d %%d", sizeof(score[0].name)-1, 134 sizeof(score[0].game)-1); 135 for (;;) { 136 good = fscanf(score_fp, scanstr, 137 score[num_scores].name, 138 score[num_scores].game, 139 &score[num_scores].planes, 140 &score[num_scores].time, 141 &score[num_scores].real_time); 142 if (good != 5 || ++num_scores >= NUM_SCORES) 143 break; 144 } 145 if (!test_mode && !list_em) { 146 if ((pw = (struct passwd *) getpwuid(getuid())) == NULL) { 147 fprintf(stderr, 148 "getpwuid failed for uid %u. Who are you?\n", 149 getuid()); 150 return (-1); 151 } 152 strlcpy(thisscore.name, pw->pw_name, sizeof(thisscore.name)); 153 154 cp = strrchr(file, '/'); 155 if (cp == NULL) { 156 warnx("log: where's the '/' in %s?", file); 157 return (-1); 158 } 159 cp++; 160 strlcpy(thisscore.game, cp, sizeof(thisscore.game)); 161 162 thisscore.time = clck; 163 thisscore.planes = safe_planes; 164 thisscore.real_time = time(0) - start_time; 165 166 for (i = 0; i < num_scores; i++) { 167 if (strcmp(thisscore.name, score[i].name) == 0 && 168 strcmp(thisscore.game, score[i].game) == 0) { 169 if (thisscore.time > score[i].time) { 170 score[i].time = thisscore.time; 171 score[i].planes = thisscore.planes; 172 score[i].real_time = 173 thisscore.real_time; 174 changed++; 175 } 176 found++; 177 break; 178 } 179 } 180 if (!found) { 181 for (i = 0; i < num_scores; i++) { 182 if (thisscore.time > score[i].time) { 183 if (num_scores < NUM_SCORES) 184 num_scores++; 185 memcpy(&score[num_scores - 1], 186 &score[i], 187 sizeof (score[i])); 188 memcpy(&score[i], &thisscore, 189 sizeof (score[i])); 190 changed++; 191 break; 192 } 193 } 194 } 195 if (!found && !changed && num_scores < NUM_SCORES) { 196 memcpy(&score[num_scores], &thisscore, 197 sizeof (score[num_scores])); 198 num_scores++; 199 changed++; 200 } 201 202 if (changed) { 203 if (found) 204 puts("You beat your previous score!"); 205 else 206 puts("You made the top players list!"); 207 qsort(score, num_scores, sizeof (*score), compar); 208 rewind(score_fp); 209 for (i = 0; i < num_scores; i++) 210 fprintf(score_fp, "%s %s %d %d %d\n", 211 score[i].name, 212 score[i].game, score[i].planes, 213 score[i].time, score[i].real_time); 214 } else { 215 if (found) 216 puts("You didn't beat your previous score."); 217 else 218 puts("You didn't make the top players list."); 219 } 220 putchar('\n'); 221 } 222 flock(fileno(score_fp), LOCK_UN); 223 fflush(score_fp); 224 fsync(fileno(score_fp)); 225 rewind(score_fp); 226 printf("%2s: %-31s %-18s %4s %9s %4s\n", "#", "name", 227 "game", "time", "real time", "safe"); 228 puts("-------------------------------------------------------------------------------"); 229 for (i = 0; i < num_scores; i++) { 230 printf("%2d: %-31s %-18s %4d %9s %4d\n", i + 1, 231 score[i].name, score[i].game, 232 score[i].time, timestr(score[i].real_time), 233 score[i].planes); 234 } 235 putchar('\n'); 236 return (0); 237 } 238 239 void 240 log_score_quit(int dummy) 241 { 242 (void)log_score(0); 243 exit(0); 244 } 245