-
Notifications
You must be signed in to change notification settings - Fork 4
/
scores.c
346 lines (315 loc) · 8.28 KB
/
scores.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
/* file scores.c */
#include "wand_head.h"
#include <err.h>
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/stat.h>
#ifdef MSDOS /* M001 */
#define LOCK
#define UNLOCK
#else
/*#define LOCK flock( fp->_cnt , LOCK_EX ) // peeked at stdio.h ! */
/*#define UNLOCK flock( fp->_cnt , LOCK_UN ) */
/* #define LOCK while((lock = creat(LOCKFILE,0))==-1) Not a good way */
#define LOCK if((lock = open(LOCKFILE, O_CREAT | O_TRUNC | O_WRONLY, "r"))==-1){errx (1,"%s","Lockfile creation failed\n"); exit(1);} /* Marina */
#define UNLOCK unlink(LOCKFILE)
#endif
#ifdef MSDOS /* M001 */
#define getuid() 0
#endif
#ifdef COMPARE_BY_NAME
#define SAMEUSER(p) (strcmp((p)->name, name) == 0)
#else
#define SAMEUSER(p) ((p)->uid == user_id)
#endif
extern int saved_game; /* prevent recording of hiscore if */
/* NO_RESTORED_GAME_HISCORES is #def'd */
/***********************************
* score_entry structure *
************************************/
typedef struct
{
char howdead[25];
char name[20];
long score;
int level;
int uid;
} score_entry;
#ifdef LINT_ARGS /* M001 */
void show_scores(score_entry *, int);
int readtable(score_entry *);
#else
void show_scores();
int readtable();
#endif
/**/
/************************
* show_scores *
*************************/
void show_scores(score_entry * table, int num)
{
int tot = num;
printf
("\nNo. Score Level Names How they died\n");
printf
("=============================================================================\n");
while (num > 0)
{
num--;
printf("%2d %5ld %3d %-20s killed by %-s\n", (tot - num),
table->score, table->level, table->name, table->howdead);
table++;
}
printf("\n\n");
}
/**/
/*************************
* readtable function *
**************************/
int readtable(score_entry * table_ptr)
{
FILE *fp;
int numread;
if ((fp = fopen(HISCOREPATH, R_BIN)) == NULL)
{
numread = 0;
}
else
{
numread = fread(VOIDSTAR table_ptr, sizeof(score_entry), ENTRIES, fp);
fclose(fp);
}
return numread;
}
/**/
/********************
* savescore *
*********************/
int savescore(char *howdead, long score, int level, char *name)
{
score_entry table[ENTRIES + 2], *table_ptr = table, new_entry, temp_entry;
int numread;
int index = 1;
int numsaved;
int lock;
int already = 0;
int output_value = 1;
int user_id;
int position;
FILE *fp;
#ifdef NO_RESTORED_GAME_HISCORES
if (saved_game)
{
printf("No hiscores recorded from restored games.\n");
printf("\nWanderer (C) 1988 S.Shipway.\n\n");
return 1;
}
#endif
user_id = getuid();
strncpy(new_entry.howdead, howdead, 25);
new_entry.howdead[24] = '\0'; /* M002 strncpy does not null terminate */
strncpy(new_entry.name, name, 20);
new_entry.name[19] = '\0'; /* M002 strncpy does not null terminate */
new_entry.score = score;
new_entry.level = level;
new_entry.uid = user_id;
LOCK; /* BUG is HERE !!! */
numread = readtable(table_ptr);
if (numread > 0)
if (table[numread - 1].score > 99999) /* stop system errors messing it up */
{
numread--;
printf("Erasing spurious entry in table.\n");
}
if (score == -1)
{
show_scores(table, numread);
UNLOCK;
return 0;
}
/* HERE */
if (numread > 0)
{
numread++; /* scan through until correct insertion point */
/* pass table entries with higher scores */
while ((table_ptr->score > score) && (index < numread))
{
if (SAMEUSER(table_ptr))
{
already = 1;
break;
}
table_ptr++;
index++;
}
/* pass table entries with equal score but higher or equal level */
while ((table_ptr->level >= level) && (index < numread)
&& (table_ptr->score == score))
{
if (SAMEUSER(table_ptr))
{
already = 1;
break;
}
table_ptr++;
index++;
}
position = index;
/* if already found: done */
if (already == 1)
{
numread--;
UNLOCK;
return numread;
}
/* shift down score list */
while (index < numread)
{
/* swap *table_ptr and new_entry */
temp_entry = *table_ptr;
*table_ptr = new_entry;
new_entry = temp_entry;
if (SAMEUSER(&new_entry))
{
already = 1;
numread--; /* an older entry found */
break;
}
table_ptr++;
index++;
}
/* if all shifted without finding an older entry */
if (already == 0)
*table_ptr = new_entry;
if (position <= ENTRIES)
printf("You rank %d in the hiscore table.\n", position);
else
printf
("Sorry, you didnt make it into the hiscore table this time.\n");
}
else
{
printf("\nCreating new hiscore table.\n\n");
*table_ptr = new_entry;
numread++;
}
numread = ((numread > ENTRIES) ? ENTRIES : numread);
if ((fp = fopen(HISCOREPATH, W_BIN)) != NULL)
{
table_ptr = table;
numsaved =
fwrite(VOIDSTAR table_ptr, sizeof(score_entry), numread, fp);
chmod(HISCOREPATH, 0666);
if (numsaved < numread)
{
printf("ERROR! Only %d items saved from %d !\n", numsaved,
numread);
output_value = 0;
}
fclose(fp);
}
else
{
UNLOCK;
err(1, "%s", "Error in savescore - fopen HISCOREPATH failed\n");
}
UNLOCK;
return output_value;
}
/**/
/*************************
* delete_entry *
**************************/
void delete_entry(int num)
{
score_entry table[ENTRIES + 22], *table_ptr = table;
int numread, index = 1, numsaved, lock;
FILE *fp;
LOCK;
numread = readtable(table_ptr);
if (numread == 0)
{
printf("Missing or unreadable hiscore table.\n\n");
UNLOCK;
exit(1);
}
if (num > numread)
{
printf("Invalid entry, choose again\n");
UNLOCK;
return;
}
while (index < num)
{
index++;
table_ptr++;
}
while (index < numread)
{
index++;
*table_ptr = *(table_ptr + 1);
table_ptr++;
}
numread--;
if ((fp = fopen(HISCOREPATH, W_BIN)) != NULL)
{
table_ptr = table;
numsaved =
fwrite(VOIDSTAR table_ptr, sizeof(score_entry), numread, fp);
chmod(HISCOREPATH, 0666);
if (numsaved < numread)
{
printf("ERROR! Only %d items saved from %d !\n", numsaved,
numread);
}
fclose(fp);
}
else
err(1, "%s", "Could not open Scorefile\n");
UNLOCK;
show_scores(table, numsaved);
}
/**/
/************************
* erase_scores *
*************************/
void erase_scores()
{
int erasenum;
int numread;
int index = 0;
char correct[20], c;
score_entry table[ENTRIES + 2], *table_ptr = table;
printf("Please enter password:");
while ((c = getchar()) != '\n' && index < 19)
{
correct[index++] = c;
}
correct[index] = 0;
if (strcmp(correct, MASTERPASSWORD))
{
printf("\nFoo, charlatan!\n");
return;
}
numread = readtable(table_ptr);
show_scores(table, numread);
printf("\n");
for (;;)
{
printf("Number to erase (0 to exit): ");
if (scanf("%d", &erasenum) == EOF && errno != 0)
{
fprintf(stderr, "scanf error\n");
exit(EXIT_FAILURE);
}
printf("\n");
if (erasenum == 0)
break;
delete_entry(erasenum);
printf("\n");
}
printf("Byee!\n");
}