usched: Allow process to change self cpu affinity
[dragonfly.git] / games / atc / log.c
blob645d043cf073c40e3e81b724507cbfda4af64c19
1 /*-
2 * Copyright (c) 1990, 1993
3 * The Regents of the University of California. All rights reserved.
5 * This code is derived from software contributed to Berkeley by
6 * Ed James.
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. Neither the name of the University nor the names of its contributors
17 * may be used to endorse or promote products derived from this software
18 * without specific prior written permission.
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.
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 $
37 * Copyright (c) 1987 by Ed James, UC Berkeley. All rights reserved.
39 * Copy permission is hereby granted provided that this notice is
40 * retained on all partial or complete copies.
42 * For more info on this and all of my stuff, mail edjames@berkeley.edu.
45 #include <sys/stat.h>
46 #include <err.h>
48 #include "include.h"
49 #include "pathnames.h"
51 #include <sys/utsname.h>
53 static FILE *score_fp;
55 static int compar(const void *, const void *);
56 static const char *timestr(int);
59 static int
60 compar(const void *va, const void *vb)
62 const SCORE *a, *b;
64 a = (const SCORE *)va;
65 b = (const SCORE *)vb;
66 if (b->planes == a->planes)
67 return (b->time - a->time);
68 else
69 return (b->planes - a->planes);
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)
82 static const char *
83 timestr(int t)
85 static char s[80];
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';
98 return (s);
101 void
102 open_score_file(void)
104 mode_t old_mask;
105 int score_fd;
106 int flags;
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;
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");
124 * This is done to take advantage of stdio, while still
125 * allowing a O_CREAT during the open(2) of the log file.
127 score_fp = fdopen(score_fd, "r+");
128 if (score_fp == NULL) {
129 warn("fdopen %s", _PATH_SCORE);
130 return;
135 log_score(int list_em)
137 int i, num_scores = 0, good, changed = 0, found = 0;
138 struct passwd *pw;
139 char *cp;
140 SCORE score[100], thisscore;
141 struct utsname xname;
143 if (score_fp == NULL) {
144 warnx("no score file available");
145 return (-1);
148 if (flock(fileno(score_fp), LOCK_EX) < 0)
150 warn("flock %s", _PATH_SCORE);
151 return (-1);
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;
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);
171 strcpy(thisscore.name, pw->pw_name);
173 uname(&xname);
174 strcpy(thisscore.host, xname.nodename);
176 cp = rindex(filename, '/');
177 if (cp == NULL) {
178 fprintf(stderr, "log: where's the '/' in %s?\n", filename);
179 return (-1);
181 cp++;
182 strcpy(thisscore.game, cp);
184 thisscore.time = clck;
185 thisscore.planes = safe_planes;
186 thisscore.real_time = time(0) - start_time;
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++;
199 found++;
200 break;
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;
218 if (!found && !changed && num_scores < NUM_SCORES) {
219 bcopy(&thisscore, &score[num_scores],
220 sizeof (score[num_scores]));
221 num_scores++;
222 changed++;
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.");
252 putchar('\n');
254 flock(fileno(score_fp), LOCK_UN);
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);
268 putchar('\n');
269 return (0);
272 void
273 log_score_quit(__unused int sig)
275 log_score(0);
276 exit(0);