]>
git.cameronkatri.com Git - bsdgames-darwin.git/blob - atc/log.c
2 * Copyright (c) 1990 The Regents of the University of California.
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. All advertising materials mentioning features or use of this software
17 * must display the following acknowledgement:
18 * This product includes software developed by the University of
19 * California, Berkeley and its contributors.
20 * 4. Neither the name of the University nor the names of its contributors
21 * may be used to endorse or promote products derived from this software
22 * without specific prior written permission.
24 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
25 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
28 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
29 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
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.
47 static char sccsid
[] = "@(#)log.c 5.7 (Berkeley) 10/30/90";
51 #include "pathnames.h"
56 if (b
->planes
== a
->planes
)
57 return (b
->time
- a
->time
);
59 return (b
->planes
- a
->planes
);
65 #define SECAHOUR (SECAMIN * MINAHOUR)
66 #define SECADAY (SECAHOUR * HOURADAY)
67 #define DAY(t) ((t) / SECADAY)
68 #define HOUR(t) (((t) % SECADAY) / SECAHOUR)
69 #define MIN(t) (((t) % SECAHOUR) / SECAMIN)
70 #define SEC(t) ((t) % SECAMIN)
78 (void)sprintf(s
, "%dd+%02dhrs", DAY(t
), HOUR(t
));
80 (void)sprintf(s
, "%d:%02d:%02d", HOUR(t
), MIN(t
), SEC(t
));
82 (void)sprintf(s
, "%d:%02d", MIN(t
), SEC(t
));
84 (void)sprintf(s
, ":%02d", SEC(t
));
93 register int i
, fd
, num_scores
= 0, good
, changed
= 0, found
= 0;
96 char *cp
, *index(), *rindex();
97 SCORE score
[100], thisscore
;
103 fd
= open(_PATH_SCORE
, O_CREAT
|O_RDWR
, 0644);
109 * This is done to take advantage of stdio, while still
110 * allowing a O_CREAT during the open(2) of the log file.
112 fp
= fdopen(fd
, "r+");
118 if (flock(fileno(fp
), LOCK_EX
) < 0)
121 while (lockf(fileno(fp
), F_LOCK
, 1) < 0)
128 good
= fscanf(fp
, "%s %s %s %d %d %d",
129 score
[num_scores
].name
,
130 score
[num_scores
].host
,
131 score
[num_scores
].game
,
132 &score
[num_scores
].planes
,
133 &score
[num_scores
].time
,
134 &score
[num_scores
].real_time
);
135 if (good
!= 6 || ++num_scores
>= NUM_SCORES
)
138 if (!test_mode
&& !list_em
) {
139 if ((pw
= (struct passwd
*) getpwuid(getuid())) == NULL
) {
141 "getpwuid failed for uid %d. Who are you?\n",
145 strcpy(thisscore
.name
, pw
->pw_name
);
147 if (gethostname(thisscore
.host
, sizeof (thisscore
.host
)) < 0) {
148 perror("gethostname");
154 strcpy(thisscore
.host
, name
.sysname
);
157 cp
= rindex(file
, '/');
159 fprintf(stderr
, "log: where's the '/' in %s?\n", file
);
163 strcpy(thisscore
.game
, cp
);
165 thisscore
.time
= clck
;
166 thisscore
.planes
= safe_planes
;
167 thisscore
.real_time
= time(0) - start_time
;
169 for (i
= 0; i
< num_scores
; i
++) {
170 if (strcmp(thisscore
.name
, score
[i
].name
) == 0 &&
171 strcmp(thisscore
.host
, score
[i
].host
) == 0 &&
172 strcmp(thisscore
.game
, score
[i
].game
) == 0) {
173 if (thisscore
.time
> score
[i
].time
) {
174 score
[i
].time
= thisscore
.time
;
175 score
[i
].planes
= thisscore
.planes
;
185 for (i
= 0; i
< num_scores
; i
++) {
186 if (thisscore
.time
> score
[i
].time
) {
187 if (num_scores
< NUM_SCORES
)
190 &score
[num_scores
- 1],
192 bcopy(&thisscore
, &score
[i
],
199 if (!found
&& !changed
&& num_scores
< NUM_SCORES
) {
200 bcopy(&thisscore
, &score
[num_scores
],
201 sizeof (score
[num_scores
]));
208 puts("You beat your previous score!");
210 puts("You made the top players list!");
211 qsort(score
, num_scores
, sizeof (*score
), compar
);
213 for (i
= 0; i
< num_scores
; i
++)
214 fprintf(fp
, "%s %s %s %d %d %d\n",
215 score
[i
].name
, score
[i
].host
,
216 score
[i
].game
, score
[i
].planes
,
217 score
[i
].time
, score
[i
].real_time
);
220 puts("You didn't beat your previous score.");
222 puts("You didn't make the top players list.");
227 flock(fileno(fp
), LOCK_UN
);
230 /* lock will evaporate upon close */
233 printf("%2s: %-8s %-8s %-18s %4s %9s %4s\n", "#", "name", "host",
234 "game", "time", "real time", "planes safe");
235 puts("-------------------------------------------------------------------------------");
236 for (i
= 0; i
< num_scores
; i
++) {
237 cp
= index(score
[i
].host
, '.');
240 printf("%2d: %-8s %-8s %-18s %4d %9s %4d\n", i
+ 1,
241 score
[i
].name
, score
[i
].host
, score
[i
].game
,
242 score
[i
].time
, timestr(score
[i
].real_time
),