]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - atc/log.c
1 /* $NetBSD: log.c,v 1.23 2017/01/10 20:40:53 christos 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.23 2017/01/10 20:40:53 christos Exp $");
53 #include <sys/types.h>
54 #include <sys/utsname.h>
55 #include <sys/stat.h> /* for umask(2) */
64 #include "pathnames.h"
70 static FILE *score_fp
;
73 compar(const void *va
, const void *vb
)
77 a
= (const SCORE
*)va
;
78 b
= (const SCORE
*)vb
;
79 if (b
->planes
== a
->planes
)
80 return (b
->time
- a
->time
);
82 return (b
->planes
- a
->planes
);
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)
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
),
106 (void)snprintf(s
, sizeof(s
), "%d:%02d", MIN(t
), SEC(t
));
108 (void)snprintf(s
, sizeof(s
), ":%02d", SEC(t
));
116 open_score_file(void)
123 #if defined(O_NOFOLLOW)
124 score_fd
= open(_PATH_SCORE
, O_CREAT
|O_RDWR
|O_NOFOLLOW
, 0664);
126 score_fd
= open(_PATH_SCORE
, O_CREAT
|O_RDWR
, 0664);
128 (void)umask(old_mask
);
130 warn("open %s", _PATH_SCORE
);
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
);
139 err(1, "fcntl F_GETFD");
141 if (fcntl(score_fd
, F_SETFD
, flags
) == -1)
142 err(1, "fcntl F_SETFD");
144 * This is done to take advantage of stdio, while still
145 * allowing a O_CREAT during the open(2) of the log file.
147 score_fp
= fdopen(score_fd
, "r+");
148 if (score_fp
== NULL
) {
149 warn("fdopen %s", _PATH_SCORE
);
155 log_score(int list_em
)
157 int i
, num_scores
= 0, good
, changed
= 0, found
= 0;
160 SCORE score
[100], thisscore
;
161 struct utsname lname
;
164 if (score_fp
== NULL
) {
165 warnx("no score file available");
170 if (flock(fileno(score_fp
), LOCK_EX
) < 0)
173 if (lockf(fileno(score_fp
), F_LOCK
, 1) < 0)
176 warn("flock %s", _PATH_SCORE
);
180 good
= fscanf(score_fp
, SCORE_SCANF_FMT
,
181 score
[num_scores
].name
,
182 score
[num_scores
].host
,
183 score
[num_scores
].game
,
184 &score
[num_scores
].planes
,
185 &score
[num_scores
].time
,
186 &score
[num_scores
].real_time
);
187 if (good
!= 6 || ++num_scores
>= NUM_SCORES
)
190 if (!test_mode
&& !list_em
) {
191 if ((pw
= (struct passwd
*) getpwuid(getuid())) == NULL
) {
192 (void)fprintf(stderr
,
193 "getpwuid failed for uid %d. Who are you?\n",
197 (void)strlcpy(thisscore
.name
, pw
->pw_name
, SCORE_NAME_LEN
);
199 (void)strlcpy(thisscore
.host
, lname
.nodename
,
200 sizeof(thisscore
.host
));
202 cp
= strrchr(filename
, '/');
204 (void)fprintf(stderr
, "log: where's the '/' in %s?\n",
209 (void)strlcpy(thisscore
.game
, cp
, SCORE_GAME_LEN
);
211 thisscore
.time
= clck
;
212 thisscore
.planes
= safe_planes
;
213 thisscore
.real_time
= time(0) - start_time
;
215 for (i
= 0; i
< num_scores
; i
++) {
216 if (strcmp(thisscore
.name
, score
[i
].name
) == 0 &&
217 strcmp(thisscore
.host
, score
[i
].host
) == 0 &&
218 strcmp(thisscore
.game
, score
[i
].game
) == 0) {
219 if (thisscore
.time
> score
[i
].time
) {
220 score
[i
].time
= thisscore
.time
;
221 score
[i
].planes
= thisscore
.planes
;
231 for (i
= 0; i
< num_scores
; i
++) {
232 if (thisscore
.time
> score
[i
].time
) {
233 if (num_scores
< NUM_SCORES
)
235 (void)memcpy(&score
[num_scores
- 1],
236 &score
[i
], sizeof (score
[i
]));
237 (void)memcpy(&score
[i
], &thisscore
,
244 if (!found
&& !changed
&& num_scores
< NUM_SCORES
) {
245 (void)memcpy(&score
[num_scores
], &thisscore
,
246 sizeof (score
[num_scores
]));
253 (void)puts("You beat your previous score!");
255 (void)puts("You made the top players list!");
256 qsort(score
, (size_t)num_scores
, sizeof (*score
),
259 for (i
= 0; i
< num_scores
; i
++)
260 (void)fprintf(score_fp
, "%s %s %s %d %d %d\n",
261 score
[i
].name
, score
[i
].host
,
262 score
[i
].game
, score
[i
].planes
,
263 score
[i
].time
, score
[i
].real_time
);
264 (void)fflush(score_fp
);
265 if (ferror(score_fp
))
266 warn("error writing %s", _PATH_SCORE
);
267 /* It is just possible that updating an entry could
268 * have reduced the length of the file, so we
269 * truncate it. The seeks are required for stream/fd
270 * synchronisation by POSIX.1. */
271 offset
= ftell(score_fp
);
272 (void)lseek(fileno(score_fp
), (off_t
)0, SEEK_SET
);
273 (void)ftruncate(fileno(score_fp
), (off_t
)offset
);
278 "You didn't beat your previous score.");
281 "You didn't make the top players list.");
286 (void)flock(fileno(score_fp
), LOCK_UN
);
289 /* lock will evaporate upon close */
291 (void)fclose(score_fp
);
292 (void)printf("%2s: %-8s %-8s %-18s %4s %9s %4s\n", "#", "name",
293 "host", "game", "time", "real time", "planes safe");
294 (void)printf("-------------------------------------------------------");
295 (void)printf("-------------------------\n");
296 for (i
= 0; i
< num_scores
; i
++) {
297 cp
= strchr(score
[i
].host
, '.');
300 (void)printf("%2d: %-8s %-8s %-18s %4d %9s %4d\n", i
+ 1,
301 score
[i
].name
, score
[i
].host
, score
[i
].game
,
302 score
[i
].time
, timestr(score
[i
].real_time
),
311 log_score_quit(int dummy __unused
)