1 /* $NetBSD: log.c,v 1.20 2009/05/26 00:00:56 dholland Exp $ */
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
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
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
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>
47 static char sccsid
[] = "@(#)log.c 8.1 (Berkeley) 5/31/93";
49 __RCSID("$NetBSD: log.c,v 1.20 2009/05/26 00:00:56 dholland Exp $");
54 #include "pathnames.h"
56 static FILE *score_fp
;
59 compar(const void *va
, const void *vb
)
63 a
= (const SCORE
*)va
;
64 b
= (const SCORE
*)vb
;
65 if (b
->planes
== a
->planes
)
66 return (b
->time
- a
->time
);
68 return (b
->planes
- a
->planes
);
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)
87 (void)snprintf(s
, sizeof(s
), "%dd+%02dhrs", DAY(t
), HOUR(t
));
89 (void)snprintf(s
, sizeof(s
), "%d:%02d:%02d", HOUR(t
), MIN(t
),
92 (void)snprintf(s
, sizeof(s
), "%d:%02d", MIN(t
), SEC(t
));
94 (void)snprintf(s
, sizeof(s
), ":%02d", SEC(t
));
102 open_score_file(void)
109 #if defined(O_NOFOLLOW)
110 score_fd
= open(_PATH_SCORE
, O_CREAT
|O_RDWR
|O_NOFOLLOW
, 0664);
112 score_fd
= open(_PATH_SCORE
, O_CREAT
|O_RDWR
, 0664);
114 (void)umask(old_mask
);
116 warn("open %s", _PATH_SCORE
);
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
);
125 err(1, "fcntl F_GETFD");
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
);
141 log_score(int list_em
)
143 int i
, num_scores
= 0, good
, changed
= 0, found
= 0;
146 SCORE score
[100], thisscore
;
147 struct utsname lname
;
150 if (score_fp
== NULL
) {
151 warnx("no score file available");
156 if (flock(fileno(score_fp
), LOCK_EX
) < 0)
159 if (lockf(fileno(score_fp
), F_LOCK
, 1) < 0)
162 warn("flock %s", _PATH_SCORE
);
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
)
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",
183 (void)strlcpy(thisscore
.name
, pw
->pw_name
, SCORE_NAME_LEN
);
185 (void)strlcpy(thisscore
.host
, lname
.nodename
,
186 sizeof(thisscore
.host
));
188 cp
= strrchr(filename
, '/');
190 (void)fprintf(stderr
, "log: where's the '/' in %s?\n",
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
;
217 for (i
= 0; i
< num_scores
; i
++) {
218 if (thisscore
.time
> score
[i
].time
) {
219 if (num_scores
< NUM_SCORES
)
221 (void)memcpy(&score
[num_scores
- 1],
222 &score
[i
], sizeof (score
[i
]));
223 (void)memcpy(&score
[i
], &thisscore
,
230 if (!found
&& !changed
&& num_scores
< NUM_SCORES
) {
231 (void)memcpy(&score
[num_scores
], &thisscore
,
232 sizeof (score
[num_scores
]));
239 (void)puts("You beat your previous score!");
241 (void)puts("You made the top players list!");
242 qsort(score
, (size_t)num_scores
, sizeof (*score
),
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
);
264 "You didn't beat your previous score.");
267 "You didn't make the top players list.");
272 (void)flock(fileno(score_fp
), LOCK_UN
);
275 /* lock will evaporate upon close */
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
, '.');
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
),
297 log_score_quit(int dummy __unused
)