Add the DragonFly cvs id and perform general cleanups on cvs/rcs/sccs ids. Most
[dragonfly.git] / games / atc / log.c
CommitLineData
984263bc
MD
1/*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
4 *
5 * This code is derived from software contributed to Berkeley by
6 * Ed James.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions
10 * are met:
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.
23 *
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
34 * SUCH DAMAGE.
1de703da
MD
35 *
36 * @(#)log.c 8.1 (Berkeley) 5/31/93
37 * $FreeBSD: src/games/atc/log.c,v 1.7 1999/11/30 03:48:20 billf Exp $
38 * $DragonFly: src/games/atc/log.c,v 1.2 2003/06/17 04:25:22 dillon Exp $
984263bc
MD
39 */
40
41/*
42 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
43 *
44 * Copy permission is hereby granted provided that this notice is
45 * retained on all partial or complete copies.
46 *
47 * For more info on this and all of my stuff, mail edjames@berkeley.edu.
48 */
49
984263bc
MD
50#include <string.h>
51#include "include.h"
52#include "pathnames.h"
53
54#ifdef SYSV
55#include <sys/utsname.h>
56#endif
57
58static FILE *score_fp;
59
60int
61compar(va, vb)
62 const void *va, *vb;
63{
64 const SCORE *a, *b;
65
66 a = (const SCORE *)va;
67 b = (const SCORE *)vb;
68
69 if (b->planes == a->planes)
70 return (b->time - a->time);
71 else
72 return (b->planes - a->planes);
73}
74
75#define SECAMIN 60
76#define MINAHOUR 60
77#define HOURADAY 24
78#define SECAHOUR (SECAMIN * MINAHOUR)
79#define SECADAY (SECAHOUR * HOURADAY)
80#define DAY(t) ((t) / SECADAY)
81#define HOUR(t) (((t) % SECADAY) / SECAHOUR)
82#define MIN(t) (((t) % SECAHOUR) / SECAMIN)
83#define SEC(t) ((t) % SECAMIN)
84
85const char *
86timestr(t)
87{
88 static char s[80];
89
90 if (DAY(t) > 0)
91 (void)sprintf(s, "%dd+%02dhrs", DAY(t), HOUR(t));
92 else if (HOUR(t) > 0)
93 (void)sprintf(s, "%d:%02d:%02d", HOUR(t), MIN(t), SEC(t));
94 else if (MIN(t) > 0)
95 (void)sprintf(s, "%d:%02d", MIN(t), SEC(t));
96 else if (SEC(t) > 0)
97 (void)sprintf(s, ":%02d", SEC(t));
98 else
99 *s = '\0';
100
101 return (s);
102}
103
104void
105open_score_file()
106{
107 mode_t old_mask;
108 int score_fd;
109 int flags;
110
111 old_mask = umask(0);
112 score_fd = open(_PATH_SCORE, O_CREAT|O_RDWR, 0664);
113 umask(old_mask);
114 if (score_fd < 0) {
115 warn("open %s", _PATH_SCORE);
116 return;
117 }
118 /* Set the close-on-exec flag. If this fails for any reason, quit
119 * rather than leave the score file open to tampering. */
120 flags = fcntl(score_fd, F_GETFD);
121 if (flags < 0)
122 err(1, "fcntl F_GETFD");
123 flags |= FD_CLOEXEC;
124 if (fcntl(score_fd, F_SETFD, flags) == -1)
125 err(1, "fcntl F_SETFD");
126 /*
127 * This is done to take advantage of stdio, while still
128 * allowing a O_CREAT during the open(2) of the log file.
129 */
130 score_fp = fdopen(score_fd, "r+");
131 if (score_fp == NULL) {
132 warn("fdopen %s", _PATH_SCORE);
133 return;
134 }
135}
136
137int
138log_score(list_em)
139 int list_em;
140{
141 int i, num_scores = 0, good, changed = 0, found = 0;
142 struct passwd *pw;
143 char *cp;
144 SCORE score[100], thisscore;
145#ifdef SYSV
146 struct utsname name;
147#endif
148
149 if (score_fp == NULL) {
150 warnx("no score file available");
151 return (-1);
152 }
153
154#ifdef BSD
155 if (flock(fileno(score_fp), LOCK_EX) < 0)
156#endif
157#ifdef SYSV
158 while (lockf(fileno(score_fp), F_LOCK, 1) < 0)
159#endif
160 {
161 warn("flock %s", _PATH_SCORE);
162 return (-1);
163 }
164 for (;;) {
165 good = fscanf(score_fp, SCORE_SCANF_FMT,
166 score[num_scores].name,
167 score[num_scores].host,
168 score[num_scores].game,
169 &score[num_scores].planes,
170 &score[num_scores].time,
171 &score[num_scores].real_time);
172 if (good != 6 || ++num_scores >= NUM_SCORES)
173 break;
174 }
175 if (!test_mode && !list_em) {
176 if ((pw = (struct passwd *) getpwuid(getuid())) == NULL) {
177 fprintf(stderr,
178 "getpwuid failed for uid %d. Who are you?\n",
179 (int)getuid());
180 return (-1);
181 }
182 strcpy(thisscore.name, pw->pw_name);
183#ifdef BSD
184 if (gethostname(thisscore.host, sizeof (thisscore.host)) < 0) {
185 perror("gethostname");
186 return (-1);
187 }
188#endif
189#ifdef SYSV
190 uname(&name);
191 strcpy(thisscore.host, name.nodename);
192#endif
193
194 cp = rindex(file, '/');
195 if (cp == NULL) {
196 fprintf(stderr, "log: where's the '/' in %s?\n", file);
197 return (-1);
198 }
199 cp++;
200 strcpy(thisscore.game, cp);
201
202 thisscore.time = clck;
203 thisscore.planes = safe_planes;
204 thisscore.real_time = time(0) - start_time;
205
206 for (i = 0; i < num_scores; i++) {
207 if (strcmp(thisscore.name, score[i].name) == 0 &&
208 strcmp(thisscore.host, score[i].host) == 0 &&
209 strcmp(thisscore.game, score[i].game) == 0) {
210 if (thisscore.time > score[i].time) {
211 score[i].time = thisscore.time;
212 score[i].planes = thisscore.planes;
213 score[i].real_time =
214 thisscore.real_time;
215 changed++;
216 }
217 found++;
218 break;
219 }
220 }
221 if (!found) {
222 for (i = 0; i < num_scores; i++) {
223 if (thisscore.time > score[i].time) {
224 if (num_scores < NUM_SCORES)
225 num_scores++;
226 bcopy(&score[i],
227 &score[num_scores - 1],
228 sizeof (score[i]));
229 bcopy(&thisscore, &score[i],
230 sizeof (score[i]));
231 changed++;
232 break;
233 }
234 }
235 }
236 if (!found && !changed && num_scores < NUM_SCORES) {
237 bcopy(&thisscore, &score[num_scores],
238 sizeof (score[num_scores]));
239 num_scores++;
240 changed++;
241 }
242
243 if (changed) {
244 if (found)
245 puts("You beat your previous score!");
246 else
247 puts("You made the top players list!");
248 qsort(score, num_scores, sizeof (*score), compar);
249 rewind(score_fp);
250 for (i = 0; i < num_scores; i++)
251 fprintf(score_fp, "%s %s %s %d %d %d\n",
252 score[i].name, score[i].host,
253 score[i].game, score[i].planes,
254 score[i].time, score[i].real_time);
255 fflush(score_fp);
256 if (ferror(score_fp))
257 warn("error writing %s", _PATH_SCORE);
258 /* It is just possible that updating an entry could
259 * have reduced the length of the file, so we
260 * truncate it. The lseek is required for stream/fd
261 * synchronisation by POSIX.1. */
262 lseek(fileno(score_fp), 0, SEEK_END);
263 ftruncate(fileno(score_fp), ftell(score_fp));
264 } else {
265 if (found)
266 puts("You didn't beat your previous score.");
267 else
268 puts("You didn't make the top players list.");
269 }
270 putchar('\n');
271 }
272#ifdef BSD
273 flock(fileno(score_fp), LOCK_UN);
274#endif
275#ifdef SYSV
276 /* lock will evaporate upon close */
277#endif
278 fclose(score_fp);
279 printf("%2s: %-8s %-8s %-18s %4s %9s %4s\n", "#", "name", "host",
280 "game", "time", "real time", "planes safe");
281 puts("-------------------------------------------------------------------------------");
282 for (i = 0; i < num_scores; i++) {
283 cp = index(score[i].host, '.');
284 if (cp != NULL)
285 *cp = '\0';
286 printf("%2d: %-8s %-8s %-18s %4d %9s %4d\n", i + 1,
287 score[i].name, score[i].host, score[i].game,
288 score[i].time, timestr(score[i].real_time),
289 score[i].planes);
290 }
291 putchar('\n');
292 return (0);
293}