]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - atc/log.c
1 /* $NetBSD: log.c,v 1.4 1997/01/13 06:50:26 tls 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.
50 static char sccsid
[] = "@(#)log.c 8.1 (Berkeley) 5/31/93";
52 static char rcsid
[] = "$NetBSD: log.c,v 1.4 1997/01/13 06:50:26 tls Exp $";
57 #include "pathnames.h"
62 if (b
->planes
== a
->planes
)
63 return (b
->time
- a
->time
);
65 return (b
->planes
- a
->planes
);
71 #define SECAHOUR (SECAMIN * MINAHOUR)
72 #define SECADAY (SECAHOUR * HOURADAY)
73 #define DAY(t) ((t) / SECADAY)
74 #define HOUR(t) (((t) % SECADAY) / SECAHOUR)
75 #define MIN(t) (((t) % SECAHOUR) / SECAMIN)
76 #define SEC(t) ((t) % SECAMIN)
84 (void)sprintf(s
, "%dd+%02dhrs", DAY(t
), HOUR(t
));
86 (void)sprintf(s
, "%d:%02d:%02d", HOUR(t
), MIN(t
), SEC(t
));
88 (void)sprintf(s
, "%d:%02d", MIN(t
), SEC(t
));
90 (void)sprintf(s
, ":%02d", SEC(t
));
99 register int i
, fd
, num_scores
= 0, good
, changed
= 0, found
= 0;
102 char *cp
, *index(), *rindex();
103 SCORE score
[100], thisscore
;
109 fd
= open(_PATH_SCORE
, O_CREAT
|O_RDWR
, 0644);
115 * This is done to take advantage of stdio, while still
116 * allowing a O_CREAT during the open(2) of the log file.
118 fp
= fdopen(fd
, "r+");
124 if (flock(fileno(fp
), LOCK_EX
) < 0)
127 while (lockf(fileno(fp
), F_LOCK
, 1) < 0)
134 good
= fscanf(fp
, "%s %s %s %d %d %d",
135 score
[num_scores
].name
,
136 score
[num_scores
].host
,
137 score
[num_scores
].game
,
138 &score
[num_scores
].planes
,
139 &score
[num_scores
].time
,
140 &score
[num_scores
].real_time
);
141 if (good
!= 6 || ++num_scores
>= NUM_SCORES
)
144 if (!test_mode
&& !list_em
) {
145 if ((pw
= (struct passwd
*) getpwuid(getuid())) == NULL
) {
147 "getpwuid failed for uid %d. Who are you?\n",
151 strcpy(thisscore
.name
, pw
->pw_name
);
153 if (gethostname(thisscore
.host
, sizeof (thisscore
.host
)) < 0) {
154 perror("gethostname");
160 strcpy(thisscore
.host
, name
.sysname
);
163 cp
= rindex(file
, '/');
165 fprintf(stderr
, "log: where's the '/' in %s?\n", file
);
169 strcpy(thisscore
.game
, cp
);
171 thisscore
.time
= clck
;
172 thisscore
.planes
= safe_planes
;
173 thisscore
.real_time
= time(0) - start_time
;
175 for (i
= 0; i
< num_scores
; i
++) {
176 if (strcmp(thisscore
.name
, score
[i
].name
) == 0 &&
177 strcmp(thisscore
.host
, score
[i
].host
) == 0 &&
178 strcmp(thisscore
.game
, score
[i
].game
) == 0) {
179 if (thisscore
.time
> score
[i
].time
) {
180 score
[i
].time
= thisscore
.time
;
181 score
[i
].planes
= thisscore
.planes
;
191 for (i
= 0; i
< num_scores
; i
++) {
192 if (thisscore
.time
> score
[i
].time
) {
193 if (num_scores
< NUM_SCORES
)
195 memcpy(&score
[num_scores
- 1],
198 memcpy(&score
[i
], &thisscore
,
205 if (!found
&& !changed
&& num_scores
< NUM_SCORES
) {
206 memcpy(&score
[num_scores
], &thisscore
,
207 sizeof (score
[num_scores
]));
214 puts("You beat your previous score!");
216 puts("You made the top players list!");
217 qsort(score
, num_scores
, sizeof (*score
), compar
);
219 for (i
= 0; i
< num_scores
; i
++)
220 fprintf(fp
, "%s %s %s %d %d %d\n",
221 score
[i
].name
, score
[i
].host
,
222 score
[i
].game
, score
[i
].planes
,
223 score
[i
].time
, score
[i
].real_time
);
226 puts("You didn't beat your previous score.");
228 puts("You didn't make the top players list.");
233 flock(fileno(fp
), LOCK_UN
);
236 /* lock will evaporate upon close */
239 printf("%2s: %-8s %-8s %-18s %4s %9s %4s\n", "#", "name", "host",
240 "game", "time", "real time", "planes safe");
241 puts("-------------------------------------------------------------------------------");
242 for (i
= 0; i
< num_scores
; i
++) {
243 cp
= index(score
[i
].host
, '.');
246 printf("%2d: %-8s %-8s %-18s %4d %9s %4d\n", i
+ 1,
247 score
[i
].name
, score
[i
].host
, score
[i
].game
,
248 score
[i
].time
, timestr(score
[i
].real_time
),