Neale Pickett
·
2011-10-13
netmisc.c
1/*
2 * copies of several routines needed for netwait
3 *
4 * @(#)netmisc.c 4.7 (Berkeley) 02/05/99
5 */
6
7# include <stdio.h>
8# include <sys/types.h>
9# include <stat.h>
10# include <ctype.h>
11
12# define TRUE 1
13# define FALSE 0
14# define MAXSTR 80
15# define bool char
16# define when break;case
17# define otherwise break;default
18
19typedef struct {
20 char *m_name;
21} MONST;
22
23char *s_vowelstr();
24
25extern char encstr[], frob;
26
27char *lockfile = "/tmp/.fredlock";
28
29char Prbuf[MAXSTR]; /* Buffer for sprintfs */
30
31MONST Monsters[] = {
32 { "aquator" }, { "bat" }, { "centaur" }, { "dragon" }, { "emu" },
33 { "venus flytrap" }, { "griffin" }, { "hobgoblin" }, { "ice monster" },
34 { "jabberwock" }, { "kobold" }, { "leprechaun" }, { "medusa" },
35 { "nymph" }, { "orc" }, { "phantom" }, { "quasit" }, { "rattlesnake" },
36 { "snake" }, { "troll" }, { "ur-vile" }, { "vampire" }, { "wraith" },
37 { "xeroc" }, { "yeti" }, { "zombie" }
38};
39
40/*
41 * s_lock_sc:
42 * lock the score file. If it takes too long, ask the user if
43 * they care to wait. Return TRUE if the lock is successful.
44 */
45bool
46s_lock_sc(void)
47{
48 int cnt;
49 static struct stat sbuf;
50 time_t time();
51
52over:
53 close(8); /* just in case there are no files left */
54 if (creat(lockfile, 0000) >= 0)
55 return TRUE;
56 for (cnt = 0; cnt < 5; cnt++)
57 {
58 sleep(1);
59 if (creat(lockfile, 0000) >= 0)
60 return TRUE;
61 }
62 if (stat(lockfile, &sbuf) < 0)
63 {
64 creat(lockfile, 0000);
65 return TRUE;
66 }
67 if (time(NULL) - sbuf.st_mtime > 10)
68 {
69 if (unlink(lockfile) < 0)
70 return FALSE;
71 goto over;
72 }
73 else
74 {
75 printf("The score file is very busy. Do you want to wait longer\n");
76 printf("for it to become free so your score can get posted?\n");
77 printf("If so, type \"y\"\n");
78 fgets(Prbuf, MAXSTR, stdin);
79 if (Prbuf[0] == 'y')
80 for (;;)
81 {
82 if (creat(lockfile, 0000) >= 0)
83 return TRUE;
84 if (stat(lockfile, &sbuf) < 0)
85 {
86 creat(lockfile, 0000);
87 return TRUE;
88 }
89 if (time(NULL) - sbuf.st_mtime > 10)
90 {
91 if (unlink(lockfile) < 0)
92 return FALSE;
93 }
94 sleep(1);
95 }
96 else
97 return FALSE;
98 }
99}
100
101/*
102 * s_unlock_sc:
103 * Unlock the score file
104 */
105void
106s_unlock_sc(void)
107{
108 unlink(lockfile);
109}
110
111/*
112 * s_encwrite:
113 * Perform an encrypted write
114 */
115void
116s_encwrite(char *start, unsigned int size, FILE *outf)
117{
118 char *e1, *e2, fb;
119 int temp;
120 extern char statlist[];
121
122 e1 = encstr;
123 e2 = statlist;
124 fb = frob;
125
126 while (size--)
127 {
128 putc(*start++ ^ *e1 ^ *e2 ^ fb, outf);
129 temp = *e1++;
130 fb += temp * *e2++;
131 if (*e1 == '\0')
132 e1 = encstr;
133 if (*e2 == '\0')
134 e2 = statlist;
135 }
136}
137
138/*
139 * s_encread:
140 * Perform an encrypted read
141 */
142void
143s_encread(char *start, unsigned int size, int inf)
144{
145 char *e1, *e2, fb;
146 int temp;
147 int read_size;
148 extern char statlist[];
149
150 fb = frob;
151
152 if ((read_size = read(inf, start, size)) == 0 || read_size == -1)
153 return;
154
155 e1 = encstr;
156 e2 = statlist;
157
158 while (size--)
159 {
160 *start++ ^= *e1 ^ *e2 ^ fb;
161 temp = *e1++;
162 fb += temp * *e2++;
163 if (*e1 == '\0')
164 e1 = encstr;
165 if (*e2 == '\0')
166 e2 = statlist;
167 }
168}
169
170/*
171 * s_killname:
172 * Convert a code to a monster name
173 */
174char *
175s_killname(char monst, bool doart)
176{
177 char *sp;
178 bool article;
179
180 article = TRUE;
181 switch (monst)
182 {
183 when 'a':
184 sp = "arrow";
185 when 'b':
186 sp = "bolt";
187 when 'd':
188 sp = "dart";
189 when 's':
190 sp = "starvation";
191 article = FALSE;
192 when 'h':
193 sp = "hypothermia";
194 article = FALSE;
195 otherwise:
196 if (isupper(monst))
197 sp = Monsters[monst-'A'].m_name;
198 else
199 {
200 sp = "God";
201 article = FALSE;
202 }
203 }
204 if (doart && article)
205 sprintf(Prbuf, "a%s ", s_vowelstr(sp));
206 else
207 Prbuf[0] = '\0';
208 strcat(Prbuf, sp);
209 return Prbuf;
210}
211
212/*
213 * s_vowelstr:
214 * For printfs: if string starts with a vowel, return "n" for an
215 * "an".
216 */
217char *
218s_vowelstr(char *str)
219{
220 switch (*str)
221 {
222 case 'a': case 'A':
223 case 'e': case 'E':
224 case 'i': case 'I':
225 case 'o': case 'O':
226 case 'u': case 'U':
227 return "n";
228 default:
229 return "";
230 }
231}