comparison rogue4/rip.c @ 279:d3968e9cb98d

Use C stdio functions for score files and save files. Switching from Unix file descriptor operations to C standard FILE* functions will reduce portability problems.
author John "Elwin" Edwards
date Fri, 15 Sep 2017 19:57:54 -0400
parents 23d2e2bf812f
children 0250220d8cdd
comparison
equal deleted inserted replaced
278:c222f9d56776 279:d3968e9cb98d
47 score(int amount, int flags, char monst) 47 score(int amount, int flags, char monst)
48 { 48 {
49 register struct sc_ent *scp; 49 register struct sc_ent *scp;
50 register int i; 50 register int i;
51 register struct sc_ent *sc2; 51 register struct sc_ent *sc2;
52 register FILE *outf;
53 register int prflags = 0; 52 register int prflags = 0;
54 register void (*fp)(int); 53 register void (*fp)(int);
55 register int uid; 54 register int uid;
56 char scoreline[MAXSTR + 100]; 55 char scoreline[MAXSTR + 100];
57 56
70 }; 69 };
71 void endit(); 70 void endit();
72 71
73 start_score(); 72 start_score();
74 73
75 if (fd >= 0)
76 outf = md_fdopen(fd, "wb");
77 else
78 return;
79
80 for (scp = top_ten; scp <= &top_ten[9]; scp++) 74 for (scp = top_ten; scp <= &top_ten[9]; scp++)
81 { 75 {
82 scp->sc_score = 0; 76 scp->sc_score = 0;
83 for (i = 0; i < MAXSTR; i++) 77 for (i = 0; i < MAXSTR; i++)
84 scp->sc_name[i] = rnd(255); 78 scp->sc_name[i] = rnd(255);
110 else if (strcmp(prbuf, "edit") == 0) 104 else if (strcmp(prbuf, "edit") == 0)
111 prflags = 2; 105 prflags = 2;
112 #endif 106 #endif
113 for(i=0; i<10; i++) 107 for(i=0; i<10; i++)
114 { 108 {
115 encread((char *) &top_ten[i].sc_name, MAXSTR, fd); 109 encread((char *) &top_ten[i].sc_name, MAXSTR, score_file);
116 scoreline[0] = '\0'; 110 scoreline[0] = '\0';
117 encread((char *) scoreline, 100, fd); 111 encread((char *) scoreline, 100, score_file);
118 sscanf(scoreline, "%d %d %hd %hd %hd", &top_ten[i].sc_flags, 112 sscanf(scoreline, "%d %d %hd %hd %hd", &top_ten[i].sc_flags,
119 &top_ten[i].sc_uid, &top_ten[i].sc_monster, &top_ten[i].sc_score, 113 &top_ten[i].sc_uid, &top_ten[i].sc_monster, &top_ten[i].sc_score,
120 &top_ten[i].sc_level); 114 &top_ten[i].sc_level);
121 } 115 }
122 /* 116 /*
207 printf(".\n"); 201 printf(".\n");
208 } 202 }
209 else 203 else
210 break; 204 break;
211 } 205 }
212 fseek(outf, 0L, 0); 206 fseek(score_file, 0L, 0);
213 /* 207 /*
214 * Update the list file 208 * Update the list file
215 */ 209 */
216 if (sc2 != NULL) 210 if (sc2 != NULL)
217 { 211 {
221 215
222 fp = signal(SIGINT, SIG_IGN); 216 fp = signal(SIGINT, SIG_IGN);
223 217
224 for(i=0; i<10; i++) 218 for(i=0; i<10; i++)
225 { 219 {
226 encwrite((char *) &top_ten[i].sc_name, MAXSTR, outf); 220 encwrite((char *) &top_ten[i].sc_name, MAXSTR, score_file);
227 sprintf(scoreline," %d %d %hd %hd %hd \n", 221 sprintf(scoreline," %d %d %hd %hd %hd \n",
228 top_ten[i].sc_flags, top_ten[i].sc_uid, 222 top_ten[i].sc_flags, top_ten[i].sc_uid,
229 top_ten[i].sc_monster, top_ten[i].sc_score, 223 top_ten[i].sc_monster, top_ten[i].sc_score,
230 top_ten[i].sc_level); 224 top_ten[i].sc_level);
231 encwrite((char *) scoreline, 100, outf); 225 encwrite((char *) scoreline, 100, score_file);
232 } 226 }
233 unlock_sc(); 227 unlock_sc();
234 signal(SIGINT, fp); 228 signal(SIGINT, fp);
235 } 229 }
236 } 230 }
237 fclose(outf); 231 fclose(score_file);
238 } 232 }
239 233
240 void writelog(int amount, int flags, char monst) 234 void writelog(int amount, int flags, char monst)
241 { 235 {
242 FILE *logfi;
243 char logmessage[220], ltemp[80]; 236 char logmessage[220], ltemp[80];
244 char *killer; 237 char *killer;
245 if (noscore) 238 if (noscore)
246 return; 239 return;
247 #ifdef LOGFILE 240 #ifdef LOGFILE
248 if (lfd >= 0) 241 if (log_file == NULL)
249 logfi = md_fdopen(lfd, "a");
250 else
251 return;
252 if (logfi == NULL)
253 return; 242 return;
254 /* otherwise writing should work */ 243 /* otherwise writing should work */
255 sprintf(logmessage, "%d %d %s %d ", time(NULL), amount, whoami, 244 sprintf(logmessage, "%d %d %s %d ", time(NULL), amount, whoami,
256 pstats.s_lvl); 245 pstats.s_lvl);
257 if (flags == 0) /* died */ 246 if (flags == 0) /* died */
281 max_level); 270 max_level);
282 strcat(logmessage, ltemp); 271 strcat(logmessage, ltemp);
283 } 272 }
284 else 273 else
285 { 274 {
286 fclose(logfi); 275 fclose(log_file);
287 return; 276 return;
288 } 277 }
289 278
290 /* then write */ 279 /* then write */
291 fprintf(logfi, "%s", logmessage); 280 fprintf(log_file, "%s", logmessage);
292 fclose(logfi); 281 fclose(log_file);
293 #endif 282 #endif
294 return; 283 return;
295 } 284 }
296 285
297 /* 286 /*