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