2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
20 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
21 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
22 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
23 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
24 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
25 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
26 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
27 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
28 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
29 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
32 * @(#)log.c 8.1 (Berkeley) 5/31/93
33 * $FreeBSD: src/games/atc/log.c,v 1.7 1999/11/30 03:48:20 billf Exp $
37 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
39 * Copy permission is hereby granted provided that this notice is
40 * retained on all partial or complete copies.
42 * For more info on this and all of my stuff, mail edjames@berkeley.edu.
49 #include "pathnames.h"
51 #include <sys/utsname.h>
53 static FILE *score_fp
;
55 static int compar(const void *, const void *);
56 static const char *timestr(int);
60 compar(const void *va
, const void *vb
)
64 a
= (const SCORE
*)va
;
65 b
= (const SCORE
*)vb
;
66 if (b
->planes
== a
->planes
)
67 return (b
->time
- a
->time
);
69 return (b
->planes
- a
->planes
);
75 #define SECAHOUR (SECAMIN * MINAHOUR)
76 #define SECADAY (SECAHOUR * HOURADAY)
77 #define DAY(t) ((t) / SECADAY)
78 #define HOUR(t) (((t) % SECADAY) / SECAHOUR)
79 #define MIN(t) (((t) % SECAHOUR) / SECAMIN)
80 #define SEC(t) ((t) % SECAMIN)
88 (void)sprintf(s
, "%dd+%02dhrs", DAY(t
), HOUR(t
));
90 (void)sprintf(s
, "%d:%02d:%02d", HOUR(t
), MIN(t
), SEC(t
));
92 (void)sprintf(s
, "%d:%02d", MIN(t
), SEC(t
));
94 (void)sprintf(s
, ":%02d", SEC(t
));
102 open_score_file(void)
109 score_fd
= open(_PATH_SCORE
, O_CREAT
|O_RDWR
, 0664);
112 warn("open %s", _PATH_SCORE
);
115 /* Set the close-on-exec flag. If this fails for any reason, quit
116 * rather than leave the score file open to tampering. */
117 flags
= fcntl(score_fd
, F_GETFD
);
119 err(1, "fcntl F_GETFD");
121 if (fcntl(score_fd
, F_SETFD
, flags
) == -1)
122 err(1, "fcntl F_SETFD");
124 * This is done to take advantage of stdio, while still
125 * allowing a O_CREAT during the open(2) of the log file.
127 score_fp
= fdopen(score_fd
, "r+");
128 if (score_fp
== NULL
) {
129 warn("fdopen %s", _PATH_SCORE
);
135 log_score(int list_em
)
137 int i
, num_scores
= 0, good
, changed
= 0, found
= 0;
140 SCORE score
[100], thisscore
;
141 struct utsname xname
;
143 if (score_fp
== NULL
) {
144 warnx("no score file available");
148 if (flock(fileno(score_fp
), LOCK_EX
) < 0)
150 warn("flock %s", _PATH_SCORE
);
154 good
= fscanf(score_fp
, SCORE_SCANF_FMT
,
155 score
[num_scores
].name
,
156 score
[num_scores
].host
,
157 score
[num_scores
].game
,
158 &score
[num_scores
].planes
,
159 &score
[num_scores
].time
,
160 &score
[num_scores
].real_time
);
161 if (good
!= 6 || ++num_scores
>= NUM_SCORES
)
164 if (!test_mode
&& !list_em
) {
165 if ((pw
= (struct passwd
*) getpwuid(getuid())) == NULL
) {
167 "getpwuid failed for uid %d. Who are you?\n",
171 strcpy(thisscore
.name
, pw
->pw_name
);
174 strcpy(thisscore
.host
, xname
.nodename
);
176 cp
= rindex(filename
, '/');
178 fprintf(stderr
, "log: where's the '/' in %s?\n", filename
);
182 strcpy(thisscore
.game
, cp
);
184 thisscore
.time
= clck
;
185 thisscore
.planes
= safe_planes
;
186 thisscore
.real_time
= time(0) - start_time
;
188 for (i
= 0; i
< num_scores
; i
++) {
189 if (strcmp(thisscore
.name
, score
[i
].name
) == 0 &&
190 strcmp(thisscore
.host
, score
[i
].host
) == 0 &&
191 strcmp(thisscore
.game
, score
[i
].game
) == 0) {
192 if (thisscore
.time
> score
[i
].time
) {
193 score
[i
].time
= thisscore
.time
;
194 score
[i
].planes
= thisscore
.planes
;
204 for (i
= 0; i
< num_scores
; i
++) {
205 if (thisscore
.time
> score
[i
].time
) {
206 if (num_scores
< NUM_SCORES
)
209 &score
[num_scores
- 1],
211 bcopy(&thisscore
, &score
[i
],
218 if (!found
&& !changed
&& num_scores
< NUM_SCORES
) {
219 bcopy(&thisscore
, &score
[num_scores
],
220 sizeof (score
[num_scores
]));
227 puts("You beat your previous score!");
229 puts("You made the top players list!");
230 qsort(score
, num_scores
, sizeof (*score
), compar
);
232 for (i
= 0; i
< num_scores
; i
++)
233 fprintf(score_fp
, "%s %s %s %d %d %d\n",
234 score
[i
].name
, score
[i
].host
,
235 score
[i
].game
, score
[i
].planes
,
236 score
[i
].time
, score
[i
].real_time
);
238 if (ferror(score_fp
))
239 warn("error writing %s", _PATH_SCORE
);
240 /* It is just possible that updating an entry could
241 * have reduced the length of the file, so we
242 * truncate it. The lseek is required for stream/fd
243 * synchronisation by POSIX.1. */
244 lseek(fileno(score_fp
), 0, SEEK_END
);
245 ftruncate(fileno(score_fp
), ftell(score_fp
));
248 puts("You didn't beat your previous score.");
250 puts("You didn't make the top players list.");
254 flock(fileno(score_fp
), LOCK_UN
);
256 printf("%2s: %-8s %-8s %-18s %4s %9s %4s\n", "#", "name", "host",
257 "game", "time", "real time", "planes safe");
258 puts("-------------------------------------------------------------------------------");
259 for (i
= 0; i
< num_scores
; i
++) {
260 cp
= index(score
[i
].host
, '.');
263 printf("%2d: %-8s %-8s %-18s %4d %9s %4d\n", i
+ 1,
264 score
[i
].name
, score
[i
].host
, score
[i
].game
,
265 score
[i
].time
, timestr(score
[i
].real_time
),
273 log_score_quit(__unused
int sig
)