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 $
34 * $DragonFly: src/games/atc/log.c,v 1.3 2006/08/08 15:03:02 pavalos Exp $
38 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
40 * Copy permission is hereby granted provided that this notice is
41 * retained on all partial or complete copies.
43 * For more info on this and all of my stuff, mail edjames@berkeley.edu.
50 #include "pathnames.h"
52 #include <sys/utsname.h>
54 static FILE *score_fp
;
56 static int compar(const void *, const void *);
57 static const char *timestr(int);
61 compar(const void *va
, const void *vb
)
65 a
= (const SCORE
*)va
;
66 b
= (const SCORE
*)vb
;
67 if (b
->planes
== a
->planes
)
68 return (b
->time
- a
->time
);
70 return (b
->planes
- a
->planes
);
76 #define SECAHOUR (SECAMIN * MINAHOUR)
77 #define SECADAY (SECAHOUR * HOURADAY)
78 #define DAY(t) ((t) / SECADAY)
79 #define HOUR(t) (((t) % SECADAY) / SECAHOUR)
80 #define MIN(t) (((t) % SECAHOUR) / SECAMIN)
81 #define SEC(t) ((t) % SECAMIN)
89 (void)sprintf(s
, "%dd+%02dhrs", DAY(t
), HOUR(t
));
91 (void)sprintf(s
, "%d:%02d:%02d", HOUR(t
), MIN(t
), SEC(t
));
93 (void)sprintf(s
, "%d:%02d", MIN(t
), SEC(t
));
95 (void)sprintf(s
, ":%02d", SEC(t
));
103 open_score_file(void)
110 score_fd
= open(_PATH_SCORE
, O_CREAT
|O_RDWR
, 0664);
113 warn("open %s", _PATH_SCORE
);
116 /* Set the close-on-exec flag. If this fails for any reason, quit
117 * rather than leave the score file open to tampering. */
118 flags
= fcntl(score_fd
, F_GETFD
);
120 err(1, "fcntl F_GETFD");
122 if (fcntl(score_fd
, F_SETFD
, flags
) == -1)
123 err(1, "fcntl F_SETFD");
125 * This is done to take advantage of stdio, while still
126 * allowing a O_CREAT during the open(2) of the log file.
128 score_fp
= fdopen(score_fd
, "r+");
129 if (score_fp
== NULL
) {
130 warn("fdopen %s", _PATH_SCORE
);
136 log_score(int list_em
)
138 int i
, num_scores
= 0, good
, changed
= 0, found
= 0;
141 SCORE score
[100], thisscore
;
142 struct utsname xname
;
144 if (score_fp
== NULL
) {
145 warnx("no score file available");
149 if (flock(fileno(score_fp
), LOCK_EX
) < 0)
151 warn("flock %s", _PATH_SCORE
);
155 good
= fscanf(score_fp
, SCORE_SCANF_FMT
,
156 score
[num_scores
].name
,
157 score
[num_scores
].host
,
158 score
[num_scores
].game
,
159 &score
[num_scores
].planes
,
160 &score
[num_scores
].time
,
161 &score
[num_scores
].real_time
);
162 if (good
!= 6 || ++num_scores
>= NUM_SCORES
)
165 if (!test_mode
&& !list_em
) {
166 if ((pw
= (struct passwd
*) getpwuid(getuid())) == NULL
) {
168 "getpwuid failed for uid %d. Who are you?\n",
172 strcpy(thisscore
.name
, pw
->pw_name
);
175 strcpy(thisscore
.host
, xname
.nodename
);
177 cp
= rindex(filename
, '/');
179 fprintf(stderr
, "log: where's the '/' in %s?\n", filename
);
183 strcpy(thisscore
.game
, cp
);
185 thisscore
.time
= clck
;
186 thisscore
.planes
= safe_planes
;
187 thisscore
.real_time
= time(0) - start_time
;
189 for (i
= 0; i
< num_scores
; i
++) {
190 if (strcmp(thisscore
.name
, score
[i
].name
) == 0 &&
191 strcmp(thisscore
.host
, score
[i
].host
) == 0 &&
192 strcmp(thisscore
.game
, score
[i
].game
) == 0) {
193 if (thisscore
.time
> score
[i
].time
) {
194 score
[i
].time
= thisscore
.time
;
195 score
[i
].planes
= thisscore
.planes
;
205 for (i
= 0; i
< num_scores
; i
++) {
206 if (thisscore
.time
> score
[i
].time
) {
207 if (num_scores
< NUM_SCORES
)
210 &score
[num_scores
- 1],
212 bcopy(&thisscore
, &score
[i
],
219 if (!found
&& !changed
&& num_scores
< NUM_SCORES
) {
220 bcopy(&thisscore
, &score
[num_scores
],
221 sizeof (score
[num_scores
]));
228 puts("You beat your previous score!");
230 puts("You made the top players list!");
231 qsort(score
, num_scores
, sizeof (*score
), compar
);
233 for (i
= 0; i
< num_scores
; i
++)
234 fprintf(score_fp
, "%s %s %s %d %d %d\n",
235 score
[i
].name
, score
[i
].host
,
236 score
[i
].game
, score
[i
].planes
,
237 score
[i
].time
, score
[i
].real_time
);
239 if (ferror(score_fp
))
240 warn("error writing %s", _PATH_SCORE
);
241 /* It is just possible that updating an entry could
242 * have reduced the length of the file, so we
243 * truncate it. The lseek is required for stream/fd
244 * synchronisation by POSIX.1. */
245 lseek(fileno(score_fp
), 0, SEEK_END
);
246 ftruncate(fileno(score_fp
), ftell(score_fp
));
249 puts("You didn't beat your previous score.");
251 puts("You didn't make the top players list.");
255 flock(fileno(score_fp
), LOCK_UN
);
257 printf("%2s: %-8s %-8s %-18s %4s %9s %4s\n", "#", "name", "host",
258 "game", "time", "real time", "planes safe");
259 puts("-------------------------------------------------------------------------------");
260 for (i
= 0; i
< num_scores
; i
++) {
261 cp
= index(score
[i
].host
, '.');
264 printf("%2d: %-8s %-8s %-18s %4d %9s %4d\n", i
+ 1,
265 score
[i
].name
, score
[i
].host
, score
[i
].game
,
266 score
[i
].time
, timestr(score
[i
].real_time
),