]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - atc/log.c
curses needs termcap
[bsdgames-darwin.git] / atc / log.c
1 /* $NetBSD: log.c,v 1.4 1997/01/13 06:50:26 tls Exp $ */
2
3 /*-
4 * Copyright (c) 1990, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * This code is derived from software contributed to Berkeley by
8 * Ed James.
9 *
10 * Redistribution and use in source and binary forms, with or without
11 * modification, are permitted provided that the following conditions
12 * are met:
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.
25 *
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
36 * SUCH DAMAGE.
37 */
38
39 /*
40 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
41 *
42 * Copy permission is hereby granted provided that this notice is
43 * retained on all partial or complete copies.
44 *
45 * For more info on this and all of my stuff, mail edjames@berkeley.edu.
46 */
47
48 #ifndef lint
49 #if 0
50 static char sccsid[] = "@(#)log.c 8.1 (Berkeley) 5/31/93";
51 #else
52 static char rcsid[] = "$NetBSD: log.c,v 1.4 1997/01/13 06:50:26 tls Exp $";
53 #endif
54 #endif not lint
55
56 #include "include.h"
57 #include "pathnames.h"
58
59 compar(a, b)
60 SCORE *a, *b;
61 {
62 if (b->planes == a->planes)
63 return (b->time - a->time);
64 else
65 return (b->planes - a->planes);
66 }
67
68 #define SECAMIN 60
69 #define MINAHOUR 60
70 #define HOURADAY 24
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)
77
78 char *
79 timestr(t)
80 {
81 static char s[80];
82
83 if (DAY(t) > 0)
84 (void)sprintf(s, "%dd+%02dhrs", DAY(t), HOUR(t));
85 else if (HOUR(t) > 0)
86 (void)sprintf(s, "%d:%02d:%02d", HOUR(t), MIN(t), SEC(t));
87 else if (MIN(t) > 0)
88 (void)sprintf(s, "%d:%02d", MIN(t), SEC(t));
89 else if (SEC(t) > 0)
90 (void)sprintf(s, ":%02d", SEC(t));
91 else
92 *s = '\0';
93
94 return (s);
95 }
96
97 log_score(list_em)
98 {
99 register int i, fd, num_scores = 0, good, changed = 0, found = 0;
100 struct passwd *pw;
101 FILE *fp;
102 char *cp, *index(), *rindex();
103 SCORE score[100], thisscore;
104 #ifdef SYSV
105 struct utsname name;
106 #endif
107
108 umask(0);
109 fd = open(_PATH_SCORE, O_CREAT|O_RDWR, 0644);
110 if (fd < 0) {
111 perror(_PATH_SCORE);
112 return (-1);
113 }
114 /*
115 * This is done to take advantage of stdio, while still
116 * allowing a O_CREAT during the open(2) of the log file.
117 */
118 fp = fdopen(fd, "r+");
119 if (fp == NULL) {
120 perror(_PATH_SCORE);
121 return (-1);
122 }
123 #ifdef BSD
124 if (flock(fileno(fp), LOCK_EX) < 0)
125 #endif
126 #ifdef SYSV
127 while (lockf(fileno(fp), F_LOCK, 1) < 0)
128 #endif
129 {
130 perror("flock");
131 return (-1);
132 }
133 for (;;) {
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)
142 break;
143 }
144 if (!test_mode && !list_em) {
145 if ((pw = (struct passwd *) getpwuid(getuid())) == NULL) {
146 fprintf(stderr,
147 "getpwuid failed for uid %d. Who are you?\n",
148 getuid());
149 return (-1);
150 }
151 strcpy(thisscore.name, pw->pw_name);
152 #ifdef BSD
153 if (gethostname(thisscore.host, sizeof (thisscore.host)) < 0) {
154 perror("gethostname");
155 return (-1);
156 }
157 #endif
158 #ifdef SYSV
159 uname(&name);
160 strcpy(thisscore.host, name.sysname);
161 #endif
162
163 cp = rindex(file, '/');
164 if (cp == NULL) {
165 fprintf(stderr, "log: where's the '/' in %s?\n", file);
166 return (-1);
167 }
168 cp++;
169 strcpy(thisscore.game, cp);
170
171 thisscore.time = clck;
172 thisscore.planes = safe_planes;
173 thisscore.real_time = time(0) - start_time;
174
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;
182 score[i].real_time =
183 thisscore.real_time;
184 changed++;
185 }
186 found++;
187 break;
188 }
189 }
190 if (!found) {
191 for (i = 0; i < num_scores; i++) {
192 if (thisscore.time > score[i].time) {
193 if (num_scores < NUM_SCORES)
194 num_scores++;
195 memcpy(&score[num_scores - 1],
196 &score[i],
197 sizeof (score[i]));
198 memcpy(&score[i], &thisscore,
199 sizeof (score[i]));
200 changed++;
201 break;
202 }
203 }
204 }
205 if (!found && !changed && num_scores < NUM_SCORES) {
206 memcpy(&score[num_scores], &thisscore,
207 sizeof (score[num_scores]));
208 num_scores++;
209 changed++;
210 }
211
212 if (changed) {
213 if (found)
214 puts("You beat your previous score!");
215 else
216 puts("You made the top players list!");
217 qsort(score, num_scores, sizeof (*score), compar);
218 rewind(fp);
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);
224 } else {
225 if (found)
226 puts("You didn't beat your previous score.");
227 else
228 puts("You didn't make the top players list.");
229 }
230 putchar('\n');
231 }
232 #ifdef BSD
233 flock(fileno(fp), LOCK_UN);
234 #endif
235 #ifdef SYSV
236 /* lock will evaporate upon close */
237 #endif
238 fclose(fp);
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, '.');
244 if (cp != NULL)
245 *cp = '\0';
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),
249 score[i].planes);
250 }
251 putchar('\n');
252 return (0);
253 }