]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - robots/score.c
don't ignore errors from read(); found by lint
[bsdgames-darwin.git] / robots / score.c
1 /* $NetBSD: score.c,v 1.22 2009/08/03 06:04:12 dholland Exp $ */
2
3 /*
4 * Copyright (c) 1980, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * 2. Redistributions in binary form must reproduce the above copyright
13 * notice, this list of conditions and the following disclaimer in the
14 * documentation and/or other materials provided with the distribution.
15 * 3. Neither the name of the University nor the names of its contributors
16 * may be used to endorse or promote products derived from this software
17 * without specific prior written permission.
18 *
19 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
20 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
21 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
22 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
23 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
32 #include <sys/cdefs.h>
33 #ifndef lint
34 #if 0
35 static char sccsid[] = "@(#)score.c 8.1 (Berkeley) 5/31/93";
36 #else
37 __RCSID("$NetBSD: score.c,v 1.22 2009/08/03 06:04:12 dholland Exp $");
38 #endif
39 #endif /* not lint */
40
41 #include <curses.h>
42 #include <err.h>
43 #include <fcntl.h>
44 #include <pwd.h>
45 #include <stdio.h>
46 #include <stdlib.h>
47 #include <string.h>
48 #include <unistd.h>
49 #include "robots.h"
50 #include "pathnames.h"
51
52 const char *Scorefile = _PATH_SCORE;
53
54 int Max_per_uid = MAX_PER_UID;
55
56 static SCORE Top[MAXSCORES];
57
58 static uint32_t numscores, max_uid;
59
60 /*
61 * read_score:
62 * Read the score file in MI format
63 */
64 static void
65 read_score(int inf)
66 {
67 SCORE *scp;
68
69 if (read(inf, &max_uid, sizeof max_uid) == sizeof max_uid &&
70 read(inf, Top, sizeof Top) == sizeof Top) {
71 max_uid = ntohl(max_uid);
72 for (scp = Top; scp < &Top[MAXSCORES]; scp++) {
73 scp->s_uid = ntohl(scp->s_uid);
74 scp->s_score = ntohl(scp->s_score);
75 scp->s_auto = ntohl(scp->s_auto);
76 scp->s_level = ntohl(scp->s_level);
77 }
78 }
79 else {
80 for (scp = Top; scp < &Top[MAXSCORES]; scp++)
81 scp->s_score = 0;
82 max_uid = Max_per_uid;
83 }
84 }
85
86 /*
87 * write_score:
88 * Write the score file in MI format
89 */
90 static void
91 write_score(int inf)
92 {
93 SCORE *scp;
94
95 lseek(inf, 0L, SEEK_SET);
96
97 max_uid = htonl(max_uid);
98 write(inf, &max_uid, sizeof max_uid);
99
100 for (scp = Top; scp < &Top[MAXSCORES]; scp++) {
101 scp->s_uid = htonl(scp->s_uid);
102 scp->s_score = htonl(scp->s_score);
103 scp->s_auto = htonl(scp->s_auto);
104 scp->s_level = htonl(scp->s_level);
105 }
106
107 write(inf, Top, sizeof Top);
108 }
109
110
111 /*
112 * score:
113 * Post the player's score, if reasonable, and then print out the
114 * top list.
115 */
116 void
117 score(int score_wfd)
118 {
119 int inf = score_wfd;
120 SCORE *scp;
121 uint32_t uid;
122 bool done_show = false;
123
124 Newscore = false;
125 if (inf < 0)
126 return;
127
128 read_score(inf);
129
130 uid = getuid();
131 if (Top[MAXSCORES-1].s_score <= Score) {
132 numscores = 0;
133 for (scp = Top; scp < &Top[MAXSCORES]; scp++)
134 if ((scp->s_uid == uid && ++numscores == max_uid)) {
135 if (scp->s_score > Score)
136 break;
137 scp->s_score = Score;
138 scp->s_uid = uid;
139 scp->s_auto = Auto_bot;
140 scp->s_level = Level;
141 set_name(scp);
142 Newscore = true;
143 break;
144 }
145 if (scp == &Top[MAXSCORES]) {
146 Top[MAXSCORES-1].s_score = Score;
147 Top[MAXSCORES-1].s_uid = uid;
148 Top[MAXSCORES-1].s_auto = Auto_bot;
149 Top[MAXSCORES-1].s_level = Level;
150 set_name(&Top[MAXSCORES-1]);
151 Newscore = true;
152 }
153 if (Newscore)
154 qsort(Top, MAXSCORES, sizeof Top[0], cmp_sc);
155 }
156
157 if (!Newscore) {
158 Full_clear = false;
159 lseek(inf, 0, SEEK_SET);
160 return;
161 }
162 else
163 Full_clear = true;
164
165 move(1, 15);
166 printw("%5.5s %5.5s %-9.9s %-8.8s %5.5s", "Rank", "Score", "User",
167 " ", "Level");
168
169 for (scp = Top; scp < &Top[MAXSCORES]; scp++) {
170 if (scp->s_score == 0)
171 break;
172 move((scp - Top) + 2, 15);
173 if (!done_show && scp->s_uid == uid && scp->s_score == Score)
174 standout();
175 printw("%5ld %5d %-8.8s %-9.9s %5d",
176 (long)(scp - Top) + 1, scp->s_score, scp->s_name,
177 scp->s_auto ? "(autobot)" : "", scp->s_level);
178 if (!done_show && scp->s_uid == uid && scp->s_score == Score) {
179 standend();
180 done_show = true;
181 }
182 }
183 Num_scores = scp - Top;
184 refresh();
185
186 if (Newscore) {
187 write_score(inf);
188 }
189 lseek(inf, 0, SEEK_SET);
190 }
191
192 void
193 set_name(SCORE *scp)
194 {
195 struct passwd *pp;
196
197 if ((pp = getpwuid(scp->s_uid)) == NULL)
198 strncpy(scp->s_name, "???", MAXNAME);
199 else
200 strncpy(scp->s_name, pp->pw_name, MAXNAME);
201 }
202
203 /*
204 * cmp_sc:
205 * Compare two scores.
206 */
207 int
208 cmp_sc(const void *s1, const void *s2)
209 {
210 return ((const SCORE *)s2)->s_score - ((const SCORE *)s1)->s_score;
211 }
212
213 /*
214 * show_score:
215 * Show the score list for the '-s' option.
216 */
217 void
218 show_score(void)
219 {
220 SCORE *scp;
221 int inf;
222
223 if ((inf = open(Scorefile, O_RDONLY)) < 0) {
224 warn("opening `%s'", Scorefile);
225 return;
226 }
227
228 read_score(inf);
229 close(inf);
230 inf = 1;
231 printf("%5.5s %5.5s %-9.9s %-8.8s %5.5s\n", "Rank", "Score", "User",
232 " ", "Level");
233 for (scp = Top; scp < &Top[MAXSCORES]; scp++)
234 if (scp->s_score > 0)
235 printf("%5d %5d %-8.8s %-9.9s %5d\n",
236 inf++, scp->s_score, scp->s_name,
237 scp->s_auto ? "(autobot)" : "", scp->s_level);
238 }