forked from rogueforge/rogomatic14
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathscorefile.c
More file actions
303 lines (252 loc) · 8.09 KB
/
scorefile.c
File metadata and controls
303 lines (252 loc) · 8.09 KB
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
/*
* Rog-O-Matic
* Automatically exploring the dungeons of doom.
*
* Copyright (C) 2008 by Anthony Molinaro
* Copyright (C) 1985 by Appel, Jacobson, Hamey, and Mauldin.
*
* This file is part of Rog-O-Matic.
*
* Rog-O-Matic is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Rog-O-Matic is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Rog-O-Matic. If not, see <http://www.gnu.org/licenses/>.
*/
/*
* scorefile.c:
*
* This file contains the functions which update the rogomatic scorefile,
* which lives in <RGMDIR>/rgmscore<versionstr>. LOCKFILE is used to
* prevent simultaneous accesses to the file. rgmdelta<versionstr>
* contains new scores, and whenever the score file is printed the delta
* file is sorted and merged into the rgmscore file.
*/
# include <stdio.h>
# include <stdlib.h>
# include <unistd.h>
# include <sys/types.h>
# include <sys/stat.h>
# include <signal.h>
# include <string.h>
# include <errno.h>
# include "types.h"
# include "config.h"
# include "globals.h"
# include "install.h"
# define LINESIZE 2048
# define SCORE(s,p) (atoi (s+p))
/* static declarations */
static void intrupscore (int sig __attribute__ ((__unused__)));
/*
* add_score: Write a new score line out to the correct rogomatic score
* file by creating a temporary copy and inserting the new line in the
* proper place. Be tense about preventing simultaneous access to the
* score file and catching interrupts and things.
*/
void
add_score (char *new_line, char *vers, int ntrm)
{
char *newfil = NULL; /* rogomatic delta filename */
FILE *newlog;
int lock_fd;
/* form paths */
newfil = form_prefix_path ( rgmdir, "rgmdelta", vers);
/* Defer interrupts while mucking with the score file */
critical ();
/* lock */
lock_fd = lock_file (__func__, NULL, lock_path);
/* Now create a temporary to copy into */
if ((newlog = wopen (newfil, "a")) == NULL) {
quit (1, "ERROR: %s: file: %s line: %d dungeon: %u Unable to write: %s: %s\n",
__func__, __FILE__, __LINE__, dnum, newfil, strerror (errno));
not_reached ();
} else {
fprintf (newlog, "%s\n", new_line);
fflush (newlog);
fclose (newlog);
}
/* unlock */
unlock_file (__func__, lock_fd);
if (newfil != NULL) {
free (newfil);
newfil = NULL;
}
/* free path */
if (newfil != NULL) {
free (newfil);
newfil = NULL;
}
uncritical ();
}
/*
* dumpscore: Print out the scoreboard.
*/
void
dumpscore (char *vers)
{
char *scrfil = NULL; /* rogomatic score file path */
char *delfil = NULL; /* rogomatic delta file path */
char *newfil = NULL; /* rogomatic new file path */
char *allfil = NULL; /* rogomatic all scores file path */
char cmd[BIGBUF + 1]; /* shell command buffer, +1 for paranoia */
FILE *scoref; /* score file stream */
FILE *deltaf; /* delta file stream */
int oldmask; /* original umask */
int lock_fd; /* lock file descriptor */
int code = 0; /* command exit code */
char ch;
/* zeroize arrays */
memset (cmd, 0, sizeof(cmd)); /* paranoia */
/* form paths */
scrfil = form_prefix_path (rgmdir, "rgmscore", vers);
delfil = form_prefix_path (rgmdir, "rgmdelta", vers);
newfil = form_prefix_path (rgmdir, "NewScore", vers);
allfil = form_prefix_path (rgmdir, "AllScore", vers);
/* On interrupts we must relinquish control of the score file */
int_exit (intrupscore);
/* lock */
lock_fd = lock_file (__func__, NULL, lock_path);
deltaf = fopen (delfil, "r");
if (deltaf == NULL) {
quit (1, "ERROR: %s: file: %s line: %d dungeon: %u failed to open rogomatic delta file: %s error: %s\n",
__func__, __FILE__, __LINE__, dnum, delfil, strerror (errno));
not_reached ();
}
scoref = fopen (scrfil, "r");
if (scoref == NULL) {
quit (1, "ERROR: %s: file: %s line: %d dungeon: %u failed to open rogomatic score file: %s error: %s\n",
__func__, __FILE__, __LINE__, dnum, scrfil, strerror (errno));
not_reached ();
}
/* If there are new scores, sort and merge them into the score file */
if (deltaf != NULL) {
fclose (deltaf);
/* Defer interrupts while mucking with the score file */
critical ();
/* Make certain any new files are world writable */
oldmask = umask (0);
/* If we have an old file and a delta file, merge them */
if (scoref != NULL) {
/* first sort command and args */
char *args_0[] = {
"sort", "+4nr", "-o", newfil, delfil, NULL
};
/* second sort command and args */
char *args_1[] = {
"sort", "+4nr", "-o", allfil, newfil, scrfil, NULL
};
/* close out the score file */
fclose (scoref);
/*
* sort delta score file into a new rogomatic score file
*/
code = fork_exec ("sort", args_0);
if (code != 0) {
quit (1, "ERROR: %s: file: %s line: %d dungeon: %u sort +4nr -o %s %s failed, exit code: %d\n",
__func__, __FILE__, __LINE__, dnum, newfil, delfil, code);
not_reached ();
}
/*
* sort merge the new rogomatic score file, and existing rogomatic score file into rogomatic all score file
*/
code = fork_exec ("sort", args_1);
if (code != 0) {
quit (1, "ERROR: %s: file: %s line: %d dungeon: %u sort +4nr -o %s %s %s failed, exit code: %d\n",
__func__, __FILE__, __LINE__, dnum, allfil, newfil, scrfil, code);
not_reached ();
}
if (filelength (allfil) != filelength (delfil) + filelength (scrfil)) {
/* unlink */
unlink (newfil);
unlink (allfil);
/* unlock */
unlock_file (__func__, lock_fd);
quit (1, "ERROR: %s: file: %s line: %d dungeon: %u new file is wrong length!\n",
__func__, __FILE__, __LINE__, dnum);
not_reached ();
}
else {
/* New file is okay, unlink old files and pointer swap score file */
unlink (delfil);
unlink (newfil);
unlink (scrfil);
link (allfil, scrfil);
unlink (allfil);
}
scoref = fopen (scrfil, "r");
}
else
/* Only have delta file, sort into scorefile and unlink delta */
{
/* first sort command and args */
char *args[] = {
"sort", "+4nr", "-o", scrfil, delfil, NULL
};
/*
* sort delta score file into a rogomatic score file
*/
code = fork_exec ("sort", args);
if (code != 0) {
quit (1, "ERROR: %s: file: %s line: %d dungeon: %u sort +4nr -o %s %s failed, exit code: %d\n",
__func__, __FILE__, __LINE__, dnum, scrfil, delfil, code);
not_reached ();
}
unlink (delfil);
scoref = fopen (scrfil, "r");
}
/* Restore umask */
umask (oldmask);
/* Restore interrupt status after score file stable */
uncritical ();
}
/* Now any new scores have been put into scrfil, read it */
if (scoref == NULL) {
/* unlock */
unlock_file (__func__, lock_fd);
quit (1, "ERROR: %s: file: %s line: %d dungeon: %u Can't find: %s\n",
__func__, __FILE__, __LINE__, dnum, scrfil);
not_reached ();
}
printf ("Rog-O-Matic Scores against version %s:\n\n", vers);
printf ("%s%s", "Date Time User Gold Killed by",
" Lvl Hp Str Ac Exp Game Dungeon\n");
while ((int) (ch = fgetc (scoref)) != EOF)
putchar (ch);
fclose (scoref);
/* unlock */
unlock_file (__func__, lock_fd);
/* free paths */
if (scrfil != NULL) {
free (scrfil);
scrfil = NULL;
}
if (delfil != NULL) {
free (delfil);
scrfil = NULL;
}
if (newfil != NULL) {
free (newfil);
scrfil = NULL;
}
if (allfil != NULL) {
free (allfil);
scrfil = NULL;
}
exit (0);
}
/*
* intrupscore: We have an interrupt, clean up and unlock the score file.
*/
static void
intrupscore (int sig __attribute__ ((__unused__)))
{
exit (1);
}