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