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