]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - atc/log.c
1 /* $NetBSD: log.c,v 1.11 2001/01/16 02:50:28 cgd 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. All advertising materials mentioning features or use of this software
19 * must display the following acknowledgement:
20 * This product includes software developed by the University of
21 * California, Berkeley and its contributors.
22 * 4. Neither the name of the University nor the names of its contributors
23 * may be used to endorse or promote products derived from this software
24 * without specific prior written permission.
26 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
27 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
28 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
29 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
30 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
31 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
32 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
33 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
34 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
35 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
40 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
42 * Copy permission is hereby granted provided that this notice is
43 * retained on all partial or complete copies.
45 * For more info on this and all of my stuff, mail edjames@berkeley.edu.
48 #include <sys/cdefs.h>
51 static char sccsid
[] = "@(#)log.c 8.1 (Berkeley) 5/31/93";
53 __RCSID("$NetBSD: log.c,v 1.11 2001/01/16 02:50:28 cgd Exp $");
58 #include "pathnames.h"
60 static FILE *score_fp
;
68 a
= (const SCORE
*)va
;
69 b
= (const SCORE
*)vb
;
70 if (b
->planes
== a
->planes
)
71 return (b
->time
- a
->time
);
73 return (b
->planes
- a
->planes
);
79 #define SECAHOUR (SECAMIN * MINAHOUR)
80 #define SECADAY (SECAHOUR * HOURADAY)
81 #define DAY(t) ((t) / SECADAY)
82 #define HOUR(t) (((t) % SECADAY) / SECAHOUR)
83 #define MIN(t) (((t) % SECAHOUR) / SECAMIN)
84 #define SEC(t) ((t) % SECAMIN)
93 (void)sprintf(s
, "%dd+%02dhrs", DAY(t
), HOUR(t
));
95 (void)sprintf(s
, "%d:%02d:%02d", HOUR(t
), MIN(t
), SEC(t
));
97 (void)sprintf(s
, "%d:%02d", MIN(t
), SEC(t
));
99 (void)sprintf(s
, ":%02d", SEC(t
));
114 score_fd
= open(_PATH_SCORE
, O_CREAT
|O_RDWR
, 0664);
117 warn("open %s", _PATH_SCORE
);
122 /* Set the close-on-exec flag. If this fails for any reason, quit
123 * rather than leave the score file open to tampering. */
124 flags
= fcntl(score_fd
, F_GETFD
);
126 err(1, "fcntl F_GETFD");
128 if (fcntl(score_fd
, F_SETFD
, flags
) == -1)
129 err(1, "fcntl F_SETFD");
131 * This is done to take advantage of stdio, while still
132 * allowing a O_CREAT during the open(2) of the log file.
134 score_fp
= fdopen(score_fd
, "r+");
135 if (score_fp
== NULL
) {
136 warn("fdopen %s", _PATH_SCORE
);
145 int i
, num_scores
= 0, good
, changed
= 0, found
= 0;
148 SCORE score
[100], thisscore
;
152 if (score_fp
== NULL
) {
153 warnx("no score file available");
158 if (flock(fileno(score_fp
), LOCK_EX
) < 0)
161 while (lockf(fileno(score_fp
), F_LOCK
, 1) < 0)
164 warn("flock %s", _PATH_SCORE
);
168 good
= fscanf(score_fp
, SCORE_SCANF_FMT
,
169 score
[num_scores
].name
,
170 score
[num_scores
].host
,
171 score
[num_scores
].game
,
172 &score
[num_scores
].planes
,
173 &score
[num_scores
].time
,
174 &score
[num_scores
].real_time
);
175 if (good
!= 6 || ++num_scores
>= NUM_SCORES
)
178 if (!test_mode
&& !list_em
) {
179 if ((pw
= (struct passwd
*) getpwuid(getuid())) == NULL
) {
181 "getpwuid failed for uid %d. Who are you?\n",
185 strcpy(thisscore
.name
, pw
->pw_name
);
187 strncpy(thisscore
.host
, name
.nodename
, sizeof(thisscore
.host
)-1);
188 thisscore
.host
[sizeof(thisscore
.host
) - 1] = '\0';
190 cp
= strrchr(file
, '/');
192 fprintf(stderr
, "log: where's the '/' in %s?\n", file
);
196 strcpy(thisscore
.game
, cp
);
198 thisscore
.time
= clck
;
199 thisscore
.planes
= safe_planes
;
200 thisscore
.real_time
= time(0) - start_time
;
202 for (i
= 0; i
< num_scores
; i
++) {
203 if (strcmp(thisscore
.name
, score
[i
].name
) == 0 &&
204 strcmp(thisscore
.host
, score
[i
].host
) == 0 &&
205 strcmp(thisscore
.game
, score
[i
].game
) == 0) {
206 if (thisscore
.time
> score
[i
].time
) {
207 score
[i
].time
= thisscore
.time
;
208 score
[i
].planes
= thisscore
.planes
;
218 for (i
= 0; i
< num_scores
; i
++) {
219 if (thisscore
.time
> score
[i
].time
) {
220 if (num_scores
< NUM_SCORES
)
222 memcpy(&score
[num_scores
- 1],
225 memcpy(&score
[i
], &thisscore
,
232 if (!found
&& !changed
&& num_scores
< NUM_SCORES
) {
233 memcpy(&score
[num_scores
], &thisscore
,
234 sizeof (score
[num_scores
]));
241 puts("You beat your previous score!");
243 puts("You made the top players list!");
244 qsort(score
, num_scores
, sizeof (*score
), compar
);
246 for (i
= 0; i
< num_scores
; i
++)
247 fprintf(score_fp
, "%s %s %s %d %d %d\n",
248 score
[i
].name
, score
[i
].host
,
249 score
[i
].game
, score
[i
].planes
,
250 score
[i
].time
, score
[i
].real_time
);
252 if (ferror(score_fp
))
253 warn("error writing %s", _PATH_SCORE
);
254 /* It is just possible that updating an entry could
255 * have reduced the length of the file, so we
256 * truncate it. The seeks are required for stream/fd
257 * synchronisation by POSIX.1. */
258 offset
= ftell(score_fp
);
259 lseek(fileno(score_fp
), 0, SEEK_SET
);
260 ftruncate(fileno(score_fp
), offset
);
264 puts("You didn't beat your previous score.");
266 puts("You didn't make the top players list.");
271 flock(fileno(score_fp
), LOCK_UN
);
274 /* lock will evaporate upon close */
277 printf("%2s: %-8s %-8s %-18s %4s %9s %4s\n", "#", "name", "host",
278 "game", "time", "real time", "planes safe");
279 puts("-------------------------------------------------------------------------------");
280 for (i
= 0; i
< num_scores
; i
++) {
281 cp
= strchr(score
[i
].host
, '.');
284 printf("%2d: %-8s %-8s %-18s %4d %9s %4d\n", i
+ 1,
285 score
[i
].name
, score
[i
].host
, score
[i
].game
,
286 score
[i
].time
, timestr(score
[i
].real_time
),
294 log_score_quit(dummy
)
295 int dummy
__attribute__((__unused__
));