Sync usage with man page.
[netbsd-mini2440.git] / games / atc / log.c
blob38b1889f970c2f4ae527c8c80b422fabe9539e76
1 /* $NetBSD: log.c,v 1.20 2009/05/26 00:00:56 dholland Exp $ */
3 /*-
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
7 * This code is derived from software contributed to Berkeley by
8 * Ed James.
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.
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.
36 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
38 * Copy permission is hereby granted provided that this notice is
39 * retained on all partial or complete copies.
41 * For more info on this and all of my stuff, mail edjames@berkeley.edu.
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.20 2009/05/26 00:00:56 dholland Exp $");
50 #endif
51 #endif /* not lint */
53 #include "include.h"
54 #include "pathnames.h"
56 static FILE *score_fp;
58 static int
59 compar(const void *va, const void *vb)
61 const SCORE *a, *b;
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);
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 MIN(t) (((t) % SECAHOUR) / SECAMIN)
79 #define SEC(t) ((t) % SECAMIN)
81 static const char *
82 timestr(int t)
84 static char s[80];
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", HOUR(t), MIN(t),
90 SEC(t));
91 else if (MIN(t) > 0)
92 (void)snprintf(s, sizeof(s), "%d:%02d", MIN(t), SEC(t));
93 else if (SEC(t) > 0)
94 (void)snprintf(s, sizeof(s), ":%02d", SEC(t));
95 else
96 *s = '\0';
98 return (s);
101 void
102 open_score_file(void)
104 mode_t old_mask;
105 int score_fd;
106 int flags;
108 old_mask = umask(0);
109 #if defined(O_NOFOLLOW)
110 score_fd = open(_PATH_SCORE, O_CREAT|O_RDWR|O_NOFOLLOW, 0664);
111 #else
112 score_fd = open(_PATH_SCORE, O_CREAT|O_RDWR, 0664);
113 #endif
114 (void)umask(old_mask);
115 if (score_fd < 0) {
116 warn("open %s", _PATH_SCORE);
117 return;
119 if (score_fd < 3)
120 exit(1);
121 /* Set the close-on-exec flag. If this fails for any reason, quit
122 * rather than leave the score file open to tampering. */
123 flags = fcntl(score_fd, F_GETFD);
124 if (flags < 0)
125 err(1, "fcntl F_GETFD");
126 flags |= FD_CLOEXEC;
127 if (fcntl(score_fd, F_SETFD, flags) == -1)
128 err(1, "fcntl F_SETFD");
130 * This is done to take advantage of stdio, while still
131 * allowing a O_CREAT during the open(2) of the log file.
133 score_fp = fdopen(score_fd, "r+");
134 if (score_fp == NULL) {
135 warn("fdopen %s", _PATH_SCORE);
136 return;
141 log_score(int list_em)
143 int i, num_scores = 0, good, changed = 0, found = 0;
144 struct passwd *pw;
145 char *cp;
146 SCORE score[100], thisscore;
147 struct utsname lname;
148 long offset;
150 if (score_fp == NULL) {
151 warnx("no score file available");
152 return (-1);
155 #ifdef BSD
156 if (flock(fileno(score_fp), LOCK_EX) < 0)
157 #endif
158 #ifdef SYSV
159 if (lockf(fileno(score_fp), F_LOCK, 1) < 0)
160 #endif
162 warn("flock %s", _PATH_SCORE);
163 return (-1);
165 for (;;) {
166 good = fscanf(score_fp, SCORE_SCANF_FMT,
167 score[num_scores].name,
168 score[num_scores].host,
169 score[num_scores].game,
170 &score[num_scores].planes,
171 &score[num_scores].time,
172 &score[num_scores].real_time);
173 if (good != 6 || ++num_scores >= NUM_SCORES)
174 break;
176 if (!test_mode && !list_em) {
177 if ((pw = (struct passwd *) getpwuid(getuid())) == NULL) {
178 (void)fprintf(stderr,
179 "getpwuid failed for uid %d. Who are you?\n",
180 (int)getuid());
181 return (-1);
183 (void)strlcpy(thisscore.name, pw->pw_name, SCORE_NAME_LEN);
184 (void)uname(&lname);
185 (void)strlcpy(thisscore.host, lname.nodename,
186 sizeof(thisscore.host));
188 cp = strrchr(filename, '/');
189 if (cp == NULL) {
190 (void)fprintf(stderr, "log: where's the '/' in %s?\n",
191 filename);
192 return (-1);
194 cp++;
195 (void)strlcpy(thisscore.game, cp, SCORE_GAME_LEN);
197 thisscore.time = clck;
198 thisscore.planes = safe_planes;
199 thisscore.real_time = time(0) - start_time;
201 for (i = 0; i < num_scores; i++) {
202 if (strcmp(thisscore.name, score[i].name) == 0 &&
203 strcmp(thisscore.host, score[i].host) == 0 &&
204 strcmp(thisscore.game, score[i].game) == 0) {
205 if (thisscore.time > score[i].time) {
206 score[i].time = thisscore.time;
207 score[i].planes = thisscore.planes;
208 score[i].real_time =
209 thisscore.real_time;
210 changed++;
212 found++;
213 break;
216 if (!found) {
217 for (i = 0; i < num_scores; i++) {
218 if (thisscore.time > score[i].time) {
219 if (num_scores < NUM_SCORES)
220 num_scores++;
221 (void)memcpy(&score[num_scores - 1],
222 &score[i], sizeof (score[i]));
223 (void)memcpy(&score[i], &thisscore,
224 sizeof (score[i]));
225 changed++;
226 break;
230 if (!found && !changed && num_scores < NUM_SCORES) {
231 (void)memcpy(&score[num_scores], &thisscore,
232 sizeof (score[num_scores]));
233 num_scores++;
234 changed++;
237 if (changed) {
238 if (found)
239 (void)puts("You beat your previous score!");
240 else
241 (void)puts("You made the top players list!");
242 qsort(score, (size_t)num_scores, sizeof (*score),
243 compar);
244 rewind(score_fp);
245 for (i = 0; i < num_scores; i++)
246 (void)fprintf(score_fp, "%s %s %s %d %d %d\n",
247 score[i].name, score[i].host,
248 score[i].game, score[i].planes,
249 score[i].time, score[i].real_time);
250 (void)fflush(score_fp);
251 if (ferror(score_fp))
252 warn("error writing %s", _PATH_SCORE);
253 /* It is just possible that updating an entry could
254 * have reduced the length of the file, so we
255 * truncate it. The seeks are required for stream/fd
256 * synchronisation by POSIX.1. */
257 offset = ftell(score_fp);
258 (void)lseek(fileno(score_fp), (off_t)0, SEEK_SET);
259 (void)ftruncate(fileno(score_fp), (off_t)offset);
260 rewind(score_fp);
261 } else {
262 if (found)
263 (void)puts(
264 "You didn't beat your previous score.");
265 else
266 (void)puts(
267 "You didn't make the top players list.");
269 (void)putchar('\n');
271 #ifdef BSD
272 (void)flock(fileno(score_fp), LOCK_UN);
273 #endif
274 #ifdef SYSV
275 /* lock will evaporate upon close */
276 #endif
277 (void)fclose(score_fp);
278 (void)printf("%2s: %-8s %-8s %-18s %4s %9s %4s\n", "#", "name",
279 "host", "game", "time", "real time", "planes safe");
280 (void)printf("-------------------------------------------------------");
281 (void)printf("-------------------------\n");
282 for (i = 0; i < num_scores; i++) {
283 cp = strchr(score[i].host, '.');
284 if (cp != NULL)
285 *cp = '\0';
286 (void)printf("%2d: %-8s %-8s %-18s %4d %9s %4d\n", i + 1,
287 score[i].name, score[i].host, score[i].game,
288 score[i].time, timestr(score[i].real_time),
289 score[i].planes);
291 (void)putchar('\n');
292 return (0);
295 /* ARGSUSED */
296 void
297 log_score_quit(int dummy __unused)
299 (void)log_score(0);
300 exit(0);