]> git.cameronkatri.com Git - bsdgames-darwin.git/blob - robots/score.c
WARNSify
[bsdgames-darwin.git] / robots / score.c
1 /* $NetBSD: score.c,v 1.4 1997/10/12 14:10:05 lukem 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. All advertising materials mentioning features or use of this software
16 * must display the following acknowledgement:
17 * This product includes software developed by the University of
18 * California, Berkeley and its contributors.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
22 *
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
34 */
35
36 #include <sys/cdefs.h>
37 #ifndef lint
38 #if 0
39 static char sccsid[] = "@(#)score.c 8.1 (Berkeley) 5/31/93";
40 #else
41 __RCSID("$NetBSD: score.c,v 1.4 1997/10/12 14:10:05 lukem Exp $");
42 #endif
43 #endif /* not lint */
44
45 # include "robots.h"
46 # include "pathnames.h"
47
48 char *Scorefile = _PATH_SCORE;
49
50 int Max_per_uid = MAX_PER_UID;
51
52 static SCORE Top[MAXSCORES];
53
54 /*
55 * score:
56 * Post the player's score, if reasonable, and then print out the
57 * top list.
58 */
59 void
60 score()
61 {
62 int inf;
63 SCORE *scp;
64 int uid;
65 bool done_show = FALSE;
66 static int numscores, max_uid;
67
68 Newscore = FALSE;
69 if ((inf = open(Scorefile, 2)) < 0) {
70 perror(Scorefile);
71 return;
72 }
73
74 if (read(inf, &max_uid, sizeof max_uid) == sizeof max_uid)
75 read(inf, Top, sizeof Top);
76 else {
77 for (scp = Top; scp < &Top[MAXSCORES]; scp++)
78 scp->s_score = -1;
79 max_uid = Max_per_uid;
80 }
81
82 uid = getuid();
83 if (Top[MAXSCORES-1].s_score <= Score) {
84 numscores = 0;
85 for (scp = Top; scp < &Top[MAXSCORES]; scp++)
86 if (scp->s_score < 0 ||
87 (scp->s_uid == uid && ++numscores == max_uid)) {
88 if (scp->s_score > Score)
89 break;
90 scp->s_score = Score;
91 scp->s_uid = uid;
92 set_name(scp);
93 Newscore = TRUE;
94 break;
95 }
96 if (scp == &Top[MAXSCORES]) {
97 Top[MAXSCORES-1].s_score = Score;
98 Top[MAXSCORES-1].s_uid = uid;
99 set_name(&Top[MAXSCORES-1]);
100 Newscore = TRUE;
101 }
102 if (Newscore)
103 qsort(Top, MAXSCORES, sizeof Top[0], cmp_sc);
104 }
105
106 if (!Newscore) {
107 Full_clear = FALSE;
108 close(inf);
109 return;
110 }
111 else
112 Full_clear = TRUE;
113
114 for (scp = Top; scp < &Top[MAXSCORES]; scp++) {
115 if (scp->s_score < 0)
116 break;
117 move((scp - Top) + 1, 15);
118 if (!done_show && scp->s_uid == uid && scp->s_score == Score)
119 standout();
120 printw(" %d\t%d\t%-8.8s ", (scp - Top) + 1, scp->s_score, scp->s_name);
121 if (!done_show && scp->s_uid == uid && scp->s_score == Score) {
122 standend();
123 done_show = TRUE;
124 }
125 }
126 Num_scores = scp - Top;
127 refresh();
128
129 if (Newscore) {
130 lseek(inf, 0L, 0);
131 write(inf, &max_uid, sizeof max_uid);
132 write(inf, Top, sizeof Top);
133 }
134 close(inf);
135 }
136
137 void
138 set_name(scp)
139 SCORE *scp;
140 {
141 PASSWD *pp;
142
143 if ((pp = getpwuid(scp->s_uid)) == NULL)
144 pp->pw_name = "???";
145 strncpy(scp->s_name, pp->pw_name, MAXNAME);
146 }
147
148 /*
149 * cmp_sc:
150 * Compare two scores.
151 */
152 int
153 cmp_sc(s1, s2)
154 const void *s1, *s2;
155 {
156 return ((SCORE *)s2)->s_score - ((SCORE *)s1)->s_score;
157 }
158
159 /*
160 * show_score:
161 * Show the score list for the '-s' option.
162 */
163 void
164 show_score()
165 {
166 SCORE *scp;
167 int inf;
168 static int max_score;
169
170 if ((inf = open(Scorefile, 0)) < 0) {
171 perror(Scorefile);
172 return;
173 }
174
175 for (scp = Top; scp < &Top[MAXSCORES]; scp++)
176 scp->s_score = -1;
177
178 read(inf, &max_score, sizeof max_score);
179 read(inf, Top, sizeof Top);
180 close(inf);
181 inf = 1;
182 for (scp = Top; scp < &Top[MAXSCORES]; scp++)
183 if (scp->s_score >= 0)
184 printf("%d\t%d\t%.*s\n", inf++, scp->s_score,
185 (int)(sizeof(scp->s_name)), scp->s_name);
186 }