|
|
1.1 root 1: /*
2: * Copyright (c) 1980 Regents of the University of California.
3: * All rights reserved.
4: *
5: * Redistribution and use in source and binary forms are permitted
6: * provided that the above copyright notice and this paragraph are
7: * duplicated in all such forms and that any documentation,
8: * advertising materials, and other materials related to such
9: * distribution and use acknowledge that the software was developed
10: * by the University of California, Berkeley. The name of the
11: * University may not be used to endorse or promote products derived
12: * from this software without specific prior written permission.
13: * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
14: * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
15: * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
16: */
17:
18: #ifndef lint
19: static char sccsid[] = "@(#)score.c 5.3 (Berkeley) 6/18/88";
20: #endif /* not lint */
21:
22: # include "robots.h"
23: # include <pwd.h>
24:
25: typedef struct {
26: int s_uid;
27: int s_score;
28: char s_name[MAXNAME];
29: } SCORE;
30:
31: typedef struct passwd PASSWD;
32:
33: char *Scorefile = SCOREFILE;
34:
35: int Max_per_uid = MAX_PER_UID;
36:
37: static SCORE Top[MAXSCORES];
38:
39: /*
40: * score:
41: * Post the player's score, if reasonable, and then print out the
42: * top list.
43: */
44: score()
45: {
46: register int inf;
47: register SCORE *scp;
48: register int uid;
49: register bool done_show = FALSE;
50: static int numscores, max_uid;
51:
52: Newscore = FALSE;
53: if ((inf = open(Scorefile, 2)) < 0) {
54: perror(Scorefile);
55: return;
56: }
57:
58: if (read(inf, &max_uid, sizeof max_uid) == sizeof max_uid)
59: read(inf, Top, sizeof Top);
60: else {
61: for (scp = Top; scp < &Top[MAXSCORES]; scp++)
62: scp->s_score = -1;
63: max_uid = Max_per_uid;
64: }
65:
66: uid = getuid();
67: if (Top[MAXSCORES-1].s_score <= Score) {
68: numscores = 0;
69: for (scp = Top; scp < &Top[MAXSCORES]; scp++)
70: if (scp->s_score < 0 ||
71: (scp->s_uid == uid && ++numscores == max_uid)) {
72: if (scp->s_score > Score)
73: break;
74: scp->s_score = Score;
75: scp->s_uid = uid;
76: set_name(scp);
77: Newscore = TRUE;
78: break;
79: }
80: if (scp == &Top[MAXSCORES]) {
81: Top[MAXSCORES-1].s_score = Score;
82: Top[MAXSCORES-1].s_uid = uid;
83: set_name(&Top[MAXSCORES-1]);
84: Newscore = TRUE;
85: }
86: if (Newscore)
87: qsort(Top, MAXSCORES, sizeof Top[0], cmp_sc);
88: }
89:
90: if (!Newscore) {
91: Full_clear = FALSE;
92: close(inf);
93: return;
94: }
95: else
96: Full_clear = TRUE;
97:
98: for (scp = Top; scp < &Top[MAXSCORES]; scp++) {
99: if (scp->s_score < 0)
100: break;
101: move((scp - Top) + 1, 15);
102: if (!done_show && scp->s_uid == uid && scp->s_score == Score)
103: standout();
104: printw(" %d\t%d\t%-8.8s ", (scp - Top) + 1, scp->s_score, scp->s_name);
105: if (!done_show && scp->s_uid == uid && scp->s_score == Score) {
106: standend();
107: done_show = TRUE;
108: }
109: }
110: Num_scores = scp - Top;
111: refresh();
112:
113: if (Newscore) {
114: lseek(inf, 0L, 0);
115: write(inf, &max_uid, sizeof max_uid);
116: write(inf, Top, sizeof Top);
117: }
118: close(inf);
119: }
120:
121: set_name(scp)
122: register SCORE *scp;
123: {
124: register PASSWD *pp;
125:
126: if ((pp = getpwuid(scp->s_uid)) == NULL)
127: pp->pw_name = "???";
128: strncpy(scp->s_name, pp->pw_name, MAXNAME);
129: }
130:
131: /*
132: * cmp_sc:
133: * Compare two scores.
134: */
135: cmp_sc(s1, s2)
136: register SCORE *s1, *s2;
137: {
138: return s2->s_score - s1->s_score;
139: }
140:
141: /*
142: * show_score:
143: * Show the score list for the '-s' option.
144: */
145: show_score()
146: {
147: register SCORE *scp;
148: register int inf;
149: static int max_score;
150:
151: if ((inf = open(Scorefile, 0)) < 0) {
152: perror(Scorefile);
153: return;
154: }
155:
156: for (scp = Top; scp < &Top[MAXSCORES]; scp++)
157: scp->s_score = -1;
158:
159: read(inf, &max_score, sizeof max_score);
160: read(inf, Top, sizeof Top);
161: close(inf);
162: inf = 1;
163: for (scp = Top; scp < &Top[MAXSCORES]; scp++)
164: if (scp->s_score >= 0)
165: printf("%d\t%d\t%.*s\n", inf++, scp->s_score, sizeof scp->s_name, scp->s_name);
166: }
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.