gdb - Local mods (compile)
[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.
6693db17 16 * 3. Neither the name of the University nor the names of its contributors
984263bc
MD
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
19 *
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
30 * SUCH DAMAGE.
1de703da
MD
31 *
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 $
984263bc
MD
34 */
35
36/*
37 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
38 *
39 * Copy permission is hereby granted provided that this notice is
40 * retained on all partial or complete copies.
41 *
42 * For more info on this and all of my stuff, mail edjames@berkeley.edu.
43 */
44
2e8d32e0
PA
45#include <sys/stat.h>
46#include <err.h>
47
984263bc
MD
48#include "include.h"
49#include "pathnames.h"
50
984263bc 51#include <sys/utsname.h>
984263bc
MD
52
53static FILE *score_fp;
54
2e8d32e0
PA
55static int compar(const void *, const void *);
56static const char *timestr(int);
57
58
59static int
60compar(const void *va, const void *vb)
984263bc
MD
61{
62 const SCORE *a, *b;
63
64 a = (const SCORE *)va;
65 b = (const SCORE *)vb;
984263bc
MD
66 if (b->planes == a->planes)
67 return (b->time - a->time);
68 else
69 return (b->planes - a->planes);
70}
71
72#define SECAMIN 60
73#define MINAHOUR 60
74#define HOURADAY 24
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)
81
2e8d32e0
PA
82static const char *
83timestr(int t)
984263bc
MD
84{
85 static char s[80];
86
87 if (DAY(t) > 0)
88 (void)sprintf(s, "%dd+%02dhrs", DAY(t), HOUR(t));
89 else if (HOUR(t) > 0)
90 (void)sprintf(s, "%d:%02d:%02d", HOUR(t), MIN(t), SEC(t));
91 else if (MIN(t) > 0)
92 (void)sprintf(s, "%d:%02d", MIN(t), SEC(t));
93 else if (SEC(t) > 0)
94 (void)sprintf(s, ":%02d", SEC(t));
95 else
96 *s = '\0';
97
98 return (s);
99}
100
101void
2e8d32e0 102open_score_file(void)
984263bc
MD
103{
104 mode_t old_mask;
105 int score_fd;
106 int flags;
107
108 old_mask = umask(0);
109 score_fd = open(_PATH_SCORE, O_CREAT|O_RDWR, 0664);
110 umask(old_mask);
111 if (score_fd < 0) {
112 warn("open %s", _PATH_SCORE);
113 return;
114 }
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);
118 if (flags < 0)
119 err(1, "fcntl F_GETFD");
120 flags |= FD_CLOEXEC;
121 if (fcntl(score_fd, F_SETFD, flags) == -1)
122 err(1, "fcntl F_SETFD");
123 /*
124 * This is done to take advantage of stdio, while still
125 * allowing a O_CREAT during the open(2) of the log file.
126 */
127 score_fp = fdopen(score_fd, "r+");
128 if (score_fp == NULL) {
129 warn("fdopen %s", _PATH_SCORE);
130 return;
131 }
132}
133
134int
2e8d32e0 135log_score(int list_em)
984263bc
MD
136{
137 int i, num_scores = 0, good, changed = 0, found = 0;
138 struct passwd *pw;
139 char *cp;
140 SCORE score[100], thisscore;
5f206b34 141 struct utsname xname;
984263bc
MD
142
143 if (score_fp == NULL) {
144 warnx("no score file available");
145 return (-1);
146 }
147
984263bc 148 if (flock(fileno(score_fp), LOCK_EX) < 0)
984263bc
MD
149 {
150 warn("flock %s", _PATH_SCORE);
151 return (-1);
152 }
153 for (;;) {
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)
162 break;
163 }
164 if (!test_mode && !list_em) {
165 if ((pw = (struct passwd *) getpwuid(getuid())) == NULL) {
166 fprintf(stderr,
167 "getpwuid failed for uid %d. Who are you?\n",
168 (int)getuid());
169 return (-1);
170 }
171 strcpy(thisscore.name, pw->pw_name);
5f206b34
MD
172
173 uname(&xname);
174 strcpy(thisscore.host, xname.nodename);
984263bc 175
2e8d32e0 176 cp = rindex(filename, '/');
984263bc 177 if (cp == NULL) {
2e8d32e0 178 fprintf(stderr, "log: where's the '/' in %s?\n", filename);
984263bc
MD
179 return (-1);
180 }
181 cp++;
182 strcpy(thisscore.game, cp);
183
184 thisscore.time = clck;
185 thisscore.planes = safe_planes;
186 thisscore.real_time = time(0) - start_time;
187
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;
195 score[i].real_time =
196 thisscore.real_time;
197 changed++;
198 }
199 found++;
200 break;
201 }
202 }
203 if (!found) {
204 for (i = 0; i < num_scores; i++) {
205 if (thisscore.time > score[i].time) {
206 if (num_scores < NUM_SCORES)
207 num_scores++;
208 bcopy(&score[i],
209 &score[num_scores - 1],
210 sizeof (score[i]));
211 bcopy(&thisscore, &score[i],
212 sizeof (score[i]));
213 changed++;
214 break;
215 }
216 }
217 }
218 if (!found && !changed && num_scores < NUM_SCORES) {
219 bcopy(&thisscore, &score[num_scores],
220 sizeof (score[num_scores]));
221 num_scores++;
222 changed++;
223 }
224
225 if (changed) {
226 if (found)
227 puts("You beat your previous score!");
228 else
229 puts("You made the top players list!");
230 qsort(score, num_scores, sizeof (*score), compar);
231 rewind(score_fp);
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);
237 fflush(score_fp);
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));
246 } else {
247 if (found)
248 puts("You didn't beat your previous score.");
249 else
250 puts("You didn't make the top players list.");
251 }
252 putchar('\n');
253 }
984263bc 254 flock(fileno(score_fp), LOCK_UN);
984263bc
MD
255 fclose(score_fp);
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, '.');
261 if (cp != NULL)
262 *cp = '\0';
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),
266 score[i].planes);
267 }
268 putchar('\n');
269 return (0);
270}
a8718c14
SW
271
272void
273log_score_quit(__unused int sig)
274{
275 log_score(0);
276 exit(0);
277}