]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - atc/log.c
1 /* $NetBSD: log.c,v 1.24 2019/03/19 00:11:34 pgoyette 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.24 2019/03/19 00:11:34 pgoyette 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 (safe_planes
== 1)
165 printf("You directed 1 plane safely to its destination.\n\n");
167 printf("You directed %d planes safely to their destinations.\n\n",
170 if (score_fp
== NULL
) {
171 warnx("no score file available");
176 if (flock(fileno(score_fp
), LOCK_EX
) < 0)
179 if (lockf(fileno(score_fp
), F_LOCK
, 1) < 0)
182 warn("flock %s", _PATH_SCORE
);
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
)
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",
203 (void)strlcpy(thisscore
.name
, pw
->pw_name
, SCORE_NAME_LEN
);
205 (void)strlcpy(thisscore
.host
, lname
.nodename
,
206 sizeof(thisscore
.host
));
208 cp
= strrchr(filename
, '/');
210 (void)fprintf(stderr
, "log: where's the '/' in %s?\n",
215 (void)strlcpy(thisscore
.game
, cp
, SCORE_GAME_LEN
);
217 thisscore
.time
= clck
;
218 thisscore
.planes
= safe_planes
;
219 thisscore
.real_time
= time(0) - start_time
;
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
;
237 for (i
= 0; i
< num_scores
; i
++) {
238 if (thisscore
.time
> score
[i
].time
) {
239 if (num_scores
< NUM_SCORES
)
241 (void)memcpy(&score
[num_scores
- 1],
242 &score
[i
], sizeof (score
[i
]));
243 (void)memcpy(&score
[i
], &thisscore
,
250 if (!found
&& !changed
&& num_scores
< NUM_SCORES
) {
251 (void)memcpy(&score
[num_scores
], &thisscore
,
252 sizeof (score
[num_scores
]));
259 (void)puts("You beat your previous score!");
261 (void)puts("You made the top players list!");
262 qsort(score
, (size_t)num_scores
, sizeof (*score
),
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
);
284 "You didn't beat your previous score.");
287 "You didn't make the top players list.");
292 (void)flock(fileno(score_fp
), LOCK_UN
);
295 /* lock will evaporate upon close */
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
, '.');
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
),
317 log_score_quit(int dummy __unused
)