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