Source:NetHack 5.0.0/src/files.c
Jump to navigation
Jump to search
Below is the full text to files.c from the source code of NetHack 5.0.0. To link to a particular line, write [[Source:NetHack 5.0.0/src/files.c#line12]], for example.
The NetHack General Public License applies to screenshots, source code and other content from NetHack.
This content was modified from the original NetHack source code distribution (by splitting up NetHack content between wiki pages, and possibly further editing). See the page history for a list of who changed it, and on what dates.
1. /* NetHack 5.0 files.c $NHDT-Date: 1740532826 2025/02/25 17:20:26 $ $NHDT-Branch: NetHack-3.7 $:$NHDT-Revision: 1.417 $ */ 2. /* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */ 3. /*-Copyright (c) Derek S. Ray, 2015. */ 4. /* NetHack may be freely redistributed. See license for details. */ 5. 6. #define NEED_VARARGS 7. 8. #if defined(WIN32) 9. #include "win32api.h" 10. #endif 11. 12. #include "hack.h" 13. #include "dlb.h" 14. 15. #ifdef SFCTOOL 16. #ifdef TTY_GRAPHICS 17. #undef TTY_GRAPHICS 18. #endif 19. #ifdef mark_synch 20. #undef mark_synch 21. #endif 22. #define mark_synch() 23. #ifdef raw_print 24. #undef raw_print 25. #endif 26. #define raw_print(a) 27. #ifdef WINDOWPORT 28. #undef WINDOWPORT 29. #endif 30. #define WINDOWPORT(x) FALSE 31. #ifdef clear_nhwindow 32. #undef clear_nhwindow 33. #endif 34. #define clear_nhwindow(x) 35. #endif /* SFCTOOL */ 36. 37. #ifdef TTY_GRAPHICS 38. #include "wintty.h" /* more() */ 39. #endif 40. 41. #if (!defined(MACOS9) && !defined(O_WRONLY) && !defined(AZTEC_C)) \ 42. || defined(USE_FCNTL) 43. #include <fcntl.h> 44. #endif 45. 46. #include <errno.h> 47. #ifdef _MSC_VER /* MSC 6.0 defines errno quite differently */ 48. #if (_MSC_VER >= 600) 49. #define SKIP_ERRNO 50. #endif 51. #else 52. #ifdef NHSTDC 53. #define SKIP_ERRNO 54. #endif 55. #endif 56. #ifndef SKIP_ERRNO 57. #ifdef _DCC 58. const 59. #endif 60. extern int errno; 61. #endif 62. 63. #ifdef ZLIB_COMP /* RLC 09 Mar 1999: Support internal ZLIB */ 64. #include "zlib.h" 65. #ifndef COMPRESS_EXTENSION 66. #define COMPRESS_EXTENSION ".gz" 67. #endif 68. #endif 69. 70. #if defined(UNIX) && defined(SELECTSAVED) 71. #include <sys/types.h> 72. #include <dirent.h> 73. #endif 74. 75. #if defined(UNIX) || defined(VMS) || !defined(NO_SIGNAL) 76. #include <signal.h> 77. #endif 78. 79. #if defined(MSDOS) || defined(OS2) || defined(TOS) || defined(WIN32) 80. #ifndef __DJGPP__ 81. #include <sys\stat.h> 82. #else 83. #include <sys/stat.h> 84. #endif 85. #endif 86. #ifndef O_BINARY /* used for micros, no-op for others */ 87. #define O_BINARY 0 88. #endif 89. 90. #ifdef PREFIXES_IN_USE 91. #define FQN_NUMBUF 8 92. static char fqn_filename_buffer[FQN_NUMBUF][FQN_MAX_FILENAME]; 93. #endif 94. 95. #if !defined(SAVE_EXTENSION) 96. #ifdef MICRO 97. #define SAVE_EXTENSION ".sav" 98. #endif 99. #ifdef WIN32 100. #define SAVE_EXTENSION ".NetHack-saved-game" 101. #endif 102. #endif 103. 104. #if defined(WIN32) 105. #include <share.h> 106. #include <io.h> 107. #define F_OK 0 108. #define access _access 109. #endif 110. 111. #ifdef AMIGA 112. extern char PATH[]; /* see sys/amiga/amidos.c */ 113. extern char bbs_id[]; 114. #ifdef __SASC_60 115. #include <proto/dos.h> 116. #endif 117. 118. #include <libraries/dos.h> 119. extern void amii_set_text_font(char *, int); 120. #endif 121. 122. #if defined(WIN32) || defined(MSDOS) 123. #ifdef MSDOS 124. #define Delay(a) msleep(a) 125. #endif 126. #define Close close 127. #ifndef WIN_CE 128. #ifdef DeleteFile 129. #undef DeleteFile 130. #endif 131. #define DeleteFile unlink 132. #endif 133. #ifdef WIN32 134. /*from windsys.c */ 135. extern char *translate_path_variables(const char *, char *); 136. extern boolean get_user_home_folder(char *, size_t); 137. #endif 138. #endif 139. 140. #ifdef MACOS9 141. #undef unlink 142. #define unlink macunlink 143. #endif 144. 145. #if (defined(macintosh) && (defined(__SC__) || defined(__MRC__))) \ 146. || defined(__MWERKS__) 147. #define PRAGMA_UNUSED 148. #endif 149. 150. #ifndef SFCTOOL 151. staticfn NHFILE *new_nhfile(void); 152. staticfn void free_nhfile(NHFILE *); 153. #else 154. NHFILE *new_nhfile(void); 155. void free_nhfile(NHFILE *); 156. #endif /* SFCTOOL */ 157. 158. #ifdef SELECTSAVED 159. staticfn int QSORTCALLBACK strcmp_wrap(const void *, const void *); 160. #endif 161. staticfn char *set_bonesfile_name(char *, d_level *); 162. staticfn char *set_bonestemp_name(void); 163. #ifdef COMPRESS 164. staticfn void redirect(const char *, const char *, FILE *, boolean); 165. #endif 166. #if defined(COMPRESS) || defined(ZLIB_COMP) 167. staticfn void docompress_file(const char *, boolean); 168. #endif 169. #if defined(ZLIB_COMP) 170. staticfn boolean make_compressed_name(const char *, char *); 171. #endif 172. 173. staticfn NHFILE *problematic_savefile(int, const char *); 174. #ifndef SFCTOOL 175. staticfn int doconvert_file(const char *, int, boolean); 176. #endif /* SFCTOOL */ 177. staticfn boolean make_converted_name(const char *); 178. 179. #ifndef SFCTOOL 180. staticfn NHFILE *viable_nhfile(NHFILE *); 181. #ifdef SELECTSAVED 182. staticfn int QSORTCALLBACK strcmp_wrap(const void *, const void *); 183. #endif 184. staticfn char *set_bonesfile_name(char *, d_level *); 185. staticfn char *set_bonestemp_name(void); 186. #ifndef USE_FCNTL 187. staticfn char *make_lockname(const char *, char *); 188. #endif 189. staticfn FILE *fopen_wizkit_file(void); 190. staticfn void wizkit_addinv(struct obj *); 191. boolean proc_wizkit_line(char *buf); 192. void read_wizkit(void); /* in extern.h; why here too? */ 193. staticfn FILE *fopen_sym_file(void); 194. staticfn NHFILE *viable_nhfile(NHFILE *); 195. #endif /* !SFCTOOL */ 196. 197. /* return a file's name without its path and optionally trailing 'type' */ 198. const char * 199. nh_basename(const char *fname, boolean keep_suffix) 200. { 201. #ifndef VMS 202. static char basebuf[80]; 203. const char *p; 204. 205. if ((p = strrchr(fname, '/')) != 0) 206. fname = p + 1; 207. #if defined(WIN32) || defined(MSDOS) 208. if ((p = strrchr(fname, '\\')) != 0) 209. fname = p + 1; 210. #endif 211. if ((p = strrchr(fname, '.')) != 0 && !keep_suffix) { 212. size_t ln = (size_t) (p - fname); 213. /* note that without path, name should be reasonable length; 214. it is expected to refer to a source file name or run-time 215. configuration file name and those aren't arbitrarily long; 216. if "name" part of "name.suffix" is too long for 'basebuf[]', 217. just return that as-is without stripping off ".suffix" */ 218. 219. if (ln < sizeof basebuf) { 220. strncpy(basebuf, fname, ln); 221. basebuf[ln] = '\0'; 222. fname = basebuf; 223. } 224. } 225. #else /* VMS */ 226. fname = vms_basename(fname, keep_suffix); 227. #endif 228. return fname; 229. } 230. 231. /* 232. * fname_encode() 233. * 234. * Args: 235. * legal zero-terminated list of acceptable file name characters 236. * quotechar lead-in character used to quote illegal characters as 237. * hex digits 238. * s string to encode 239. * callerbuf buffer to house result 240. * bufsz size of callerbuf 241. * 242. * Notes: 243. * The hex digits 0-9 and A-F are always part of the legal set due to 244. * their use in the encoding scheme, even if not explicitly included in 245. * 'legal'. 246. * 247. * Sample: 248. * The following call: 249. * (void) fname_encode("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz", 250. * '%', "This is a % test!", buf, 512); 251. * results in this encoding: 252. * "This%20is%20a%20%25%20test%21" 253. */ 254. char * 255. fname_encode( 256. const char *legal, 257. char quotechar, 258. char *s, 259. char *callerbuf, 260. int bufsz) 261. { 262. char *sp, *op; 263. int cnt = 0; 264. static char hexdigits[] = "0123456789ABCDEF"; 265. 266. sp = s; 267. op = callerbuf; 268. *op = '\0'; 269. 270. while (*sp) { 271. /* Do we have room for one more character or encoding? */ 272. if ((bufsz - cnt) <= 4) 273. return callerbuf; 274. 275. if (*sp == quotechar) { 276. (void) sprintf(op, "%c%02X", quotechar, *sp); 277. op += 3; 278. cnt += 3; 279. } else if ((strchr(legal, *sp) != 0) 280. || (strchr(hexdigits, *sp) != 0)) { 281. *op++ = *sp; 282. *op = '\0'; 283. cnt++; 284. } else { 285. (void) sprintf(op, "%c%02X", quotechar, *sp); 286. op += 3; 287. cnt += 3; 288. } 289. sp++; 290. } 291. return callerbuf; 292. } 293. 294. /* 295. * fname_decode() 296. * 297. * Args: 298. * quotechar lead-in character used to quote illegal characters as 299. * hex digits 300. * s string to decode 301. * callerbuf buffer to house result 302. * bufsz size of callerbuf 303. */ 304. char * 305. fname_decode(char quotechar, char *s, char *callerbuf, int bufsz) 306. { 307. char *sp, *op; 308. int k, calc, cnt = 0; 309. static char hexdigits[] = "0123456789ABCDEF"; 310. 311. sp = s; 312. op = callerbuf; 313. *op = '\0'; 314. 315. while (*sp) { 316. /* Do we have room for one more character? */ 317. if ((bufsz - cnt) <= 2) 318. return callerbuf; 319. if (*sp == quotechar) { 320. sp++; 321. for (k = 0; k < 16; ++k) 322. if (*sp == hexdigits[k]) 323. break; 324. if (k >= 16) 325. return callerbuf; /* impossible, so bail */ 326. calc = k << 4; 327. sp++; 328. for (k = 0; k < 16; ++k) 329. if (*sp == hexdigits[k]) 330. break; 331. if (k >= 16) 332. return callerbuf; /* impossible, so bail */ 333. calc += k; 334. sp++; 335. *op++ = calc; 336. *op = '\0'; 337. } else { 338. *op++ = *sp++; 339. *op = '\0'; 340. } 341. cnt++; 342. } 343. return callerbuf; 344. } 345. 346. #ifdef PREFIXES_IN_USE 347. #define UNUSED_if_not_PREFIXES_IN_USE /*empty*/ 348. #else 349. #define UNUSED_if_not_PREFIXES_IN_USE UNUSED 350. #endif 351. 352. /*ARGSUSED*/ 353. const char * 354. fqname(const char *basenam, 355. int whichprefix UNUSED_if_not_PREFIXES_IN_USE, 356. int buffnum UNUSED_if_not_PREFIXES_IN_USE) 357. { 358. #ifdef PREFIXES_IN_USE 359. char *bufptr; 360. #endif 361. #ifdef WIN32 362. char tmpbuf[BUFSZ]; 363. #endif 364. 365. #ifndef PREFIXES_IN_USE 366. return basenam; 367. #else 368. if (!basenam || whichprefix < 0 || whichprefix >= PREFIX_COUNT) 369. return basenam; 370. if (!gf.fqn_prefix[whichprefix]) 371. return basenam; 372. if (buffnum < 0 || buffnum >= FQN_NUMBUF) { 373. impossible("Invalid fqn_filename_buffer specified: %d", buffnum); 374. buffnum = 0; 375. } 376. bufptr = gf.fqn_prefix[whichprefix]; 377. #ifdef WIN32 378. if (strchr(gf.fqn_prefix[whichprefix], '%') 379. || strchr(gf.fqn_prefix[whichprefix], '~')) 380. bufptr = translate_path_variables(gf.fqn_prefix[whichprefix], tmpbuf); 381. #endif 382. if (strlen(bufptr) + strlen(basenam) >= FQN_MAX_FILENAME) { 383. impossible("fqname too long: %s + %s", bufptr, basenam); 384. return basenam; /* XXX */ 385. } 386. Strcpy(fqn_filename_buffer[buffnum], bufptr); 387. return strcat(fqn_filename_buffer[buffnum], basenam); 388. #endif /* !PREFIXES_IN_USE */ 389. } 390. 391. #ifndef SFCTOOL 392. /* reasonbuf must be at least BUFSZ, supplied by caller */ 393. int 394. validate_prefix_locations(char *reasonbuf) 395. { 396. #if defined(NOCWD_ASSUMPTIONS) 397. FILE *fp; 398. const char *filename; 399. int prefcnt, failcount = 0; 400. char panicbuf1[BUFSZ], panicbuf2[BUFSZ]; 401. const char *details; 402. #endif 403. 404. if (reasonbuf) 405. reasonbuf[0] = '\0'; 406. #if defined(NOCWD_ASSUMPTIONS) 407. for (prefcnt = 1; prefcnt < PREFIX_COUNT; prefcnt++) { 408. /* don't test writing to configdir or datadir; they're readonly */ 409. if (prefcnt == SYSCONFPREFIX || prefcnt == CONFIGPREFIX 410. || prefcnt == DATAPREFIX) 411. continue; 412. filename = fqname("validate", prefcnt, 3); 413. if ((fp = fopen(filename, "w"))) { 414. fclose(fp); 415. (void) unlink(filename); 416. } else { 417. if (reasonbuf) { 418. if (failcount) 419. Strcat(reasonbuf, ", "); 420. Strcat(reasonbuf, fqn_prefix_names[prefcnt]); 421. } 422. /* the paniclog entry gets the value of errno as well */ 423. Sprintf(panicbuf1, "Invalid %s", fqn_prefix_names[prefcnt]); 424. #if defined(NHSTDC) && !defined(NOTSTDC) 425. if (!(details = strerror(errno))) 426. #endif 427. details = ""; 428. Sprintf(panicbuf2, "\"%s\", (%d) %s", 429. gf.fqn_prefix[prefcnt], errno, details); 430. paniclog(panicbuf1, panicbuf2); 431. failcount++; 432. } 433. } 434. if (failcount) 435. return 0; 436. else 437. #endif 438. return 1; 439. } 440. 441. /* fopen a file, with OS-dependent bells and whistles */ 442. /* NOTE: a simpler version of this routine also exists in util/dlb_main.c */ 443. FILE * 444. fopen_datafile(const char *filename, const char *mode, int prefix) 445. { 446. FILE *fp; 447. 448. filename = fqname(filename, prefix, prefix == TROUBLEPREFIX ? 3 : 0); 449. fp = fopen(filename, mode); 450. return fp; 451. } 452. #endif /* !SFCTOOL */ 453. 454. /* ---------- EXTERNAL FILE SUPPORT ----------- */ 455. 456. /* determine byte order */ 457. static const int bei = 1; 458. #define IS_BIGENDIAN() ( (*(char*)&bei) == 0 ) 459. 460. void 461. init_nhfile(NHFILE *nhfp) 462. { 463. if (nhfp->structlevel) { 464. if (nhfp->fd != -1) { 465. impossible("Warning - Unclosed structlevel file being reinitialized"); 466. (void) nhclose(nhfp->fd); 467. } 468. } else if (nhfp->fpdef) { 469. if (nhfp->fpdef) { 470. impossible("Warning - Unclosed fieldlevel file being reinitialized"); 471. (void) fclose(nhfp->fpdef); 472. } 473. } 474. nhfp->fd = -1; 475. nhfp->fpdef = (FILE *) 0; 476. 477. nhfp->mode = COUNTING; 478. nhfp->structlevel = TRUE; 479. nhfp->fieldlevel = FALSE; 480. nhfp->addinfo = FALSE; 481. nhfp->bendian = IS_BIGENDIAN(); 482. nhfp->fplog = (FILE *) 0; 483. nhfp->fpdebug = (FILE *) 0; 484. nhfp->rcount = nhfp->wcount = 0; 485. nhfp->eof = FALSE; 486. nhfp->fnidx = 0; 487. nhfp->style.deflt = FALSE; 488. nhfp->style.binary = TRUE; 489. nhfp->nhfpconvert = 0; 490. } 491. 492. #ifndef SFCTOOL 493. staticfn 494. #endif 495. NHFILE * 496. new_nhfile(void) 497. { 498. NHFILE *nhfp = (NHFILE *) alloc(sizeof *nhfp); 499. 500. memset((genericptr_t) nhfp, 0, sizeof *nhfp); 501. init_nhfile(nhfp); 502. return nhfp; 503. } 504. 505. #ifndef SFCTOOL 506. staticfn 507. #endif 508. void 509. free_nhfile(NHFILE *nhfp) 510. { 511. if (nhfp) { 512. init_nhfile(nhfp); 513. free((genericptr_t) nhfp); 514. } 515. } 516. 517. void 518. close_nhfile(NHFILE *nhfp) 519. { 520. if (nhfp->structlevel && nhfp->fd != -1) 521. (void) nhclose(nhfp->fd), nhfp->fd = -1; 522. else if (nhfp->fpdef) 523. (void) fclose(nhfp->fpdef), nhfp->fpdef = (FILE *) 0; 524. if (nhfp->fplog) 525. (void) fprintf(nhfp->fplog, "# closing\n"); 526. if (nhfp->fplog) 527. (void) fclose(nhfp->fplog); 528. if (nhfp->fpdebug) 529. (void) fclose(nhfp->fpdebug); 530. free_nhfile(nhfp); 531. } 532. 533. void 534. rewind_nhfile(NHFILE *nhfp) 535. { 536. if (nhfp->structlevel) { 537. #ifdef BSD 538. (void) lseek(nhfp->fd, 0L, 0); 539. #else 540. (void) lseek(nhfp->fd, (off_t) 0, 0); 541. #endif 542. } else { 543. rewind(nhfp->fpdef); 544. } 545. } 546. 547. #ifndef SFCTOOL 548. staticfn NHFILE * 549. viable_nhfile(NHFILE *nhfp) 550. { 551. /* perform some sanity checks before returning 552. the pointer to the nethack file descriptor */ 553. if (nhfp) { 554. /* check for no open file at all, 555. * not a structlevel legacy file, 556. * nor a fieldlevel file. 557. */ 558. if (((nhfp->fd == -1) && !nhfp->fpdef) 559. || (nhfp->structlevel && nhfp->fd < 0) 560. || (nhfp->fieldlevel && !nhfp->fpdef)) { 561. /* not viable, start the cleanup */ 562. if (nhfp->fieldlevel) { 563. if (nhfp->fpdef) { 564. (void) fclose(nhfp->fpdef); 565. nhfp->fpdef = (FILE *) 0; 566. } 567. if (nhfp->fplog) { 568. (void) fprintf(nhfp->fplog, "# closing, not viable\n"); 569. (void) fclose(nhfp->fplog); 570. } 571. if (nhfp->fpdebug) 572. (void) fclose(nhfp->fpdebug); 573. } 574. free_nhfile(nhfp); 575. nhfp = (NHFILE *) 0; 576. } 577. } 578. return nhfp; 579. } 580. #endif /* !SFCTOOL */ 581. 582. int 583. nhclose(int fd) 584. { 585. int retval = 0; 586. 587. if (fd >= 0) { 588. if (close_check(fd)) 589. bclose(fd); 590. else 591. retval = close(fd); 592. } 593. return retval; 594. } 595. 596. /* ---------- BEGIN LEVEL FILE HANDLING ----------- */ 597. 598. #ifndef SFCTOOL 599. /* Construct a file name for a level-type file, which is of the form 600. * something.level (with any old level stripped off). 601. * This assumes there is space on the end of 'file' to append 602. * a two digit number. This is true for 'level' 603. * but be careful if you use it for other things -dgk 604. */ 605. void 606. set_levelfile_name(char *file, int lev) 607. { 608. char *tf; 609. 610. tf = strrchr(file, '.'); 611. if (!tf) 612. tf = eos(file); 613. Sprintf(tf, ".%d", lev); 614. #ifdef VMS 615. Strcat(tf, ";1"); 616. #endif 617. return; 618. } 619. 620. NHFILE * 621. create_levelfile(int lev, char errbuf[]) 622. { 623. const char *fq_lock; 624. NHFILE *nhfp = (NHFILE *) 0; 625. 626. if (errbuf) 627. *errbuf = '\0'; 628. set_levelfile_name(gl.lock, lev); 629. fq_lock = fqname(gl.lock, LEVELPREFIX, 0); 630. 631. nhfp = new_nhfile(); 632. if (nhfp) { 633. nhfp->ftype = NHF_LEVELFILE; 634. nhfp->mode = WRITING; 635. nhfp->structlevel = TRUE; /* do set this TRUE for levelfiles */ 636. nhfp->fieldlevel = FALSE; /* don't set this TRUE for levelfiles */ 637. nhfp->addinfo = FALSE; 638. nhfp->style.deflt = FALSE; 639. nhfp->style.binary = TRUE; 640. nhfp->fnidx = historical; 641. nhfp->fd = -1; 642. nhfp->fpdef = (FILE *) 0; 643. #if defined(MICRO) || defined(WIN32) 644. /* Use O_TRUNC to force the file to be shortened if it already 645. * exists and is currently longer. 646. */ 647. nhfp->fd = open(fq_lock, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 648. FCMASK); 649. #else 650. #ifdef MACOS9 651. nhfp->fd = maccreat(fq_lock, LEVL_TYPE); 652. #else 653. nhfp->fd = creat(fq_lock, FCMASK); 654. #endif 655. #endif /* MICRO || WIN32 */ 656. 657. if (nhfp->fd >= 0) 658. svl.level_info[lev].flags |= LFILE_EXISTS; 659. else if (errbuf) /* failure explanation */ 660. Sprintf(errbuf, 661. "Cannot create file \"%s\" for level %d (errno %d).", 662. gl.lock, lev, errno); 663. #if defined(MSDOS) || defined(WIN32) 664. if (nhfp->fd >= 0) 665. (void) setmode(nhfp->fd, O_BINARY); 666. #endif 667. } 668. nhfp = viable_nhfile(nhfp); 669. return nhfp; 670. } 671. 672. NHFILE * 673. open_levelfile(int lev, char errbuf[]) 674. { 675. const char *fq_lock; 676. NHFILE *nhfp = (NHFILE *) 0; 677. 678. if (errbuf) 679. *errbuf = '\0'; 680. set_levelfile_name(gl.lock, lev); 681. fq_lock = fqname(gl.lock, LEVELPREFIX, 0); 682. nhfp = new_nhfile(); 683. if (nhfp) { 684. nhfp->mode = READING; 685. nhfp->structlevel = TRUE; /* do set this TRUE for levelfiles */ 686. nhfp->fieldlevel = FALSE; /* do not set this TRUE for levelfiles */ 687. nhfp->addinfo = FALSE; 688. nhfp->style.deflt = FALSE; 689. nhfp->style.binary = TRUE; 690. nhfp->ftype = NHF_LEVELFILE; 691. nhfp->fnidx = historical; 692. nhfp->fd = -1; 693. nhfp->fpdef = (FILE *) 0; 694. } 695. if (nhfp && nhfp->structlevel) { 696. #ifdef MACOS9 697. nhfp->fd = macopen(fq_lock, O_RDONLY | O_BINARY, LEVL_TYPE); 698. #else 699. nhfp->fd = open(fq_lock, O_RDONLY | O_BINARY, 0); 700. #endif 701. 702. /* for failure, return an explanation that our caller can use; 703. settle for `lock' instead of `fq_lock' because the latter 704. might end up being too big for nethack's BUFSZ */ 705. if (nhfp->fd < 0 && errbuf) 706. Sprintf(errbuf, 707. "Cannot open file \"%s\" for level %d (errno %d).", 708. gl.lock, lev, errno); 709. #if defined(MSDOS) || defined(WIN32) 710. if (nhfp->fd >= 0) 711. (void) setmode(nhfp->fd, O_BINARY); 712. #endif 713. } 714. nhfp = viable_nhfile(nhfp); 715. return nhfp; 716. } 717. 718. void 719. delete_levelfile(int lev) 720. { 721. /* 722. * Level 0 might be created by port specific code that doesn't 723. * call create_levfile(), so always assume that it exists. 724. */ 725. if (lev == 0 || (svl.level_info[lev].flags & LFILE_EXISTS)) { 726. set_levelfile_name(gl.lock, lev); 727. (void) unlink(fqname(gl.lock, LEVELPREFIX, 0)); 728. svl.level_info[lev].flags &= ~LFILE_EXISTS; 729. } 730. } 731. 732. void 733. clearlocks(void) 734. { 735. int x; 736. 737. #ifdef HANGUPHANDLING 738. if (program_state.preserve_locks) 739. return; 740. #endif 741. #ifndef NO_SIGNAL 742. (void) signal(SIGINT, SIG_IGN); 743. #if defined(UNIX) || defined(VMS) 744. sethanguphandler((void (*)(int)) SIG_IGN); 745. #endif 746. #endif /* NO_SIGNAL */ 747. /* can't access maxledgerno() before dungeons are created -dlc */ 748. for (x = (svn.n_dgns ? maxledgerno() : 0); x >= 0; x--) 749. delete_levelfile(x); /* not all levels need be present */ 750. } 751. 752. #if defined(SELECTSAVED) 753. /* qsort comparison routine */ 754. staticfn int QSORTCALLBACK 755. strcmp_wrap(const void *p, const void *q) 756. { 757. return strcmp(*(char **) p, *(char **) q); 758. } 759. #endif 760. 761. /* ---------- END LEVEL FILE HANDLING ----------- */ 762. 763. /* ---------- BEGIN BONES FILE HANDLING ----------- */ 764. 765. /* set up "file" to be file name for retrieving bones, and return a 766. * bonesid to be read/written in the bones file. 767. */ 768. staticfn char * 769. set_bonesfile_name(char *file, d_level *lev) 770. { 771. s_level *sptr; 772. char *dptr; 773. 774. /* 775. * "bonD0.nn" = bones for level nn in the main dungeon; 776. * "bonM0.T" = bones for Minetown; 777. * "bonQBar.n" = bones for level n in the Barbarian quest; 778. * "bon3D0.nn" = \ 779. * "bon3M0.T" = > same as above, but for bones pool #3. 780. * "bon3QBar.n" = / 781. * 782. * Return value for content validation skips "bon" and the 783. * pool number (if present), making it feasible for the admin 784. * to manually move a bones file from one pool to another by 785. * renaming it. 786. */ 787. Strcpy(file, "bon"); 788. #ifdef SYSCF 789. if (sysopt.bones_pools > 1) { 790. unsigned poolnum = min((unsigned) sysopt.bones_pools, 10); 791. 792. poolnum = (unsigned) ubirthday % poolnum; /* 0..9 */ 793. Sprintf(eos(file), "%u", poolnum); 794. } 795. #endif 796. dptr = eos(file); 797. /* when this naming scheme was adopted, 'filecode' was one letter; 798. 3.3.0 turned it into a three letter string for quest levels */ 799. Sprintf(dptr, "%c%s", svd.dungeons[lev->dnum].boneid, 800. In_quest(lev) ? gu.urole.filecode : "0"); 801. if ((sptr = Is_special(lev)) != 0) 802. Sprintf(eos(dptr), ".%c", sptr->boneid); 803. else 804. Sprintf(eos(dptr), ".%d", lev->dlevel); 805. #ifdef VMS 806. Strcat(dptr, ";1"); 807. #endif 808. return dptr; 809. } 810. 811. /* set up temporary file name for writing bones, to avoid another game's 812. * trying to read from an uncompleted bones file. we want an uncontentious 813. * name, so use one in the namespace reserved for this game's level files. 814. * (we are not reading or writing level files while writing bones files, so 815. * the same array may be used instead of copying.) 816. */ 817. staticfn char * 818. set_bonestemp_name(void) 819. { 820. char *tf; 821. 822. tf = strrchr(gl.lock, '.'); 823. if (!tf) 824. tf = eos(gl.lock); 825. Sprintf(tf, ".bn"); 826. #ifdef VMS 827. Strcat(tf, ";1"); 828. #endif 829. return gl.lock; 830. } 831. 832. NHFILE * 833. create_bonesfile(d_level *lev, char **bonesid, char errbuf[]) 834. { 835. const char *file; 836. NHFILE *nhfp = (NHFILE *) 0; 837. int failed = 0; 838. #if defined(WIN32) 839. errno_t err; 840. #endif 841. 842. if (errbuf) 843. *errbuf = '\0'; 844. *bonesid = set_bonesfile_name(gb.bones, lev); 845. file = set_bonestemp_name(); 846. file = fqname(file, BONESPREFIX, 0); 847. 848. nhfp = new_nhfile(); 849. if (nhfp) { 850. nhfp->ftype = NHF_BONESFILE; 851. nhfp->mode = WRITING; 852. nhfp->structlevel = TRUE; 853. nhfp->fieldlevel = FALSE; 854. nhfp->addinfo = TRUE; 855. nhfp->style.deflt = TRUE; 856. nhfp->style.binary = TRUE; 857. nhfp->fnidx = historical; 858. nhfp->fd = -1; 859. nhfp->fpdef = (FILE *) 0; 860. if (nhfp->fpdef) { 861. #ifdef SAVEFILE_DEBUGGING 862. nhfp->fpdebug = fopen("create_bonesfile-debug.log", "a"); 863. #endif 864. } else { 865. failed = errno; 866. } 867. if (nhfp->structlevel) { 868. #if defined(MICRO) 869. /* Use O_TRUNC to force the file to be shortened if it already 870. * exists and is currently longer. 871. */ 872. nhfp->fd = open(file, 873. O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, FCMASK); 874. #elif defined(WIN32) 875. err = _sopen_s(&nhfp->fd, file, 876. O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, 877. _SH_DENYRW, _S_IREAD | _S_IWRITE); 878. #else /* ?MICRO || WIN32 */ 879. /* implies UNIX or MACOS9 (MACOS9 is for OS9 or earlier) */ 880. #ifdef MACOS9 881. nhfp->fd = maccreat(file, BONE_TYPE); 882. #else 883. nhfp->fd = creat(file, FCMASK); 884. #endif /* ?MACOS9 */ 885. #endif /* ?MICRO || WIN32 */ 886. if (nhfp->fd < 0) 887. failed = errno; 888. #if defined(MSDOS) || defined(WIN32) 889. if (nhfp->fd >= 0) 890. (void) setmode(nhfp->fd, O_BINARY); 891. #endif 892. } 893. if (failed && errbuf) /* failure explanation */ 894. Sprintf(errbuf, "Cannot create bones \"%s\", id %s (errno %d).", 895. gl.lock, *bonesid, errno); 896. } 897. #if defined(VMS) && !defined(SECURE) 898. /* 899. Re-protect bones file with world:read+write+execute+delete access. 900. umask() doesn't seem very reliable; also, vaxcrtl won't let us set 901. delete access without write access, which is what's really wanted. 902. Can't simply create it with the desired protection because creat 903. ANDs the mask with the user's default protection, which usually 904. denies some or all access to world. 905. */ 906. (void) chmod(file, FCMASK | 007); /* allow other users full access */ 907. #endif /* VMS && !SECURE */ 908. 909. nhfp = viable_nhfile(nhfp); 910. return nhfp; 911. } 912. 913. /* move completed bones file to proper name */ 914. void 915. commit_bonesfile(d_level *lev) 916. { 917. const char *fq_bones, *tempname; 918. int ret; 919. 920. (void) set_bonesfile_name(gb.bones, lev); 921. fq_bones = fqname(gb.bones, BONESPREFIX, 0); 922. tempname = set_bonestemp_name(); 923. tempname = fqname(tempname, BONESPREFIX, 1); 924. 925. #if (defined(SYSV) && !defined(SVR4)) || defined(GENIX) 926. /* old SYSVs don't have rename. Some SVR3's may, but since they 927. * also have link/unlink, it doesn't matter. :-) 928. */ 929. (void) unlink(fq_bones); 930. ret = link(tempname, fq_bones); 931. ret += unlink(tempname); 932. #else 933. ret = rename(tempname, fq_bones); 934. #endif 935. if (wizard && ret != 0) 936. pline("couldn't rename %s to %s.", tempname, fq_bones); 937. } 938. 939. NHFILE * 940. open_bonesfile(d_level *lev, char **bonesid) 941. { 942. const char *fq_bones; 943. NHFILE *nhfp = (NHFILE *) 0; 944. #if defined(WIN32) 945. errno_t err UNUSED; 946. #endif 947. 948. *bonesid = set_bonesfile_name(gb.bones, lev); 949. fq_bones = fqname(gb.bones, BONESPREFIX, 0); 950. nh_uncompress(fq_bones); /* no effect if nonexistent */ 951. 952. nhfp = new_nhfile(); 953. if (nhfp) { 954. #if defined(WIN32) && defined(DEBUG) 955. if (nhfp->fd >= 0) 956. impossible("bones file NHFILE * has odd fd (%d)", nhfp->fd); 957. #endif 958. nhfp->structlevel = TRUE; 959. nhfp->fieldlevel = FALSE; 960. nhfp->ftype = NHF_BONESFILE; 961. nhfp->mode = READING; 962. nhfp->addinfo = TRUE; 963. nhfp->style.deflt = TRUE; 964. nhfp->style.binary = (sysopt.bonesformat[0] != exportascii); 965. nhfp->fnidx = sysopt.bonesformat[0]; 966. nhfp->fd = -1; 967. nhfp->fpdef = (FILE *) 0; 968. if (nhfp->fpdef) { 969. #ifdef SAVEFILE_DEBUGGING 970. nhfp->fpdebug = fopen("open_bonesfile-debug.log", "a"); 971. #endif 972. } 973. if (nhfp->structlevel) { 974. #if defined(MACOS9) 975. nhfp->fd = macopen(fq_bones, O_RDONLY | O_BINARY, BONE_TYPE); 976. #elif defined(WIN32) 977. err = _sopen_s(&nhfp->fd, fq_bones, _O_RDONLY | _O_BINARY, 978. _SH_DENYRW, _S_IREAD | _S_IWRITE); 979. #else 980. nhfp->fd = open(fq_bones, O_RDONLY | O_BINARY, 0); 981. #endif 982. #if defined(MSDOS) || defined(WIN32) 983. if (nhfp->fd >= 0) 984. (void) setmode(nhfp->fd, O_BINARY); 985. #endif 986. } 987. } 988. nhfp = viable_nhfile(nhfp); 989. return nhfp; 990. } 991. 992. int 993. delete_bonesfile(d_level *lev) 994. { 995. int reslt; 996. 997. (void) set_bonesfile_name(gb.bones, lev); 998. reslt = unlink(fqname(gb.bones, BONESPREFIX, 0)); 999. delete_convertedfile(fqname(gb.bones, BONESPREFIX, 0)); 1000. return !(reslt < 0); 1001. } 1002. 1003. /* assume we're compressing the recently read or created bonesfile, so the 1004. * file name is already set properly */ 1005. void 1006. compress_bonesfile(void) 1007. { 1008. nh_sfconvert(fqname(gb.bones, BONESPREFIX, 0)); 1009. nh_compress(fqname(gb.bones, BONESPREFIX, 0)); 1010. } 1011. #endif /* !SFCTOOL */ 1012. 1013. /* ---------- END BONES FILE HANDLING ----------- */ 1014. 1015. /* ---------- BEGIN SAVE FILE HANDLING ----------- */ 1016. 1017. /* set savefile name in OS-dependent manner from pre-existing svp.plname, 1018. * avoiding troublesome characters */ 1019. void 1020. set_savefile_name(boolean regularize_it) 1021. { 1022. int regoffset = 0, overflow = 0, 1023. indicator_spot = 0; /* 0=no indicator, 1=before ext, 2=after ext */ 1024. const char *postappend = (const char *) 0, 1025. *sfindicator = (const char *) 0; 1026. #if defined(WIN32) 1027. char tmp[BUFSZ]; 1028. #endif 1029. 1030. #ifdef VMS 1031. Sprintf(gs.SAVEF, "[.save]%d%s", getuid(), svp.plname); 1032. regoffset = 7; 1033. indicator_spot = 1; 1034. postappend = ";1"; 1035. #endif 1036. #if defined(WIN32) 1037. if (regularize_it) { 1038. static const char okchars[] 1039. = "*ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz_-."; 1040. const char *legal = okchars; 1041. 1042. ++legal; /* skip '*' wildcard character */ 1043. (void) fname_encode(legal, '%', svp.plname, tmp, sizeof tmp); 1044. } else { 1045. Sprintf(tmp, "%s", svp.plname); 1046. } 1047. if (strlen(tmp) < (SAVESIZE - 1)) 1048. Strcpy(gs.SAVEF, tmp); 1049. else 1050. overflow = 1; 1051. indicator_spot = 1; 1052. regularize_it = FALSE; 1053. #endif 1054. #ifdef UNIX 1055. Sprintf(gs.SAVEF, "save/%d%s", (int) getuid(), svp.plname); 1056. regoffset = 5; 1057. indicator_spot = 2; 1058. #endif 1059. #if defined(MSDOS) 1060. if (strlen(gs.SAVEP) < (SAVESIZE - 1)) 1061. Strcpy(gs.SAVEF, gs.SAVEP); 1062. if (strlen(gs.SAVEF) < (SAVESIZE - 1)) 1063. (void) strncat(gs.SAVEF, svp.plname, (SAVESIZE - strlen(gs.SAVEF))); 1064. #endif 1065. #if defined(MICRO) && !defined(WIN32) && !defined(MSDOS) 1066. if (strlen(gs.SAVEP) < (SAVESIZE - 1)) 1067. Strcpy(gs.SAVEF, gs.SAVEP); 1068. else 1069. #ifdef AMIGA 1070. if (strlen(gs.SAVEP) + strlen(bbs_id) < (SAVESIZE - 1)) 1071. strncat(gs.SAVEF, bbs_id, PATHLEN); 1072. #endif 1073. { 1074. int i = strlen(gs.SAVEP); 1075. #ifdef AMIGA 1076. /* svp.plname has to share space with gs.SAVEP and ".sav" */ 1077. (void) strncat(gs.SAVEF, svp.plname, 1078. FILENAME - i - strlen(SAVE_EXTENSION)); 1079. #else 1080. (void) strncat(gs.SAVEF, svp.plname, 8); 1081. #endif 1082. regoffset = i; 1083. } 1084. #endif /* MICRO */ 1085. 1086. if (regularize_it) 1087. regularize(gs.SAVEF + regoffset); 1088. if (indicator_spot == 1 && sfindicator && !overflow) { 1089. if (strlen(gs.SAVEF) + strlen(sfindicator) < (SAVESIZE - 1)) 1090. Strcat(gs.SAVEF, sfindicator); 1091. else 1092. overflow = 2; 1093. } 1094. #ifdef SAVE_EXTENSION 1095. /* (0) is placed in brackets below so that the [&& !overflow] is 1096. explicit dead code (the ">" comparison is detected as always 1097. FALSE at compile-time). Done to appease clang's -Wunreachable-code */ 1098. if (strlen(SAVE_EXTENSION) > (0) && !overflow) { 1099. if (strlen(gs.SAVEF) + strlen(SAVE_EXTENSION) < (SAVESIZE - 1)) { 1100. Strcat(gs.SAVEF, SAVE_EXTENSION); 1101. } else 1102. overflow = 3; 1103. } 1104. #endif 1105. if (indicator_spot == 2 && sfindicator && !overflow) { 1106. if (strlen(gs.SAVEF) + strlen(sfindicator) < (SAVESIZE - 1)) 1107. Strcat(gs.SAVEF, sfindicator); 1108. else 1109. overflow = 4; 1110. } 1111. if (postappend && !overflow) { 1112. if (strlen(gs.SAVEF) + strlen(postappend) < (SAVESIZE - 1)) 1113. Strcat(gs.SAVEF, postappend); 1114. else 1115. overflow = 5; 1116. } 1117. #if (NH_DEVEL_STATUS != NH_STATUS_RELEASED) 1118. if (overflow) 1119. impossible("set_savefile_name() couldn't complete" 1120. " without overflow %d", 1121. overflow); 1122. #endif 1123. } 1124. 1125. #ifndef SFCTOOL 1126. #ifdef INSURANCE 1127. void 1128. save_savefile_name(NHFILE *nhfp) 1129. { 1130. Sfo_char(nhfp, gs.SAVEF, "savefile_name", sizeof(gs.SAVEF)); 1131. } 1132. #endif 1133. 1134. #ifndef MICRO 1135. /* change pre-existing savefile name to indicate an error savefile */ 1136. void 1137. set_error_savefile(void) 1138. { 1139. #ifdef VMS 1140. { 1141. char *semi_colon = strrchr(gs.SAVEF, ';'); 1142. 1143. if (semi_colon) 1144. *semi_colon = '\0'; 1145. } 1146. Strcat(gs.SAVEF, ".e;1"); 1147. #else 1148. #ifdef MACOS9 1149. Strcat(gs.SAVEF, "-e"); 1150. #else 1151. Strcat(gs.SAVEF, ".e"); 1152. #endif 1153. #endif 1154. } 1155. #endif 1156. 1157. /* create save file, overwriting one if it already exists */ 1158. NHFILE * 1159. create_savefile(void) 1160. { 1161. const char *fq_save; 1162. NHFILE *nhfp = (NHFILE *) 0; 1163. boolean do_historical = TRUE; 1164. 1165. fq_save = fqname(gs.SAVEF, SAVEPREFIX, 0); 1166. nhfp = new_nhfile(); 1167. if (nhfp) { 1168. nhfp->ftype = NHF_SAVEFILE; 1169. nhfp->mode = WRITING; 1170. if (program_state.in_self_recover || do_historical) { 1171. nhUse(do_historical); 1172. nhfp->structlevel = TRUE; 1173. nhfp->fieldlevel = FALSE; 1174. nhfp->addinfo = FALSE; 1175. nhfp->style.deflt = FALSE; 1176. nhfp->style.binary = TRUE; 1177. nhfp->fnidx = historical; 1178. nhfp->fd = -1; 1179. nhfp->fpdef = (FILE *) 0; 1180. #ifdef SAVEFILE_DEBUGGING 1181. nhfp->fplog = fopen("create-savefile.log", "w"); 1182. #endif 1183. #if defined(MICRO) || defined(WIN32) 1184. nhfp->fd = open(fq_save, O_WRONLY | O_BINARY | O_CREAT | O_TRUNC, 1185. FCMASK); 1186. #else /* !MICRO && !WIN32 */ 1187. /* UNIX || MACOS9 implied (MACOS9 is OS9 or earlier only) */ 1188. #ifdef MACOS9 1189. nhfp->fd = maccreat(fq_save, SAVE_TYPE); 1190. #else 1191. nhfp->fd = creat(fq_save, FCMASK); 1192. #endif 1193. #endif /* MICRO || WIN32 */ 1194. #if defined(MSDOS) || defined(WIN32) 1195. if (nhfp->fd >= 0) 1196. (void) setmode(nhfp->fd, O_BINARY); 1197. #endif 1198. } 1199. } 1200. #if defined(VMS) && !defined(SECURE) 1201. /* 1202. Make sure the save file is owned by the current process. That's 1203. the default for non-privileged users, but for priv'd users the 1204. file will be owned by the directory's owner instead of the user. 1205. */ 1206. #undef getuid 1207. (void) chown(fq_save, getuid(), getgid()); 1208. #define getuid() vms_getuid() 1209. #endif /* VMS && !SECURE */ 1210. 1211. nhfp = viable_nhfile(nhfp); 1212. return nhfp; 1213. } 1214. 1215. /* open savefile for reading */ 1216. NHFILE * 1217. open_savefile(void) 1218. { 1219. const char *fq_save; 1220. NHFILE *nhfp = (NHFILE *) 0; 1221. boolean do_historical = TRUE; 1222. 1223. fq_save = fqname(gs.SAVEF, SAVEPREFIX, 0); 1224. nhfp = new_nhfile(); 1225. if (nhfp) { 1226. nhfp->ftype = NHF_SAVEFILE; 1227. nhfp->mode = READING; 1228. if (program_state.in_self_recover || do_historical) { 1229. do_historical = TRUE; /* force it */ 1230. nhUse(do_historical); 1231. nhfp->structlevel = TRUE; 1232. nhfp->fieldlevel = FALSE; 1233. nhfp->addinfo = FALSE; 1234. nhfp->style.deflt = FALSE; 1235. nhfp->style.binary = TRUE; 1236. nhfp->fnidx = historical; 1237. nhfp->fd = -1; 1238. nhfp->fpdef = (FILE *) 0; 1239. #ifdef SAVEFILE_DEBUGGING 1240. nhfp->fplog = fopen("open-savefile.log", "w"); 1241. #endif 1242. } 1243. #ifdef MACOS9 1244. nhfp->fd = macopen(fq_save, O_RDONLY | O_BINARY, SAVE_TYPE); 1245. #else 1246. nhfp->fd = open(fq_save, O_RDONLY | O_BINARY, 0); 1247. #endif 1248. #if defined(MSDOS) || defined(WIN32) 1249. if (nhfp->fd >= 0) 1250. (void) setmode(nhfp->fd, O_BINARY); 1251. #endif 1252. } 1253. nhfp = viable_nhfile(nhfp); 1254. return nhfp; 1255. } 1256. 1257. /* delete savefile */ 1258. int 1259. delete_savefile(void) 1260. { 1261. const char *sfname = fqname(gs.SAVEF, SAVEPREFIX, 0); 1262. 1263. (void) unlink(sfname); 1264. (void) delete_convertedfile(sfname); 1265. return 0; /* for restore_saved_game() (ex-xxxmain.c) test */ 1266. } 1267. 1268. /* try to open up a save file and prepare to restore it */ 1269. NHFILE * 1270. restore_saved_game(void) 1271. { 1272. const char *fq_save; 1273. NHFILE *nhfp = (NHFILE *) 0; 1274. int sfstatus = 0; 1275. 1276. set_savefile_name(TRUE); 1277. fq_save = fqname(gs.SAVEF, SAVEPREFIX, 0); 1278. 1279. nh_uncompress(fq_save); 1280. if ((nhfp = open_savefile()) != 0) { 1281. if ((sfstatus = validate(nhfp, fq_save, FALSE)) != SF_UPTODATE) { 1282. close_nhfile(nhfp); 1283. nhfp = problematic_savefile(sfstatus, fq_save); 1284. } 1285. } 1286. return nhfp; 1287. } 1288. 1289. /* 1290. * This doesn't open any files. It provides a valid (NHFILE *) 1291. * to provide to functions that take one as a parameter, with 1292. * only the FREEING bit set. 1293. * 1294. * close_nhfile() can, and should, be called on the returned 1295. * (NHFILE *), and it will handle it correctly. 1296. */ 1297. 1298. NHFILE * 1299. get_freeing_nhfile(void) 1300. { 1301. NHFILE *nhfp = (NHFILE *) 0; 1302. 1303. nhfp = new_nhfile(); /* also sets fd to -1 */ 1304. if (nhfp) { 1305. nhfp->mode = FREEING; 1306. } 1307. return nhfp; 1308. } 1309. 1310. /* called if there is no save file for current character */ 1311. int 1312. check_panic_save(void) 1313. { 1314. int result = 0; 1315. #ifdef CHECK_PANIC_SAVE 1316. FILE *cf; 1317. const char *savef; 1318. 1319. set_error_savefile(); 1320. savef = fqname(gs.SAVEF, SAVEPREFIX, 0); 1321. 1322. /* 1323. * This duplicates part of docompress_file(). 1324. * We don't want to start by uncompressing just to check for the 1325. * file's existence and then have to recompress it. 1326. */ 1327. 1328. #ifdef COMPRESS_EXTENSION 1329. unsigned ln = (unsigned) (strlen(savef) + strlen(COMPRESS_EXTENSION)); 1330. char *cfn = (char *) alloc(ln + 1); 1331. 1332. Strcpy(cfn, savef); 1333. Strcat(cfn, COMPRESS_EXTENSION); 1334. if ((cf = fopen(cfn, RDBMODE)) != NULL) { 1335. (void) fclose(cf); 1336. result = 1; 1337. } 1338. free((genericptr_t) cfn); 1339. #endif /* COMPRESS_EXTENSION */ 1340. 1341. if (!result) { 1342. /* maybe it has already been manually uncompressed */ 1343. if ((cf = fopen(savef, RDBMODE)) != NULL) { 1344. (void) fclose(cf); 1345. result = 1; 1346. } 1347. } 1348. 1349. set_savefile_name(TRUE); /* reset to normal */ 1350. #endif /* CHECK_PANIC_SAVE */ 1351. return result; 1352. } 1353. 1354. #if defined(SELECTSAVED) 1355. 1356. char * 1357. plname_from_file( 1358. const char *filename, 1359. boolean without_wait_synch_per_file) 1360. { 1361. NHFILE *nhfp; 1362. unsigned ln; 1363. char *result = 0; 1364. int sfstatus = 0; 1365. 1366. Strcpy(gs.SAVEF, filename); 1367. #ifdef COMPRESS_EXTENSION 1368. { 1369. /* if COMPRESS_EXTENSION is present, strip it off */ 1370. int sln = (int) strlen(gs.SAVEF), 1371. xln = (int) strlen(COMPRESS_EXTENSION); 1372. 1373. if (sln > xln && !strcmp(&gs.SAVEF[sln - xln], COMPRESS_EXTENSION)) 1374. gs.SAVEF[sln - xln] = '\0'; 1375. } 1376. #endif 1377. nh_uncompress(gs.SAVEF); 1378. if ((nhfp = open_savefile()) != 0) { 1379. if ((sfstatus = validate(nhfp, filename, 1380. without_wait_synch_per_file)) == SF_UPTODATE) { 1381. /* room for "name+role+race+gend+algn X" where the space before 1382. X is actually NUL and X is playmode: one of '-', 'X', or 'D' */ 1383. ln = (unsigned) PL_NSIZ_PLUS; 1384. result = memset((genericptr_t) alloc(ln), '\0', ln); 1385. get_plname_from_file(nhfp, result, FALSE); 1386. } 1387. close_nhfile(nhfp); 1388. } 1389. nh_compress(gs.SAVEF); 1390. return result; /* file's plname[]+playmode value */ 1391. } 1392. #endif /* defined(SELECTSAVED) */ 1393. 1394. #define SUPPRESS_WAITSYNCH_PERFILE TRUE 1395. #define ALLOW_WAITSYNCH_PERFILE FALSE 1396. 1397. /* get list of saved games owned by current user */ 1398. char ** 1399. get_saved_games(void) 1400. { 1401. char **result = NULL; 1402. #if defined(SELECTSAVED) 1403. #if defined(WIN32) || defined(UNIX) 1404. int n; 1405. #endif 1406. int j = 0; 1407. 1408. #ifdef WIN32 1409. { 1410. char *foundfile; 1411. const char *fq_save; 1412. #if 0 1413. const char *fq_new_save; 1414. const char *fq_old_save; 1415. #endif 1416. char **files = 0; 1417. int i, count_failures = 0; 1418. 1419. Strcpy(svp.plname, "*"); 1420. set_savefile_name(FALSE); 1421. #if defined(ZLIB_COMP) 1422. Strcat(gs.SAVEF, COMPRESS_EXTENSION); 1423. #endif 1424. fq_save = fqname(gs.SAVEF, SAVEPREFIX, 0); 1425. 1426. n = 0; 1427. foundfile = foundfile_buffer(); 1428. if (findfirst((char *) fq_save)) { 1429. do { 1430. ++n; 1431. } while (findnext()); 1432. } 1433. 1434. if (n > 0) { 1435. files = (char **) alloc((n + 1) * sizeof (char *)); /* at most */ 1436. (void) memset((genericptr_t) files, 0, (n + 1) * sizeof (char *)); 1437. if (findfirst((char *) fq_save)) { 1438. i = 0; 1439. do { 1440. files[i++] = dupstr(foundfile); 1441. } while (findnext()); 1442. } 1443. } 1444. 1445. if (n > 0) { 1446. result = (char **) alloc((n + 1) * sizeof (char *)); /* at most */ 1447. (void) memset((genericptr_t) result, 0, (n + 1) * sizeof (char *)); 1448. for(i = 0; i < n; i++) { 1449. char *r; 1450. r = plname_from_file(files[i], SUPPRESS_WAITSYNCH_PERFILE); 1451. 1452. if (r) { 1453. /* this renaming of the savefile is not compatible 1454. * with 1f36b98b, 'selectsaved' extension from 1455. * Oct 10, 2024. Disable the renaming for the time 1456. * being. 1457. */ 1458. #if 0 1459. /* rename file if it is not named as expected */ 1460. Strcpy(svp.plname, r); 1461. set_savefile_name(TRUE); 1462. fq_new_save = fqname(gs.SAVEF, SAVEPREFIX, 0); 1463. fq_old_save = fqname(files[i], SAVEPREFIX, 1); 1464. 1465. if (strcmp(fq_old_save, fq_new_save) != 0 1466. && !file_exists(fq_new_save)) 1467. (void) rename(fq_old_save, fq_new_save); 1468. #endif 1469. result[j++] = r; 1470. } else { 1471. count_failures++; 1472. } 1473. } 1474. } 1475. 1476. free_saved_games(files); 1477. if (count_failures) 1478. wait_synch(); 1479. } 1480. #endif /* WIN32 */ 1481. #ifdef UNIX 1482. /* posixly correct version */ 1483. int myuid = getuid(); 1484. DIR *dir; 1485. 1486. if ((dir = opendir(fqname("save", SAVEPREFIX, 0)))) { 1487. for (n = 0; readdir(dir); n++) 1488. ; 1489. closedir(dir); 1490. if (n > 0) { 1491. int i; 1492. 1493. if (!(dir = opendir(fqname("save", SAVEPREFIX, 0)))) 1494. return 0; 1495. result = (char **) alloc((n + 1) * sizeof(char *)); /* at most */ 1496. (void) memset((genericptr_t) result, 0, (n + 1) * sizeof(char *)); 1497. for (i = 0, j = 0; i < n; i++) { 1498. int uid; 1499. char name[64]; /* more than PL_NSIZ+1 */ 1500. struct dirent *entry = readdir(dir); 1501. 1502. if (!entry) 1503. break; 1504. if (sscanf(entry->d_name, "%d%63s", &uid, name) == 2) { 1505. if (uid == myuid) { 1506. char filename[BUFSZ]; 1507. char *r; 1508. 1509. Sprintf(filename, "save/%d%s", uid, name); 1510. r = plname_from_file(filename, 1511. ALLOW_WAITSYNCH_PERFILE); 1512. if (r) 1513. result[j++] = r; 1514. } 1515. } 1516. } 1517. closedir(dir); 1518. } 1519. } 1520. #endif /* UNIX */ 1521. #ifdef VMS 1522. Strcpy(svp.plname, "*"); 1523. set_savefile_name(FALSE); 1524. j = vms_get_saved_games(gs.SAVEF, &result); 1525. #endif /* VMS */ 1526. 1527. if (j > 0) { 1528. if (j > 1) 1529. qsort(result, j, sizeof (char *), strcmp_wrap); 1530. result[j] = (char *) NULL; 1531. } else if (result) { /* could happen if save files are obsolete */ 1532. free_saved_games(result); 1533. result = (char **) NULL; 1534. } 1535. #endif /* SELECTSAVED */ 1536. 1537. return result; 1538. } 1539. #undef SUPPRESS_WAITSYNCH_PERFILE 1540. #undef ALLOW_WAITSYNCH_PERFILE 1541. 1542. void 1543. free_saved_games(char **saved) 1544. { 1545. if (saved) { 1546. int i; 1547. 1548. for (i = 0; saved[i]; ++i) 1549. free((genericptr_t) saved[i]); 1550. free((genericptr_t) saved); 1551. } 1552. } 1553. 1554. #endif /* !SFCTOOL */ 1555. 1556. /* ---------- END SAVE FILE HANDLING ----------- */ 1557. 1558. /* ---------- BEGIN FILE COMPRESSION HANDLING ----------- */ 1559. 1560. #ifdef COMPRESS /* external compression */ 1561. 1562. staticfn void 1563. redirect( 1564. const char *filename, 1565. const char *mode, 1566. FILE *stream, 1567. boolean uncomp) 1568. { 1569. if (freopen(filename, mode, stream) == (FILE *) 0) { 1570. const char *details; 1571. 1572. #if defined(NHSTDC) && !defined(NOTSTDC) 1573. if ((details = strerror(errno)) == 0) 1574. #endif 1575. details = ""; 1576. (void) fprintf(stderr, 1577. "freopen of %s for %scompress failed; (%d) %s\n", 1578. filename, uncomp ? "un" : "", errno, details); 1579. nh_terminate(EXIT_FAILURE); 1580. } 1581. } 1582. 1583. /* 1584. * using system() is simpler, but opens up security holes and causes 1585. * problems on at least Interactive UNIX 3.0.1 (SVR3.2), where any 1586. * setuid is renounced by /bin/sh, so the files cannot be accessed. 1587. * 1588. * cf. child() in unixunix.c. 1589. */ 1590. staticfn void 1591. docompress_file(const char *filename, boolean uncomp) 1592. { 1593. char *cfn = 0; 1594. const char *xtra; 1595. FILE *cf; 1596. const char *args[10]; 1597. #ifdef COMPRESS_OPTIONS 1598. char opts[sizeof COMPRESS_OPTIONS]; 1599. #endif 1600. int i = 0; 1601. int f, childstatus; 1602. unsigned ln; 1603. #ifdef TTY_GRAPHICS 1604. boolean istty = WINDOWPORT(tty); 1605. #endif 1606. 1607. #ifdef COMPRESS_EXTENSION 1608. xtra = COMPRESS_EXTENSION; 1609. #else 1610. xtra = ""; 1611. #endif 1612. #ifdef SFCTOOL 1613. ln = strlen(filename) + sizeof COMPRESS_EXTENSION; 1614. cfn = (char *) alloc(ln); 1615. #else /* SFCTOOL */ 1616. ln = (unsigned) (strlen(filename) + strlen(xtra)); 1617. cfn = (char *) alloc(ln + 1); 1618. #endif /* SFCTOOL */ 1619. 1620. Strcpy(cfn, filename); 1621. Strcat(cfn, xtra); 1622. 1623. /* when compressing, we know the file exists */ 1624. if (uncomp) { 1625. if ((cf = fopen(cfn, RDBMODE)) == (FILE *) 0) { 1626. free((genericptr_t) cfn); 1627. return; 1628. } 1629. (void) fclose(cf); 1630. } 1631. 1632. args[0] = COMPRESS; 1633. if (uncomp) 1634. args[++i] = "-d"; /* uncompress */ 1635. #ifdef COMPRESS_OPTIONS 1636. { 1637. /* we can't guarantee there's only one additional option, sigh */ 1638. char *opt; 1639. boolean inword = FALSE; 1640. 1641. opt = strcpy(opts, COMPRESS_OPTIONS); 1642. while (*opt) { 1643. if ((*opt == ' ') || (*opt == '\t')) { 1644. if (inword) { 1645. *opt = '\0'; 1646. inword = FALSE; 1647. } 1648. } else if (!inword) { 1649. args[++i] = opt; 1650. inword = TRUE; 1651. } 1652. opt++; 1653. } 1654. } 1655. #endif 1656. args[++i] = (char *) 0; 1657. 1658. #ifdef TTY_GRAPHICS 1659. /* If we don't do this and we are right after a y/n question *and* 1660. * there is an error message from the compression, the 'y' or 'n' can 1661. * end up being displayed after the error message. 1662. */ 1663. if (istty) { 1664. mark_synch(); 1665. } 1666. #endif 1667. f = fork(); 1668. if (f == 0) { /* child */ 1669. #ifdef TTY_GRAPHICS 1670. /* any error messages from the compression must come out after 1671. * the first line, because the more() to let the user read 1672. * them will have to clear the first line. This should be 1673. * invisible if there are no error messages. 1674. */ 1675. if (istty) { 1676. raw_print(""); 1677. } 1678. #endif 1679. /* run compressor without privileges, in case other programs 1680. * have surprises along the line of gzip once taking filenames 1681. * in GZIP. 1682. */ 1683. /* assume all compressors will compress stdin to stdout 1684. * without explicit filenames. this is true of at least 1685. * compress and gzip, those mentioned in config.h. 1686. */ 1687. if (uncomp) { 1688. redirect(cfn, RDBMODE, stdin, uncomp); 1689. redirect(filename, WRBMODE, stdout, uncomp); 1690. } else { 1691. redirect(filename, RDBMODE, stdin, uncomp); 1692. redirect(cfn, WRBMODE, stdout, uncomp); 1693. } 1694. (void) setgid(getgid()); 1695. (void) setuid(getuid()); 1696. (void) execv(args[0], (char *const *) args); 1697. perror((char *) 0); 1698. (void) fprintf(stderr, "Exec to %scompress %s failed.\n", 1699. uncomp ? "un" : "", filename); 1700. free((genericptr_t) cfn); 1701. nh_terminate(EXIT_FAILURE); 1702. } else if (f == -1) { 1703. perror((char *) 0); 1704. pline("Fork to %scompress %s failed.", uncomp ? "un" : "", filename); 1705. free((genericptr_t) cfn); 1706. return; 1707. } 1708. 1709. /* 1710. * back in parent... 1711. */ 1712. #ifndef NO_SIGNAL 1713. childstatus = 1; /* wait() should update this, ideally setting it to 0 */ 1714. (void) signal(SIGINT, SIG_IGN); 1715. (void) signal(SIGQUIT, SIG_IGN); 1716. errno = 0; /* avoid stale details if wait() doesn't set errno */ 1717. /* wait() returns child's pid and sets 'childstatus' to child's 1718. exit status, or returns -1 and leaves 'childstatus' unmodified */ 1719. if ((long) wait((int *) &childstatus) == -1L) { 1720. char numbuf[40]; 1721. const char *details = strerror(errno); 1722. 1723. if (!details) { 1724. Sprintf(numbuf, "(%d)", errno); 1725. details = numbuf; 1726. } 1727. raw_printf("Wait when %scompressing %s failed; %s.", 1728. uncomp ? "un" : "", filename, details); 1729. } 1730. (void) signal(SIGINT, (SIG_RET_TYPE) done1); 1731. if (wizard) 1732. (void) signal(SIGQUIT, SIG_DFL); 1733. #else 1734. /* I don't think we can really cope with external compression 1735. * without signals, so we'll declare that compress failed and 1736. * go on. (We could do a better job by forcing off external 1737. * compression if there are no signals, but we want this for 1738. * testing with FailSafeC 1739. */ 1740. childstatus = 1; /* non-zero => failure */ 1741. #endif 1742. if (childstatus == 0) { 1743. /* (un)compress succeeded: remove file left behind */ 1744. if (uncomp) 1745. (void) unlink(cfn); 1746. else 1747. (void) unlink(filename); 1748. } else { 1749. /* (un)compress failed; remove the new, bad file */ 1750. if (uncomp) { 1751. raw_printf("Unable to uncompress %s", filename); 1752. (void) unlink(filename); 1753. } else { 1754. /* no message needed for compress case; life will go on */ 1755. (void) unlink(cfn); 1756. } 1757. #ifdef TTY_GRAPHICS 1758. /* Give them a chance to read any error messages from the 1759. * compression--these would go to stdout or stderr and would get 1760. * overwritten only in tty mode. It's still ugly, since the 1761. * messages are being written on top of the screen, but at least 1762. * the user can read them. 1763. */ 1764. if (istty && iflags.window_inited) { 1765. clear_nhwindow(WIN_MESSAGE); 1766. more(); 1767. /* No way to know if this is feasible */ 1768. /* doredraw(); */ 1769. } 1770. #endif 1771. } 1772. 1773. free((genericptr_t) cfn); 1774. return; 1775. } 1776. 1777. #endif /* COMPRESS : external compression */ 1778. 1779. #if defined(COMPRESS) || defined(ZLIB_COMP) 1780. #define UNUSED_if_not_COMPRESS /*empty*/ 1781. #else 1782. #define UNUSED_if_not_COMPRESS UNUSED 1783. #endif 1784. 1785. /* compress file */ 1786. void 1787. nh_compress(const char *filename UNUSED_if_not_COMPRESS) 1788. { 1789. #if defined(COMPRESS) || defined(ZLIB_COMP) 1790. docompress_file(filename, FALSE); 1791. #endif 1792. } 1793. 1794. /* uncompress file if it exists */ 1795. void 1796. nh_uncompress(const char *filename UNUSED_if_not_COMPRESS) 1797. { 1798. #if defined(COMPRESS) || defined(ZLIB_COMP) 1799. docompress_file(filename, TRUE); 1800. #endif 1801. } 1802. 1803. #ifdef ZLIB_COMP /* RLC 09 Mar 1999: Support internal ZLIB */ 1804. staticfn boolean 1805. make_compressed_name(const char *filename, char *cfn) 1806. { 1807. #ifndef SHORT_FILENAMES 1808. /* Assume free-form filename with no 8.3 restrictions */ 1809. strcpy(cfn, filename); 1810. strcat(cfn, COMPRESS_EXTENSION); 1811. return TRUE; 1812. #else 1813. #ifdef SAVE_EXTENSION 1814. char *bp = (char *) 0; 1815. 1816. strcpy(cfn, filename); 1817. if ((bp = strstri(cfn, SAVE_EXTENSION))) { 1818. strsubst(bp, SAVE_EXTENSION, ".saz"); 1819. return TRUE; 1820. } else { 1821. /* find last occurrence of bon */ 1822. bp = eos(cfn); 1823. while (bp-- > cfn) { 1824. if (strstri(bp, "bon")) { 1825. strsubst(bp, "bon", "boz"); 1826. return TRUE; 1827. } 1828. } 1829. } 1830. #endif /* SAVE_EXTENSION */ 1831. return FALSE; 1832. #endif /* SHORT_FILENAMES */ 1833. } 1834. 1835. staticfn void 1836. docompress_file(const char *filename, boolean uncomp) 1837. { 1838. gzFile compressedfile; 1839. FILE *uncompressedfile; 1840. #ifndef SFCTOOL 1841. char cfn[256]; 1842. #else 1843. char *cfn; 1844. #endif 1845. char buf[1024]; 1846. unsigned len, len2; 1847. 1848. #ifdef SFCTOOL 1849. cfn = (char *) alloc(strlen(filename) + strlen(COMPRESS_EXTENSION) + 1); 1850. #endif 1851. if (!make_compressed_name(filename, cfn)) 1852. return; 1853. 1854. if (!uncomp) { 1855. /* Open the input and output files */ 1856. /* Note that gzopen takes "wb" as its mode, even on systems where 1857. fopen takes "r" and "w" */ 1858. 1859. uncompressedfile = fopen(filename, RDBMODE); 1860. if (!uncompressedfile) { 1861. pline("Error in zlib docompress_file %s", filename); 1862. return; 1863. } 1864. compressedfile = gzopen(cfn, "wb"); 1865. if (compressedfile == NULL) { 1866. if (errno == 0) { 1867. pline("zlib failed to allocate memory"); 1868. } else { 1869. panic("Error in docompress_file %d", errno); 1870. } 1871. #ifdef SFCTOOL 1872. free(cfn); 1873. #endif 1874. fclose(uncompressedfile); 1875. return; 1876. } 1877. 1878. /* Copy from the uncompressed to the compressed file */ 1879. 1880. while (1) { 1881. len = fread(buf, 1, sizeof(buf), uncompressedfile); 1882. if (ferror(uncompressedfile)) { 1883. pline("Failure reading uncompressed file"); 1884. pline("Can't compress %s.", filename); 1885. fclose(uncompressedfile); 1886. gzclose(compressedfile); 1887. (void) unlink(cfn); 1888. #ifdef SFCTOOL 1889. free(cfn); 1890. #endif 1891. return; 1892. } 1893. if (len == 0) 1894. break; /* End of file */ 1895. 1896. len2 = gzwrite(compressedfile, buf, len); 1897. if (len2 == 0) { 1898. pline("Failure writing compressed file"); 1899. pline("Can't compress %s.", filename); 1900. fclose(uncompressedfile); 1901. gzclose(compressedfile); 1902. (void) unlink(cfn); 1903. #ifdef SFCTOOL 1904. free(cfn); 1905. #endif 1906. return; 1907. } 1908. } 1909. 1910. fclose(uncompressedfile); 1911. gzclose(compressedfile); 1912. 1913. /* Delete the file left behind */ 1914. 1915. (void) unlink(filename); 1916. 1917. } else { /* uncomp */ 1918. 1919. /* Open the input and output files */ 1920. /* Note that gzopen takes "rb" as its mode, even on systems where 1921. fopen takes "r" and "w" */ 1922. 1923. compressedfile = gzopen(cfn, "rb"); 1924. if (compressedfile == NULL) { 1925. if (errno == 0) { 1926. pline("zlib failed to allocate memory"); 1927. } else if (errno != ENOENT) { 1928. panic("Error in zlib docompress_file %s, %d", filename, 1929. errno); 1930. } 1931. #ifdef SFCTOOL 1932. free(cfn); 1933. #endif 1934. return; 1935. } 1936. uncompressedfile = fopen(filename, WRBMODE); 1937. if (!uncompressedfile) { 1938. pline("Error in zlib docompress file uncompress %s", filename); 1939. gzclose(compressedfile); 1940. #ifdef SFCTOOL 1941. free(cfn); 1942. #endif 1943. return; 1944. } 1945. 1946. /* Copy from the compressed to the uncompressed file */ 1947. 1948. while (1) { 1949. len = gzread(compressedfile, buf, sizeof(buf)); 1950. if (len == (unsigned) -1) { 1951. pline("Failure reading compressed file"); 1952. pline("Can't uncompress %s.", filename); 1953. fclose(uncompressedfile); 1954. gzclose(compressedfile); 1955. (void) unlink(filename); 1956. #ifdef SFCTOOL 1957. free(cfn); 1958. #endif 1959. return; 1960. } 1961. if (len == 0) 1962. break; /* End of file */ 1963. 1964. fwrite(buf, 1, len, uncompressedfile); 1965. if (ferror(uncompressedfile)) { 1966. pline("Failure writing uncompressed file"); 1967. pline("Can't uncompress %s.", filename); 1968. fclose(uncompressedfile); 1969. gzclose(compressedfile); 1970. (void) unlink(filename); 1971. #ifdef SFCTOOL 1972. free(cfn); 1973. #endif 1974. return; 1975. } 1976. } 1977. 1978. fclose(uncompressedfile); 1979. gzclose(compressedfile); 1980. 1981. /* Delete the file left behind */ 1982. (void) unlink(cfn); 1983. } 1984. #ifdef SFCTOOL 1985. free(cfn); 1986. #endif 1987. } 1988. #endif /* RLC 09 Mar 1999: End ZLIB patch */ 1989. 1990. #undef UNUSED_if_not_COMPRESS 1991. 1992. /* ---------- END FILE COMPRESSION HANDLING ----------- */ 1993. 1994. 1995. /* ---------- BEGIN PROBLEMATIC SAVEFILE HANDLING ----------- */ 1996. #ifndef SFCTOOL 1997. static struct sfstatus_to_msg { 1998. int sfstatus; 1999. const char *msg; 2000. } sf2msg[] = { 2001. { SF_UPTODATE, "everything matches" }, 2002. { SF_OUTDATED, "outdated savefile" }, 2003. { SF_CRITICAL_BYTE_COUNT_MISMATCH, 2004. "savefile critical byte-count mismatch" }, 2005. { SF_DM_IL32LLP64_ON_ILP32LL64, "Windows x64 savefile on x86" }, 2006. { SF_DM_I32LP64_ON_ILP32LL64, "Unix 64 savefile on x86" }, 2007. { SF_DM_ILP32LL64_ON_I32LP64, "x86 savefile on Unix 64" }, 2008. { SF_DM_ILP32LL64_ON_IL32LLP64, "x86 savefile on Windows x64" }, 2009. { SF_DM_I32LP64_ON_IL32LLP64, "Unix 64 savefile on Windows x64" }, 2010. { SF_DM_IL32LLP64_ON_I32LP64, "Windows x64 savefile on Unix 64" }, 2011. { SF_DM_MISMATCH, "generic savefile mismatch" }, 2012. }; 2013. 2014. staticfn NHFILE * 2015. problematic_savefile(int sfstatus, const char *savefilenm) 2016. { 2017. int i; 2018. NHFILE *nhfp = (NHFILE *) 0; 2019. 2020. switch (sfstatus) { 2021. case SF_UPTODATE: 2022. break; 2023. case SF_DM_IL32LLP64_ON_ILP32LL64: 2024. case SF_DM_I32LP64_ON_ILP32LL64: 2025. case SF_DM_ILP32LL64_ON_I32LP64: 2026. case SF_DM_ILP32LL64_ON_IL32LLP64: 2027. case SF_DM_I32LP64_ON_IL32LLP64: 2028. case SF_DM_IL32LLP64_ON_I32LP64: 2029. FALLTHROUGH; 2030. /*FALLTHRU*/ 2031. case SF_DM_MISMATCH: 2032. case SF_OUTDATED: 2033. case SF_CRITICAL_BYTE_COUNT_MISMATCH: 2034. default: 2035. for (i = 0; i < SIZE(sf2msg); ++i) { 2036. if (sf2msg[i].sfstatus == sfstatus) { 2037. raw_printf("\n%s is %s %s\n", 2038. savefilenm, 2039. (sfstatus == SF_OUTDATED) ? "an" : "a", 2040. sf2msg[i].msg); 2041. break; 2042. } 2043. } 2044. } 2045. return nhfp; 2046. } 2047. #endif /* !SFCTOOL */ 2048. 2049. /* ---------- END PROBLEMATIC SAVEFILE HANDLING ----------- */ 2050. 2051. /* ---------- BEGIN EXTERNAL CONVERSION HANDLING ----------- */ 2052. 2053. static boolean cvtinit = FALSE; 2054. 2055. #ifndef SFCTOOL 2056. static char *unconverted_filename = 0, *converted_filename = 0; 2057. 2058. /* 2059. * Returns non-zero if unconvert was successful 2060. */ 2061. staticfn int 2062. doconvert_file(const char *filename, int sfstatus, boolean unconvert) 2063. { 2064. nhUse(filename); 2065. nhUse(sfstatus); 2066. nhUse(unconvert); 2067. return 1; 2068. } 2069. 2070. /* convert file */ 2071. void 2072. nh_sfconvert(const char *filename) 2073. { 2074. (void) doconvert_file(filename, 0, FALSE); 2075. } 2076. 2077. /* unconvert file if it exists */ 2078. void 2079. nh_sfunconvert(const char *filename) 2080. { 2081. (void) doconvert_file(filename, 0, TRUE); 2082. } 2083. 2084. #else /* !SFCTOOL */ 2085. /* in sfctool, these are in sfctool.c, not in here */ 2086. extern char *unconverted_filename, *converted_filename; 2087. #endif /* !SFCTOOL */ 2088. 2089. staticfn boolean 2090. make_converted_name(const char *filename) 2091. { 2092. unsigned ln; 2093. const char *xtra, *finaldirchar; 2094. const char *dir = NULL; 2095. boolean needsep = FALSE; 2096. #if defined(WIN32) 2097. static char folderbuf[MAX_PATH]; 2098. #endif 2099. 2100. if (!filename) 2101. return FALSE; 2102. 2103. if (unconverted_filename) 2104. free((genericptr_t) unconverted_filename), unconverted_filename = 0; 2105. if (converted_filename) 2106. free((genericptr_t) converted_filename), converted_filename = 0; 2107. 2108. #ifndef SHORT_FILENAMES 2109. /* do we need to do some ms-dos processing here? */ 2110. #endif /* SHORT_FILENAMES */ 2111. 2112. ln = (unsigned) strlen(filename); 2113. if (!contains_directory(filename)) { 2114. #if defined(UNIX) 2115. dir = nh_getenv("NETHACKDIR"); 2116. if (!dir) 2117. dir = nh_getenv("HACKDIR"); 2118. #ifdef HACKDIR 2119. if (!dir) 2120. dir = HACKDIR; 2121. #endif 2122. #elif defined(WIN32) 2123. if (get_user_home_folder(folderbuf, sizeof folderbuf)) { 2124. size_t sz = strlen(folderbuf); 2125. 2126. Snprintf(eos(folderbuf), sizeof folderbuf - sz, 2127. "\\AppData\\Local\\NetHack\\5.0\\"); 2128. dir = (const char *) folderbuf; 2129. } 2130. #endif /* UNIX || WIN32 */ 2131. if (dir) { 2132. finaldirchar = c_eos(dir); 2133. finaldirchar--; 2134. if (!(*finaldirchar == '/' || *finaldirchar == '\\' 2135. || *finaldirchar == ':')) { 2136. needsep = TRUE; 2137. ln += 1; 2138. } 2139. ln += strlen(dir); 2140. } 2141. } 2142. unconverted_filename = (char *) alloc(ln + 1); 2143. Snprintf(unconverted_filename, ln + 1, "%s%s%s", 2144. dir ? dir : "", 2145. (dir && needsep) ? "/" : "", 2146. filename); 2147. xtra = ".exportascii"; 2148. ln += (unsigned) strlen(xtra); 2149. converted_filename = (char *) alloc(ln + 1); 2150. Strcpy(converted_filename, unconverted_filename); 2151. Strcat(converted_filename, xtra); 2152. return TRUE; 2153. } 2154. 2155. /* delete converted savefile as a normal course of action */ 2156. int 2157. delete_convertedfile(const char *basefilename) 2158. { 2159. if (!converted_filename) 2160. make_converted_name(basefilename); 2161. if (converted_filename) { 2162. (void) unlink(converted_filename); 2163. } 2164. return 0; 2165. } 2166. 2167. void 2168. free_convert_filenames(void) 2169. { 2170. if (converted_filename) 2171. free((genericptr_t) converted_filename), converted_filename = 0; 2172. if (unconverted_filename) 2173. free((genericptr_t) unconverted_filename), unconverted_filename = 0; 2174. cvtinit = FALSE; 2175. } 2176. 2177. /* return TRUE if s contains a directory, not just a filespec */ 2178. boolean 2179. contains_directory(const char *s) 2180. { 2181. int i, slen = strlen(s); 2182. const char *cp = s; 2183. 2184. for (i = 0; i < slen; ++i) { 2185. if (*cp == '\\' || *cp == '/' || *cp == ':') 2186. return TRUE; 2187. cp++; 2188. } 2189. return FALSE; 2190. } 2191. 2192. /* =========================================================================*/ 2193. 2194. /* ---------- END EXTERNAL CONVERSION HANDLING ----------- */ 2195. 2196. /* ---------- BEGIN FILE LOCKING HANDLING ----------- */ 2197. 2198. #ifndef SFCTOOL 2199. 2200. #if defined(NO_FILE_LINKS) || defined(USE_FCNTL) /* implies UNIX */ 2201. static int lockfd = -1; /* for lock_file() to pass to unlock_file() */ 2202. #endif 2203. #ifdef USE_FCNTL 2204. static struct flock sflock; /* for unlocking, same as above */ 2205. #endif 2206. 2207. #if defined(HANGUPHANDLING) 2208. #define HUP if (!program_state.done_hup) 2209. #else 2210. #define HUP 2211. #endif 2212. 2213. 2214. #if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(WIN32) \ 2215. || defined(MSDOS) 2216. #define UNUSED_conditional /*empty*/ 2217. #else 2218. #define UNUSED_conditional UNUSED 2219. #endif 2220. 2221. 2222. #ifndef USE_FCNTL 2223. staticfn char * 2224. make_lockname(const char *filename UNUSED_conditional, char *lockname) 2225. { 2226. #if defined(UNIX) || defined(VMS) || defined(AMIGA) || defined(WIN32) \ 2227. || defined(MSDOS) 2228. #ifdef NO_FILE_LINKS 2229. Strcpy(lockname, LOCKDIR); 2230. Strcat(lockname, "/"); 2231. Strcat(lockname, filename); 2232. #else 2233. Strcpy(lockname, filename); 2234. #endif 2235. #ifdef VMS 2236. { 2237. char *semi_colon = strrchr(lockname, ';'); 2238. if (semi_colon) 2239. *semi_colon = '\0'; 2240. } 2241. Strcat(lockname, ".lock;1"); 2242. #else 2243. Strcat(lockname, "_lock"); 2244. #endif 2245. return lockname; 2246. #else /* !(UNIX || VMS || AMIGA || WIN32 || MSDOS) */ 2247. lockname[0] = '\0'; 2248. return (char *) 0; 2249. #endif 2250. } 2251. #endif /* !USE_FCNTL */ 2252. 2253. /* lock a file */ 2254. boolean 2255. lock_file(const char *filename, int whichprefix, 2256. int retryct UNUSED_conditional) 2257. { 2258. #ifndef USE_FCNTL 2259. char locknambuf[BUFSZ]; 2260. const char *lockname; 2261. #endif 2262. 2263. gn.nesting++; 2264. if (gn.nesting > 1) { 2265. impossible("TRIED TO NEST LOCKS"); 2266. return TRUE; 2267. } 2268. 2269. #ifndef USE_FCNTL 2270. lockname = make_lockname(filename, locknambuf); 2271. #ifndef NO_FILE_LINKS /* LOCKDIR should be subsumed by LOCKPREFIX */ 2272. lockname = fqname(lockname, LOCKPREFIX, 2); 2273. #endif 2274. #endif 2275. filename = fqname(filename, whichprefix, 0); 2276. #ifdef USE_FCNTL 2277. lockfd = open(filename, O_RDWR); 2278. if (lockfd == -1) { 2279. HUP raw_printf("Cannot open file %s. " 2280. " Is NetHack installed correctly?", 2281. filename); 2282. gn.nesting--; 2283. return FALSE; 2284. } 2285. sflock.l_type = F_WRLCK; 2286. sflock.l_whence = SEEK_SET; 2287. sflock.l_start = 0; 2288. sflock.l_len = 0; 2289. #endif 2290. 2291. #if defined(UNIX) || defined(VMS) 2292. #ifdef USE_FCNTL 2293. while (fcntl(lockfd, F_SETLK, &sflock) == -1) { 2294. #else 2295. #ifdef NO_FILE_LINKS 2296. while ((lockfd = open(lockname, O_RDWR | O_CREAT | O_EXCL, 0666)) == -1) { 2297. #else 2298. while (link(filename, lockname) == -1) { 2299. #endif 2300. #endif 2301. 2302. #ifdef USE_FCNTL 2303. if (retryct--) { 2304. HUP raw_printf("Waiting for release of fcntl lock on %s. " 2305. " (%d retries left.)", 2306. filename, retryct); 2307. sleep(1); 2308. } else { 2309. HUP raw_print("I give up. Sorry."); 2310. HUP raw_printf("Some other process has an unnatural grip on %s.", 2311. filename); 2312. gn.nesting--; 2313. return FALSE; 2314. } 2315. #else 2316. int errnosv = errno; 2317. 2318. switch (errnosv) { /* George Barbanis */ 2319. case EEXIST: 2320. if (retryct--) { 2321. HUP raw_printf("Waiting for access to %s. " 2322. " (%d retries left).", 2323. filename, retryct); 2324. #if defined(SYSV) || defined(ULTRIX) || defined(VMS) 2325. (void) 2326. #endif 2327. sleep(1); 2328. } else { 2329. HUP raw_print("I give up. Sorry."); 2330. HUP raw_printf("Perhaps there is an old %s around?", 2331. lockname); 2332. gn.nesting--; 2333. return FALSE; 2334. } 2335. 2336. break; 2337. case ENOENT: 2338. HUP raw_printf("Can't find file %s to lock!", filename); 2339. gn.nesting--; 2340. return FALSE; 2341. case EACCES: 2342. HUP raw_printf("No write permission to lock %s!", filename); 2343. gn.nesting--; 2344. return FALSE; 2345. #ifdef VMS /* c__translate(vmsfiles.c) */ 2346. case EPERM: 2347. /* could be misleading, but usually right */ 2348. HUP raw_printf("Can't lock %s due to directory protection.", 2349. filename); 2350. gn.nesting--; 2351. return FALSE; 2352. #endif 2353. case EROFS: 2354. /* take a wild guess at the underlying cause */ 2355. HUP perror(lockname); 2356. HUP raw_printf("Cannot lock %s.", filename); 2357. HUP raw_printf("(Perhaps you are running NetHack from" 2358. " inside the distribution package?)."); 2359. gn.nesting--; 2360. return FALSE; 2361. default: 2362. HUP perror(lockname); 2363. HUP raw_printf("Cannot lock %s for unknown reason (%d).", 2364. filename, errnosv); 2365. gn.nesting--; 2366. return FALSE; 2367. } 2368. #endif /* USE_FCNTL */ 2369. } 2370. #endif /* UNIX || VMS */ 2371. 2372. #if (defined(AMIGA) || defined(WIN32) || defined(MSDOS)) \ 2373. && !defined(USE_FCNTL) 2374. #ifdef AMIGA 2375. #define OPENFAILURE(fd) (!fd) 2376. gl.lockptr = 0; 2377. #else 2378. #define OPENFAILURE(fd) (fd < 0) 2379. gl.lockptr = -1; 2380. #endif 2381. while (--retryct && OPENFAILURE(gl.lockptr)) { 2382. #if defined(WIN32) && !defined(WIN_CE) 2383. gl.lockptr = sopen(lockname, O_RDWR | O_CREAT, SH_DENYRW, S_IWRITE); 2384. #else 2385. (void) DeleteFile(lockname); /* in case dead process was here first */ 2386. #ifdef AMIGA 2387. gl.lockptr = Open(lockname, MODE_NEWFILE); 2388. #else 2389. gl.lockptr = open(lockname, O_RDWR | O_CREAT | O_EXCL, S_IWRITE); 2390. #endif 2391. #endif 2392. if (OPENFAILURE(gl.lockptr)) { 2393. raw_printf("Waiting for access to %s. (%d retries left).", 2394. filename, retryct); 2395. Delay(50); 2396. } 2397. } 2398. if (!retryct) { 2399. raw_printf("I give up. Sorry."); 2400. gn.nesting--; 2401. return FALSE; 2402. } 2403. #endif /* AMIGA || WIN32 || MSDOS */ 2404. return TRUE; 2405. } 2406. 2407. #ifdef VMS /* for unlock_file, use the unlink() routine in vmsunix.c */ 2408. #ifdef unlink 2409. #undef unlink 2410. #endif 2411. #define unlink(foo) vms_unlink(foo) 2412. #endif 2413. 2414. /* unlock file, which must be currently locked by lock_file */ 2415. void 2416. unlock_file(const char *filename) 2417. { 2418. #ifndef USE_FCNTL 2419. char locknambuf[BUFSZ]; 2420. const char *lockname; 2421. #endif 2422. 2423. if (gn.nesting == 1) { 2424. #ifdef USE_FCNTL 2425. sflock.l_type = F_UNLCK; 2426. if (lockfd >= 0) { 2427. if (fcntl(lockfd, F_SETLK, &sflock) == -1) 2428. HUP raw_printf("Can't remove fcntl lock on %s.", filename); 2429. (void) close(lockfd), lockfd = -1; 2430. } 2431. #else 2432. lockname = make_lockname(filename, locknambuf); 2433. #ifndef NO_FILE_LINKS /* LOCKDIR should be subsumed by LOCKPREFIX */ 2434. lockname = fqname(lockname, LOCKPREFIX, 2); 2435. #endif 2436. 2437. #if defined(UNIX) || defined(VMS) 2438. if (unlink(lockname) < 0) 2439. HUP raw_printf("Can't unlink %s.", lockname); 2440. #ifdef NO_FILE_LINKS 2441. (void) nhclose(lockfd), lockfd = -1; 2442. #endif 2443. 2444. #endif /* UNIX || VMS */ 2445. 2446. #if defined(AMIGA) || defined(WIN32) || defined(MSDOS) 2447. if (gl.lockptr) 2448. Close(gl.lockptr); 2449. DeleteFile(lockname); 2450. gl.lockptr = 0; 2451. #endif /* AMIGA || WIN32 || MSDOS */ 2452. #endif /* USE_FCNTL */ 2453. } 2454. 2455. gn.nesting--; 2456. } 2457. 2458. #undef UNUSED_conditional 2459. 2460. /* ---------- END FILE LOCKING HANDLING ----------- */ 2461. 2462. /* ---------- BEGIN WIZKIT FILE HANDLING ----------- */ 2463. 2464. staticfn FILE * 2465. fopen_wizkit_file(void) 2466. { 2467. FILE *fp; 2468. #if defined(VMS) || defined(UNIX) 2469. char tmp_wizkit[BUFSZ]; 2470. #endif 2471. char *envp; 2472. 2473. envp = nh_getenv("WIZKIT"); 2474. if (envp && *envp) 2475. (void) strncpy(gw.wizkit, envp, WIZKIT_MAX - 1); 2476. if (!gw.wizkit[0]) 2477. return (FILE *) 0; 2478. 2479. #ifdef UNIX 2480. if (access(gw.wizkit, 4) == -1) { 2481. /* 4 is R_OK on newer systems */ 2482. /* nasty sneaky attempt to read file through 2483. * NetHack's setuid permissions -- this is a 2484. * place a file name may be wholly under the player's 2485. * control 2486. */ 2487. raw_printf("Access to %s denied (%d).", gw.wizkit, errno); 2488. wait_synch(); 2489. /* fall through to standard names */ 2490. } else 2491. #endif 2492. if ((fp = fopen(gw.wizkit, "r")) != (FILE *) 0) { 2493. return fp; 2494. #if defined(UNIX) || defined(VMS) 2495. } else { 2496. /* access() above probably caught most problems for UNIX */ 2497. raw_printf("Couldn't open requested wizkit file %s (%d).", gw.wizkit, 2498. errno); 2499. wait_synch(); 2500. #endif 2501. } 2502. 2503. #if defined(MICRO) || defined(MACOS9) || defined(__BEOS__) || defined(WIN32) 2504. if ((fp = fopen(fqname(gw.wizkit, CONFIGPREFIX, 0), "r")) != (FILE *) 0) 2505. return fp; 2506. #else 2507. #ifdef VMS 2508. envp = nh_getenv("HOME"); 2509. if (envp) 2510. Sprintf(tmp_wizkit, "%s%s", envp, gw.wizkit); 2511. else 2512. Sprintf(tmp_wizkit, "%s%s", "sys$login:", gw.wizkit); 2513. if ((fp = fopen(tmp_wizkit, "r")) != (FILE *) 0) 2514. return fp; 2515. #else /* should be only UNIX left */ 2516. envp = nh_getenv("HOME"); 2517. if (envp) 2518. Sprintf(tmp_wizkit, "%s/%s", envp, gw.wizkit); 2519. else 2520. Strcpy(tmp_wizkit, gw.wizkit); 2521. if ((fp = fopen(tmp_wizkit, "r")) != (FILE *) 0) 2522. return fp; 2523. else if (errno != ENOENT) { 2524. /* e.g., problems when setuid NetHack can't search home 2525. * directory restricted to user */ 2526. raw_printf("Couldn't open default gw.wizkit file %s (%d).", 2527. tmp_wizkit, errno); 2528. wait_synch(); 2529. } 2530. #endif 2531. #endif 2532. return (FILE *) 0; 2533. } 2534. 2535. /* add to hero's inventory if there's room, otherwise put item on floor */ 2536. staticfn void 2537. wizkit_addinv(struct obj *obj) 2538. { 2539. if (!obj || obj == &hands_obj) 2540. return; 2541. 2542. /* subset of starting inventory pre-ID */ 2543. observe_object(obj); 2544. if (Role_if(PM_CLERIC)) 2545. obj->bknown = 1; /* ok to bypass set_bknown() */ 2546. /* same criteria as lift_object()'s check for available inventory slot */ 2547. if (obj->oclass != COIN_CLASS && inv_cnt(FALSE) >= invlet_basic 2548. && !merge_choice(gi.invent, obj)) { 2549. /* inventory overflow; can't just place & stack object since 2550. hero isn't in position yet, so schedule for arrival later */ 2551. add_to_migration(obj); 2552. obj->ox = 0; /* index of main dungeon */ 2553. obj->oy = 1; /* starting level number */ 2554. obj->owornmask = 2555. (long) (MIGR_WITH_HERO | MIGR_NOBREAK | MIGR_NOSCATTER); 2556. } else { 2557. (void) addinv(obj); 2558. } 2559. } 2560. 2561. boolean 2562. proc_wizkit_line(char *buf) 2563. { 2564. struct obj *otmp; 2565. 2566. if (strlen(buf) >= BUFSZ) 2567. buf[BUFSZ - 1] = '\0'; 2568. otmp = readobjnam(buf, (struct obj *) 0); 2569. 2570. if (otmp) { 2571. if (otmp != &hands_obj) { 2572. wish_history_add(buf); 2573. wizkit_addinv(otmp); 2574. } 2575. } else { 2576. /* .60 limits output line width to 79 chars */ 2577. config_error_add("Bad wizkit item: \"%.60s\"", buf); 2578. return FALSE; 2579. } 2580. return TRUE; 2581. } 2582. 2583. void 2584. read_wizkit(void) 2585. { 2586. FILE *fp; 2587. 2588. if (!wizard || !(fp = fopen_wizkit_file())) 2589. return; 2590. 2591. program_state.wizkit_wishing = 1; 2592. config_error_init(TRUE, "WIZKIT", FALSE); 2593. 2594. parse_conf_file(fp, proc_wizkit_line); 2595. (void) fclose(fp); 2596. 2597. config_error_done(); 2598. program_state.wizkit_wishing = 0; 2599. 2600. return; 2601. } 2602. 2603. /* ---------- END WIZKIT FILE HANDLING ----------- */ 2604. 2605. /* ---------- BEGIN SYMSET FILE HANDLING ----------- */ 2606. 2607. extern const char *const known_handling[]; /* symbols.c */ 2608. extern const char *const known_restrictions[]; /* symbols.c */ 2609. 2610. staticfn FILE * 2611. fopen_sym_file(void) 2612. { 2613. FILE *fp; 2614. 2615. fp = fopen_datafile(SYMBOLS, "r", 2616. #ifdef WIN32 2617. SYSCONFPREFIX 2618. #else 2619. HACKPREFIX 2620. #endif 2621. ); 2622. 2623. return fp; 2624. } 2625. 2626. /* 2627. * Returns 1 if the chosen symset was found and loaded. 2628. * 0 if it wasn't found in the sym file or other problem. 2629. */ 2630. int 2631. read_sym_file(int which_set) 2632. { 2633. FILE *fp; 2634. 2635. gs.symset[which_set].explicitly = FALSE; 2636. if (!(fp = fopen_sym_file())) 2637. return 0; 2638. 2639. gs.symset[which_set].explicitly = TRUE; 2640. gc.chosen_symset_start = gc.chosen_symset_end = FALSE; 2641. gs.symset_which_set = which_set; 2642. gs.symset_count = 0; 2643. 2644. config_error_init(TRUE, "symbols", FALSE); 2645. 2646. parse_conf_file(fp, proc_symset_line); 2647. (void) fclose(fp); 2648. 2649. if (!gc.chosen_symset_start && !gc.chosen_symset_end) { 2650. /* name caller put in symset[which_set].name was not found; 2651. if it looks like "Default symbols", null it out and return 2652. success to use the default; otherwise, return failure */ 2653. if (gs.symset[which_set].name 2654. && (fuzzymatch(gs.symset[which_set].name, "Default symbols", 2655. " -_", TRUE) 2656. || !strcmpi(gs.symset[which_set].name, "default"))) 2657. clear_symsetentry(which_set, TRUE); 2658. config_error_done(); 2659. 2660. /* If name was defined, it was invalid. Then we're loading fallback */ 2661. if (gs.symset[which_set].name) { 2662. gs.symset[which_set].explicitly = FALSE; 2663. return 0; 2664. } 2665. 2666. return 1; 2667. } 2668. if (!gc.chosen_symset_end) 2669. config_error_add("Missing finish for symset \"%s\"", 2670. gs.symset[which_set].name ? gs.symset[which_set].name 2671. : "unknown"); 2672. config_error_done(); 2673. return 1; 2674. } 2675. 2676. /* ---------- END SYMSET FILE HANDLING ----------- */ 2677. 2678. /* ---------- BEGIN SCOREBOARD CREATION ----------- */ 2679. 2680. #ifdef OS2_CODEVIEW 2681. #define UNUSED_if_not_OS2_CODEVIEW /*empty*/ 2682. #else 2683. #define UNUSED_if_not_OS2_CODEVIEW UNUSED 2684. #endif 2685. 2686. /* verify that we can write to scoreboard file; if not, try to create one */ 2687. /*ARGUSED*/ 2688. void 2689. check_recordfile(const char *dir UNUSED_if_not_OS2_CODEVIEW) 2690. { 2691. const char *fq_record; 2692. int fd; 2693. 2694. #if defined(UNIX) || defined(VMS) 2695. fq_record = fqname(RECORD, SCOREPREFIX, 0); 2696. fd = open(fq_record, O_RDWR, 0); 2697. if (fd >= 0) { 2698. #ifdef VMS /* must be stream-lf to use UPDATE_RECORD_IN_PLACE */ 2699. if (!file_is_stmlf(fd)) { 2700. raw_printf("Warning: scoreboard file '%s'" 2701. " is not in stream_lf format", 2702. fq_record); 2703. wait_synch(); 2704. } 2705. #endif 2706. (void) nhclose(fd); /* RECORD is accessible */ 2707. } else if ((fd = open(fq_record, O_CREAT | O_RDWR, FCMASK)) >= 0) { 2708. (void) nhclose(fd); /* RECORD newly created */ 2709. #if defined(VMS) && !defined(SECURE) 2710. /* Re-protect RECORD with world:read+write+execute+delete access. */ 2711. (void) chmod(fq_record, FCMASK | 007); 2712. #endif /* VMS && !SECURE */ 2713. } else { 2714. raw_printf("Warning: cannot write scoreboard file '%s'", fq_record); 2715. wait_synch(); 2716. } 2717. #endif /* !UNIX && !VMS */ 2718. #if defined(MICRO) || defined(WIN32) 2719. char tmp[PATHLEN]; 2720. 2721. #ifdef OS2_CODEVIEW /* explicit path on opening for OS/2 */ 2722. /* how does this work when there isn't an explicit path or fopen 2723. * for later access to the file via fopen_datafile? ? */ 2724. (void) strncpy(tmp, dir, PATHLEN - 1); 2725. tmp[PATHLEN - 1] = '\0'; 2726. if ((strlen(tmp) + 1 + strlen(RECORD)) < (PATHLEN - 1)) { 2727. append_slash(tmp); 2728. Strcat(tmp, RECORD); 2729. } 2730. fq_record = tmp; 2731. #else 2732. Strcpy(tmp, RECORD); 2733. fq_record = fqname(RECORD, SCOREPREFIX, 0); 2734. #endif 2735. #ifdef WIN32 2736. /* If dir is NULL it indicates create but 2737. only if it doesn't already exist */ 2738. if (!dir) { 2739. char buf[BUFSZ]; 2740. 2741. buf[0] = '\0'; 2742. fd = open(fq_record, O_RDWR); 2743. if (!(fd == -1 && errno == ENOENT)) { 2744. if (fd >= 0) { 2745. (void) nhclose(fd); 2746. } else { 2747. /* explanation for failure other than missing file */ 2748. Sprintf(buf, "error \"%s\", (errno %d).", 2749. fq_record, errno); 2750. paniclog("scorefile", buf); 2751. } 2752. return; 2753. } 2754. Sprintf(buf, "missing \"%s\", creating new scorefile.", 2755. fq_record); 2756. paniclog("scorefile", buf); 2757. } 2758. #endif 2759. 2760. if ((fd = open(fq_record, O_RDWR)) < 0) { 2761. /* try to create empty 'record' */ 2762. #if defined(AZTEC_C) || defined(_DCC) \ 2763. || (defined(__GNUC__) && defined(__AMIGA__)) 2764. /* Aztec doesn't use the third argument */ 2765. /* DICE doesn't like it */ 2766. fd = open(fq_record, O_CREAT | O_RDWR); 2767. #else 2768. fd = open(fq_record, O_CREAT | O_RDWR, S_IREAD | S_IWRITE); 2769. #endif 2770. if (fd <= 0) { 2771. raw_printf("Warning: cannot write record '%s'", tmp); 2772. wait_synch(); 2773. } else { 2774. (void) nhclose(fd); 2775. } 2776. } else { 2777. /* open succeeded => 'record' exists */ 2778. (void) nhclose(fd); 2779. } 2780. #else /* MICRO || WIN32*/ 2781. 2782. #ifdef MACOS9 2783. /* Create the "record" file, if necessary */ 2784. fq_record = fqname(RECORD, SCOREPREFIX, 0); 2785. fd = macopen(fq_record, O_RDWR | O_CREAT, TEXT_TYPE); 2786. if (fd != -1) 2787. macclose(fd); 2788. #endif /* MACOS9 */ 2789. 2790. #endif /* MICRO || WIN32*/ 2791. } 2792. 2793. #undef UNUSED_if_not_OS2_CODEVIEW 2794. 2795. /* ---------- END SCOREBOARD CREATION ----------- */ 2796. 2797. /* ---------- BEGIN PANIC/IMPOSSIBLE/TESTING LOG ----------- */ 2798. 2799. /*ARGSUSED*/ 2800. void 2801. paniclog( 2802. const char *type, /* panic, impossible, trickery, [lua] */ 2803. const char *reason) /* explanation */ 2804. { 2805. #ifdef PANICLOG 2806. FILE *lfile; 2807. 2808. if (!program_state.in_paniclog) { 2809. program_state.in_paniclog = 1; 2810. lfile = fopen_datafile(PANICLOG, "a", TROUBLEPREFIX); 2811. if (lfile) { 2812. #ifdef PANICLOG_FMT2 2813. (void) fprintf(lfile, "%ld %s: %s %s\n", 2814. ubirthday, (svp.plname[0] ? svp.plname : "(none)"), 2815. type, reason); 2816. #else 2817. char buf[BUFSZ]; 2818. time_t now = getnow(); 2819. int uid = getuid(); 2820. char playmode = wizard ? 'D' : discover ? 'X' : '-'; 2821. 2822. (void) fprintf(lfile, "%s %08ld %06ld %d %c: %s %s\n", 2823. version_string(buf, sizeof buf), 2824. yyyymmdd(now), hhmmss(now), 2825. uid, playmode, type, reason); 2826. #endif /* !PANICLOG_FMT2 */ 2827. (void) fclose(lfile); 2828. } 2829. program_state.in_paniclog = 0; 2830. } 2831. #endif /* PANICLOG */ 2832. return; 2833. } 2834. 2835. void 2836. testinglog(const char *filenm, /* ad hoc file name */ 2837. const char *type, 2838. const char *reason) /* explanation */ 2839. { 2840. FILE *lfile; 2841. char fnbuf[BUFSZ]; 2842. 2843. if (!filenm) 2844. return; 2845. Strcpy(fnbuf, filenm); 2846. if (strchr(fnbuf, '.') == 0) 2847. Strcat(fnbuf, ".log"); 2848. lfile = fopen_datafile(fnbuf, "a", TROUBLEPREFIX); 2849. if (lfile) { 2850. (void) fprintf(lfile, "%s\n%s\n", type, reason); 2851. (void) fclose(lfile); 2852. } 2853. return; 2854. } 2855. 2856. /* ---------- END PANIC/IMPOSSIBLE/TESTING LOG ----------- */ 2857. 2858. #ifdef SELF_RECOVER 2859. 2860. /* ---------- BEGIN INTERNAL RECOVER ----------- */ 2861. 2862. extern uchar critical_sizes[], cscbuf[]; /* version.c */ 2863. 2864. boolean 2865. recover_savefile(void) 2866. { 2867. NHFILE *gnhfp, *lnhfp, *snhfp; 2868. int lev, savelev, hpid, 2869. pltmpsiz, cscount = get_critical_size_count(); 2870. xint8 levc; 2871. struct version_info version_data; 2872. int processed[256]; 2873. char savename[SAVESIZE], errbuf[BUFSZ], indicator, file_cscount; 2874. char tmpplbuf[PL_NSIZ_PLUS]; 2875. const char *savewrite_failure = (const char *) 0; 2876. off_t filesz = 0; 2877. 2878. for (lev = 0; lev < 256; lev++) 2879. processed[lev] = 0; 2880. 2881. /* level 0 file contains: 2882. * pid of creating process (ignored here) 2883. * level number for current level of save file 2884. * name of save file nethack would have created 2885. * player name 2886. * and game state 2887. */ 2888. 2889. gnhfp = open_levelfile(0, errbuf); 2890. if (!gnhfp) { 2891. raw_printf("%s\n", errbuf); 2892. return FALSE; 2893. } 2894. filesz = lseek(gnhfp->fd, 0L, SEEK_END); 2895. (void) lseek(gnhfp->fd, 0L, SEEK_SET); 2896. if ((size_t) filesz < (sizeof hpid 2897. + sizeof lev 2898. + sizeof savename 2899. + sizeof indicator 2900. + sizeof file_cscount 2901. + sizeof version_data + sizeof pltmpsiz)) { 2902. const char *fq_save; 2903. 2904. /* this indicates a .0 file that was created as part of 2905. * recover that did not complete. There could be an intact 2906. * savefile already there. Check for that and return TRUE 2907. * if there is. */ 2908. set_savefile_name(TRUE); 2909. fq_save = fqname(gs.SAVEF, SAVEPREFIX, 0); 2910. if (access(fq_save, F_OK) == 0) { 2911. close_nhfile(gnhfp); 2912. delete_levelfile(0); 2913. return TRUE; 2914. } else { 2915. /* savefile doesn't exist, so fall through */ 2916. } 2917. } 2918. if (read(gnhfp->fd, (genericptr_t) &hpid, sizeof hpid) != sizeof hpid) { 2919. raw_printf("\n%s\n%s\n", 2920. "Checkpoint data incompletely written" 2921. " or subsequently clobbered.", 2922. "Recovery impossible."); 2923. close_nhfile(gnhfp); 2924. return FALSE; 2925. } 2926. if (read(gnhfp->fd, (genericptr_t) &savelev, sizeof(savelev)) 2927. != sizeof(savelev)) { 2928. raw_printf("\n%s %s %s\n", 2929. "Checkpointing was not in effect for", 2930. gl.lock, 2931. "-- recovery impossible."); 2932. close_nhfile(gnhfp); 2933. return FALSE; 2934. } 2935. if ((read(gnhfp->fd, (genericptr_t) savename, sizeof savename) 2936. != sizeof savename) 2937. || (read(gnhfp->fd, (genericptr_t) &indicator, sizeof indicator) 2938. != sizeof indicator) 2939. || (read(gnhfp->fd, (genericptr_t) &file_cscount, sizeof file_cscount) 2940. != sizeof file_cscount) 2941. || (file_cscount <= cscount 2942. && read(gnhfp->fd, (genericptr_t) &cscbuf, file_cscount) 2943. != file_cscount) 2944. || (read(gnhfp->fd, (genericptr_t) &version_data, sizeof version_data) 2945. != sizeof version_data) 2946. || (read(gnhfp->fd, (genericptr_t) &pltmpsiz, sizeof pltmpsiz) 2947. != sizeof pltmpsiz) || (pltmpsiz > PL_NSIZ_PLUS) 2948. || (read(gnhfp->fd, (genericptr_t) &tmpplbuf, pltmpsiz) 2949. != pltmpsiz)) { 2950. raw_printf("\nError reading %s -- can't recover.\n", gl.lock); 2951. close_nhfile(gnhfp); 2952. return FALSE; 2953. } 2954. 2955. /* save file should contain: 2956. * format indicator (1 byte) 2957. * n = count of critical size list (1 byte) 2958. * n bytes of critical sizes (n bytes) 2959. * version info 2960. * plnametmp = player name size (int, 2 bytes) 2961. * player name (PL_NSIZ_PLUS) 2962. * current level (including pets) 2963. * (non-level-based) game state 2964. * other levels 2965. */ 2966. 2967. /* 2968. * 2969. * Set a flag for the savefile routines to know the 2970. * circumstances and act accordingly: 2971. * program_state.in_self_recover 2972. */ 2973. program_state.in_self_recover = TRUE; 2974. set_savefile_name(TRUE); 2975. snhfp = create_savefile(); 2976. if (!snhfp) { 2977. raw_printf("\nCannot recover savefile %s.\n", gs.SAVEF); 2978. close_nhfile(gnhfp); 2979. return FALSE; 2980. } 2981. 2982. lnhfp = open_levelfile(savelev, errbuf); 2983. if (!lnhfp) { 2984. raw_printf("\n%s\n", errbuf); 2985. close_nhfile(gnhfp); 2986. close_nhfile(snhfp); 2987. delete_savefile(); 2988. return FALSE; 2989. } 2990. 2991. store_version(snhfp); 2992. 2993. if (savewrite_failure) 2994. goto cleanup; 2995. 2996. if (snhfp->structlevel) 2997. bufoff(snhfp->fd); 2998. 2999. /* TODO: this is not a single byte, so a big-endian byte swap 3000. * might be necessary here, if anyone is concerned about big-endian */ 3001. Sfo_int(snhfp, &pltmpsiz, "plname-size"); 3002. savewrite_failure = (const char *) 0; 3003. if (savewrite_failure) 3004. goto cleanup; 3005. 3006. Sfo_char(snhfp, tmpplbuf, "plname", pltmpsiz); 3007. savewrite_failure = (const char *) 0; 3008. if (savewrite_failure) 3009. goto cleanup; 3010. 3011. if (!copy_bytes(lnhfp->fd, snhfp->fd)) { 3012. close_nhfile(gnhfp); 3013. close_nhfile(snhfp); 3014. close_nhfile(lnhfp); 3015. delete_savefile(); 3016. return FALSE; 3017. } 3018. close_nhfile(lnhfp); 3019. processed[savelev] = 1; 3020. 3021. if (!copy_bytes(gnhfp->fd, snhfp->fd)) { 3022. close_nhfile(gnhfp); 3023. close_nhfile(snhfp); 3024. delete_savefile(); 3025. return FALSE; 3026. } 3027. close_nhfile(gnhfp); 3028. processed[0] = 1; 3029. 3030. for (lev = 1; lev < 256; lev++) { 3031. /* level numbers are kept in xint8's in save.c, so the 3032. * maximum level number (for the endlevel) must be < 256 3033. */ 3034. if (lev != savelev) { 3035. lnhfp = open_levelfile(lev, (char *) 0); 3036. if (lnhfp) { 3037. /* any or all of these may not exist */ 3038. levc = (xint8) lev; 3039. (void) write(snhfp->fd, (genericptr_t) &levc, sizeof(levc)); 3040. if (!copy_bytes(lnhfp->fd, snhfp->fd)) { 3041. close_nhfile(lnhfp); 3042. close_nhfile(snhfp); 3043. delete_savefile(); 3044. return FALSE; 3045. } 3046. close_nhfile(lnhfp); 3047. processed[lev] = 1; 3048. } 3049. } 3050. } 3051. if (snhfp->structlevel) 3052. bufon(snhfp->fd); 3053. close_nhfile(snhfp); 3054. /* 3055. * We have a successful savefile! 3056. * Only now do we erase the level files. 3057. */ 3058. for (lev = 0; lev < 256; lev++) { 3059. if (processed[lev]) { 3060. const char *fq_lock; 3061. 3062. set_levelfile_name(gl.lock, lev); 3063. fq_lock = fqname(gl.lock, LEVELPREFIX, 3); 3064. (void) unlink(fq_lock); 3065. } 3066. } 3067. cleanup: 3068. if (savewrite_failure) { 3069. raw_printf("\nError writing %s; recovery failed (%s).\n", 3070. gs.SAVEF, savewrite_failure); 3071. close_nhfile(gnhfp); 3072. close_nhfile(snhfp); 3073. close_nhfile(lnhfp); 3074. program_state.in_self_recover = FALSE; 3075. delete_savefile(); 3076. return FALSE; 3077. } 3078. /* we don't clear program_state.in_self_recover here, we 3079. leave it as a flag to reload the structlevel savefile 3080. in the caller. The caller should then clear it. */ 3081. return TRUE; 3082. } 3083. 3084. /* ---------- END INTERNAL RECOVER ----------- */ 3085. #endif /*SELF_RECOVER*/ 3086. 3087. /* ---------- OTHER ----------- */ 3088. 3089. ATTRNORETURN void 3090. do_deferred_showpaths(int code) 3091. { 3092. gd.deferred_showpaths = FALSE; 3093. reveal_paths(code); 3094. 3095. /* cleanup before heading to an exit */ 3096. freedynamicdata(); 3097. dlb_cleanup(); 3098. l_nhcore_done(); 3099. 3100. #ifdef UNIX 3101. after_opt_showpaths(gd.deferred_showpaths_dir); 3102. #else 3103. #ifndef WIN32 3104. #ifdef CHDIR 3105. chdirx(gd.deferred_showpaths_dir, 0); 3106. #endif 3107. #endif 3108. #if defined(WIN32) || defined(MICRO) || defined(OS2) 3109. nethack_exit(EXIT_SUCCESS); 3110. #else 3111. exit(EXIT_SUCCESS); 3112. #endif 3113. #endif 3114. } 3115. #endif /* !SFCTOOL */ 3116. 3117. #ifdef DEBUG 3118. /* used by debugpline() to decide whether to issue a message 3119. * from a particular source file; caller passes __FILE__ and we check 3120. * whether it is in the source file list supplied by SYSCF's DEBUGFILES 3121. * 3122. * pass FALSE to override wildcard matching; useful for files 3123. * like dungeon.c and questpgr.c, which generate a ridiculous amount of 3124. * output if DEBUG is defined and effectively block the use of a wildcard */ 3125. boolean 3126. debugcore(const char *filename, boolean wildcards) 3127. { 3128. const char *debugfiles, *p; 3129. 3130. /* debugpline() messages might disclose information that the player 3131. doesn't normally get to see, so only display them in wizard mode */ 3132. if (!wizard) 3133. return FALSE; 3134. 3135. if (!filename || !*filename) 3136. return FALSE; /* sanity precaution */ 3137. 3138. debugfiles = sysopt.debugfiles; 3139. /* usual case: sysopt.debugfiles will be empty */ 3140. if (!debugfiles || !*debugfiles) 3141. return FALSE; 3142. 3143. /* strip filename's path if present */ 3144. filename = nh_basename(filename, TRUE); 3145. 3146. /* 3147. * Wildcard match will only work if there's a single pattern (which 3148. * might be a single file name without any wildcarding) rather than 3149. * a space-separated list. 3150. * [to NOT do: We could step through the space-separated list and 3151. * attempt a wildcard match against each element, but that would be 3152. * overkill for the intended usage.] 3153. */ 3154. if (wildcards && pmatch(debugfiles, filename)) 3155. return TRUE; 3156. 3157. /* check whether filename is an element of the list */ 3158. if ((p = strstr(debugfiles, filename)) != 0) { 3159. int l = (int) strlen(filename); 3160. 3161. if ((p == debugfiles || p[-1] == ' ' || p[-1] == '/') 3162. && (p[l] == ' ' || p[l] == '\0')) 3163. return TRUE; 3164. } 3165. return FALSE; 3166. } 3167. 3168. #endif /*DEBUG*/ 3169. 3170. #ifndef SFCTOOL 3171. 3172. #define SYSCONFFILE "system configuration file" 3173. 3174. void 3175. reveal_paths(int code) 3176. { 3177. #if defined(SYSCF) 3178. boolean skip_sysopt = FALSE; 3179. #endif 3180. const char *fqn, *nodumpreason; 3181. 3182. char buf[BUFSZ]; 3183. #if defined(SYSCF) || !defined(UNIX) || defined(DLB) 3184. const char *filep; 3185. #ifdef SYSCF 3186. const char *gamename = (gh.hname && *gh.hname) ? gh.hname : "NetHack"; 3187. #endif 3188. #endif 3189. #if defined(PREFIXES_IN_USE) 3190. const char *cstrp; 3191. #endif 3192. #ifdef UNIX 3193. char *endp, *envp, cwdbuf[PATH_MAX]; 3194. #endif 3195. #ifdef PREFIXES_IN_USE 3196. int i, maxlen = 0; 3197. 3198. raw_print("Variable playground locations:"); 3199. for (i = 0; i < PREFIX_COUNT; i++) 3200. raw_printf(" [%-10s]=\"%s\"", fqn_prefix_names[i], 3201. gf.fqn_prefix[i] ? gf.fqn_prefix[i] : "not set"); 3202. #endif 3203. 3204. /* sysconf file */ 3205. 3206. #ifdef SYSCF 3207. #ifdef PREFIXES_IN_USE 3208. cstrp = fqn_prefix_names[SYSCONFPREFIX]; 3209. maxlen = BUFSZ - sizeof " (in )"; 3210. if (cstrp && (int) strlen(cstrp) < maxlen) 3211. Sprintf(buf, " (in %s)", cstrp); 3212. #else 3213. buf[0] = '\0'; 3214. #endif 3215. raw_printf("%s %s%s:", s_suffix(gamename), 3216. SYSCONFFILE, buf); 3217. #ifdef SYSCF_FILE 3218. filep = SYSCF_FILE; 3219. #else 3220. filep = "sysconf"; 3221. #endif 3222. fqn = fqname(filep, SYSCONFPREFIX, 0); 3223. if (fqn) { 3224. set_configfile_name(fqn); 3225. filep = get_configfile(); 3226. } 3227. raw_printf(" \"%s\"", filep); 3228. if (code == 1) { 3229. raw_printf("NOTE: The %s above is missing or inaccessible!", 3230. SYSCONFFILE); 3231. skip_sysopt = TRUE; 3232. } 3233. #else /* !SYSCF */ 3234. raw_printf("No system configuration file."); 3235. #endif /* ?SYSCF */ 3236. 3237. /* symbols file */ 3238. 3239. buf[0] = '\0'; 3240. #ifndef UNIX 3241. #ifdef PREFIXES_IN_USE 3242. #ifdef WIN32 3243. cstrp = fqn_prefix_names[SYSCONFPREFIX]; 3244. #else 3245. cstrp = fqn_prefix_names[HACKPREFIX]; 3246. #endif /* WIN32 */ 3247. maxlen = BUFSZ - sizeof " (in )"; 3248. if (cstrp && (int) strlen(cstrp) < maxlen) 3249. Sprintf(buf, " (in %s)", cstrp); 3250. #endif /* PREFIXES_IN_USE */ 3251. raw_printf("The loadable symbols file%s:", buf); 3252. #endif /* UNIX */ 3253. 3254. #ifdef UNIX 3255. envp = getcwd(cwdbuf, PATH_MAX); 3256. if (envp) { 3257. raw_print("The loadable symbols file:"); 3258. raw_printf(" \"%s/%s\"", envp, SYMBOLS); 3259. } 3260. #else /* UNIX */ 3261. filep = SYMBOLS; 3262. #ifdef PREFIXES_IN_USE 3263. #ifdef WIN32 3264. fqn = fqname(filep, SYSCONFPREFIX, 1); 3265. #else 3266. fqn = fqname(filep, HACKPREFIX, 1); 3267. #endif /* WIN32 */ 3268. if (fqn) 3269. filep = fqn; 3270. #endif /* PREFIXES_IN_USE */ 3271. raw_printf(" \"%s\"", filep); 3272. #endif /* UNIX */ 3273. 3274. /* dlb vs non-dlb */ 3275. 3276. buf[0] = '\0'; 3277. #ifdef PREFIXES_IN_USE 3278. cstrp = fqn_prefix_names[DATAPREFIX]; 3279. maxlen = BUFSZ - sizeof " (in )"; 3280. if (cstrp && (int) strlen(cstrp) < maxlen) 3281. Sprintf(buf, " (in %s)", cstrp); 3282. #endif 3283. #ifdef DLB 3284. raw_printf("Basic data files%s are collected inside:", buf); 3285. filep = DLBFILE; 3286. #ifdef VERSION_IN_DLB_FILENAME 3287. Strcpy(buf, build_dlb_filename((const char *) 0)); 3288. #ifdef PREFIXES_IN_USE 3289. fqn = fqname(buf, DATAPREFIX, 1); 3290. if (fqn) 3291. filep = fqn; 3292. #endif /* PREFIXES_IN_USE */ 3293. #endif 3294. raw_printf(" \"%s\"", filep); 3295. #ifdef DLBFILE2 3296. filep = DLBFILE2; 3297. raw_printf(" \"%s\"", filep); 3298. #endif 3299. #else /* !DLB */ 3300. raw_printf("Basic data files%s are in many separate files.", buf); 3301. #endif /* ?DLB */ 3302. 3303. /* dumplog */ 3304. 3305. fqn = (char *) 0; 3306. #ifndef DUMPLOG 3307. nodumpreason = "not supported"; 3308. #else 3309. nodumpreason = "disabled"; 3310. #ifdef SYSCF 3311. if (!skip_sysopt) { 3312. fqn = sysopt.dumplogfile; 3313. if (!fqn) 3314. nodumpreason = "DUMPLOGFILE is not set in " SYSCONFFILE; 3315. } else { 3316. nodumpreason = SYSCONFFILE " is missing; no DUMPLOGFILE setting"; 3317. } 3318. #else /* !SYSCF */ 3319. #ifdef DUMPLOG_FILE 3320. fqn = DUMPLOG_FILE; 3321. #endif 3322. #endif /* ?SYSCF */ 3323. if (fqn && *fqn) { 3324. raw_print("Your end-of-game disclosure file:"); 3325. (void) dump_fmtstr(fqn, buf, FALSE); 3326. buf[sizeof buf - sizeof " \"\""] = '\0'; 3327. raw_printf(" \"%s\"", buf); 3328. } else { 3329. raw_printf("No end-of-game disclosure file (%s).", nodumpreason); 3330. } 3331. #endif /* ?DUMPLOG */ 3332. 3333. #ifdef SYSCF 3334. #ifdef WIN32 3335. if (!skip_sysopt) { 3336. if (sysopt.portable_device_paths) { 3337. const char *pd = get_portable_device(); 3338. 3339. /* an empty value for pd indicates that portable_device_paths 3340. got set TRUE in a sysconf file other than the one containing 3341. the executable; disregard it */ 3342. if (strlen(pd) > 0) { 3343. raw_printf("portable_device_paths (set in sysconf):"); 3344. raw_printf(" \"%s\"", pd); 3345. } 3346. } 3347. } 3348. #endif 3349. #endif 3350. 3351. /* personal configuration file */ 3352. 3353. buf[0] = '\0'; 3354. #ifdef PREFIXES_IN_USE 3355. cstrp = fqn_prefix_names[CONFIGPREFIX]; 3356. maxlen = BUFSZ - sizeof " (in )"; 3357. if (cstrp && (int) strlen(cstrp) < maxlen) 3358. Sprintf(buf, " (in %s)", cstrp); 3359. #endif /* PREFIXES_IN_USE */ 3360. raw_printf("Your personal configuration file%s:", buf); 3361. 3362. #ifdef UNIX 3363. buf[0] = '\0'; 3364. if ((envp = nh_getenv("HOME")) != 0) { 3365. copynchars(buf, envp, (int) sizeof buf - 1 - 1); 3366. Strcat(buf, "/"); 3367. } 3368. endp = eos(buf); 3369. copynchars(endp, get_default_configfile(), 3370. (int) (sizeof buf - 1 - strlen(buf))); 3371. #if defined(__APPLE__) /* UNIX+__APPLE__ => MacOSX aka OSX aka macOS */ 3372. if (envp) { 3373. if (access(buf, 4) == -1) { /* 4: R_OK, -1: failure */ 3374. /* read access to default failed; might be protected excessively 3375. but more likely it doesn't exist; try first alternate: 3376. "$HOME/Library/Pref..."; 'endp' points past '/' */ 3377. copynchars(endp, "Library/Preferences/NetHack Defaults", 3378. (int) (sizeof buf - 1 - strlen(buf))); 3379. if (access(buf, 4) == -1) { 3380. /* first alternate failed, try second: 3381. ".../NetHack Defaults.txt"; no 'endp', just append */ 3382. copynchars(eos(buf), ".txt", 3383. (int) (sizeof buf - 1 - strlen(buf))); 3384. if (access(buf, 4) == -1) { 3385. /* second alternate failed too, so revert to the 3386. original default ("$HOME/.nethackrc") for message */ 3387. copynchars(endp, get_default_configfile(), 3388. (int) (sizeof buf - 1 - strlen(buf))); 3389. } 3390. } 3391. } 3392. } 3393. #endif /* __APPLE__ */ 3394. raw_printf(" \"%s\"", buf); 3395. #else /* !UNIX */ 3396. fqn = (const char *) 0; 3397. #ifdef PREFIXES_IN_USE 3398. fqn = fqname(get_default_configfile(), CONFIGPREFIX, 2); 3399. #endif 3400. raw_printf(" \"%s\"", fqn ? fqn : get_default_configfile()); 3401. #endif /* ?UNIX */ 3402. 3403. raw_print(""); 3404. #if defined(WIN32) && !defined(WIN32CON) 3405. wait_synch(); 3406. #endif 3407. #ifndef DUMPLOG 3408. #ifdef SYSCF 3409. nhUse(skip_sysopt); 3410. #endif 3411. nhUse(nodumpreason); 3412. #endif 3413. } 3414. 3415. /* ---------- BEGIN TRIBUTE ----------- */ 3416. 3417. /* 3.6 tribute code 3418. */ 3419. 3420. #define SECTIONSCOPE 1 3421. #define TITLESCOPE 2 3422. #define PASSAGESCOPE 3 3423. 3424. #define MAXPASSAGES SIZE(svc.context.novel.pasg) /* 20 */ 3425. 3426. staticfn int choose_passage(int, unsigned); 3427. 3428. /* choose a random passage that hasn't been chosen yet; once all have 3429. been chosen, reset the tracking to make all passages available again */ 3430. staticfn int 3431. choose_passage(int passagecnt, /* total of available passages */ 3432. unsigned oid) /* book.o_id, used to determine whether 3433. re-reading same book */ 3434. { 3435. int idx, res; 3436. 3437. if (passagecnt < 1) 3438. return 0; 3439. 3440. /* if a different book or we've used up all the passages already, 3441. reset in order to have all 'passagecnt' passages available */ 3442. if (oid != svc.context.novel.id || svc.context.novel.count == 0) { 3443. int i, range = passagecnt, limit = MAXPASSAGES; 3444. 3445. svc.context.novel.id = oid; 3446. if (range <= limit) { 3447. /* collect all of the N indices */ 3448. svc.context.novel.count = passagecnt; 3449. for (idx = 0; idx < MAXPASSAGES; idx++) 3450. svc.context.novel.pasg[idx] = (xint16) ((idx < passagecnt) 3451. ? idx + 1 : 0); 3452. } else { 3453. /* collect MAXPASSAGES of the N indices */ 3454. svc.context.novel.count = MAXPASSAGES; 3455. for (idx = i = 0; i < passagecnt; ++i, --range) 3456. if (range > 0 && rn2(range) < limit) { 3457. svc.context.novel.pasg[idx++] = (xint16) (i + 1); 3458. --limit; 3459. } 3460. } 3461. } 3462. 3463. idx = rn2(svc.context.novel.count); 3464. res = (int) svc.context.novel.pasg[idx]; 3465. /* move the last slot's passage index into the slot just used 3466. and reduce the number of passages available */ 3467. svc.context.novel.pasg[idx] 3468. = svc.context.novel.pasg[--svc.context.novel.count]; 3469. return res; 3470. } 3471. 3472. /* Returns True if you were able to read something. */ 3473. boolean 3474. read_tribute(const char *tribsection, const char *tribtitle, 3475. int tribpassage, char *nowin_buf, int bufsz, 3476. unsigned oid) /* book identifier */ 3477. { 3478. dlb *fp; 3479. char line[BUFSZ], lastline[BUFSZ]; 3480. 3481. int scope = 0; 3482. int linect = 0, passagecnt = 0, targetpassage = 0; 3483. const char *badtranslation = "an incomprehensible foreign translation"; 3484. boolean matchedsection = FALSE, matchedtitle = FALSE; 3485. winid tribwin = WIN_ERR; 3486. boolean grasped = FALSE; 3487. boolean foundpassage = FALSE; 3488. 3489. if (nowin_buf) 3490. *nowin_buf = '\0'; 3491. 3492. /* check for mandatories */ 3493. if (!tribsection || !tribtitle) { 3494. if (!nowin_buf) 3495. pline("It's %s of \"%s\"!", badtranslation, tribtitle); 3496. return grasped; 3497. } 3498. 3499. debugpline3("read_tribute %s, %s, %d.", tribsection, tribtitle, 3500. tribpassage); 3501. 3502. fp = dlb_fopen(TRIBUTEFILE, "r"); 3503. if (!fp) { 3504. /* this is actually an error - cannot open tribute file! */ 3505. if (!nowin_buf) 3506. You_feel("too overwhelmed to continue!"); 3507. return grasped; 3508. } 3509. 3510. /* 3511. * Syntax (not case-sensitive): 3512. * %section books 3513. * 3514. * In the books section: 3515. * %title booktitle (n) 3516. * where booktitle=book title without quotes 3517. * (n)= total number of passages present for this title 3518. * %passage k 3519. * where k=sequential passage number 3520. * 3521. * %e ends the passage/book/section 3522. * If in a passage, it marks the end of that passage. 3523. * If in a book, it marks the end of that book. 3524. * If in a section, it marks the end of that section. 3525. * 3526. * %section death 3527. */ 3528. 3529. *line = *lastline = '\0'; 3530. while (dlb_fgets(line, sizeof line, fp) != 0) { 3531. linect++; 3532. (void) strip_newline(line); 3533. switch (line[0]) { 3534. case '%': 3535. if (!strncmpi(&line[1], "section ", sizeof "section " - 1)) { 3536. char *st = &line[9]; /* 9 from "%section " */ 3537. 3538. scope = SECTIONSCOPE; 3539. matchedsection = !strcmpi(st, tribsection) ? TRUE : FALSE; 3540. } else if (!strncmpi(&line[1], "title ", sizeof "title " - 1)) { 3541. char *st = &line[7]; /* 7 from "%title " */ 3542. char *p1, *p2; 3543. 3544. if ((p1 = strchr(st, '(')) != 0) { 3545. *p1++ = '\0'; 3546. (void) mungspaces(st); 3547. if ((p2 = strchr(p1, ')')) != 0) { 3548. *p2 = '\0'; 3549. passagecnt = atoi(p1); 3550. scope = TITLESCOPE; 3551. if (matchedsection && !strcmpi(st, tribtitle)) { 3552. matchedtitle = TRUE; 3553. targetpassage = !tribpassage 3554. ? choose_passage(passagecnt, oid) 3555. : (tribpassage <= passagecnt) 3556. ? tribpassage : 0; 3557. } else { 3558. matchedtitle = FALSE; 3559. } 3560. } 3561. } 3562. } else if (!strncmpi(&line[1], "passage ", 3563. sizeof "passage " - 1)) { 3564. int passagenum = 0; 3565. char *st = &line[9]; /* 9 from "%passage " */ 3566. 3567. mungspaces(st); 3568. passagenum = atoi(st); 3569. if (passagenum > 0 && passagenum <= passagecnt) { 3570. scope = PASSAGESCOPE; 3571. if (matchedtitle && passagenum == targetpassage) { 3572. foundpassage = TRUE; 3573. if (!nowin_buf) { 3574. tribwin = create_nhwindow(NHW_MENU); 3575. if (tribwin == WIN_ERR) 3576. goto cleanup; 3577. } 3578. } 3579. } 3580. } else if (!strncmpi(&line[1], "e ", sizeof "e " - 1)) { 3581. if (foundpassage) 3582. goto cleanup; 3583. if (scope == TITLESCOPE) 3584. matchedtitle = FALSE; 3585. if (scope == SECTIONSCOPE) 3586. matchedsection = FALSE; 3587. if (scope) 3588. --scope; 3589. } else { 3590. debugpline1("tribute file error: bad %% command, line %d.", 3591. linect); 3592. } 3593. break; 3594. case '#': 3595. /* comment only, next! */ 3596. break; 3597. default: 3598. if (foundpassage) { 3599. if (!nowin_buf) { 3600. /* outputting multi-line passage to text window */ 3601. putstr(tribwin, 0, line); 3602. if (*line) 3603. Strcpy(lastline, line); 3604. } else { 3605. /* fetching one-line passage into buffer */ 3606. copynchars(nowin_buf, line, bufsz - 1); 3607. goto cleanup; /* don't wait for "%e passage" */ 3608. } 3609. } 3610. } 3611. } 3612. 3613. cleanup: 3614. (void) dlb_fclose(fp); 3615. if (nowin_buf) { 3616. /* one-line buffer */ 3617. grasped = *nowin_buf ? TRUE : FALSE; 3618. } else { 3619. if (tribwin != WIN_ERR) { /* implies 'foundpassage' */ 3620. /* multi-line window, normal case; 3621. if lastline is empty, there were no non-empty lines between 3622. "%passage n" and "%e passage" so we leave 'grasped' False */ 3623. if (*lastline) { 3624. char *p; 3625. 3626. display_nhwindow(tribwin, FALSE); 3627. /* put the final attribution line into message history, 3628. analogous to the summary line from long quest messages */ 3629. if (strchr(lastline, '[')) 3630. mungspaces(lastline); /* to remove leading spaces */ 3631. else /* construct one if necessary */ 3632. Sprintf(lastline, "[%s, by Terry Pratchett]", tribtitle); 3633. if ((p = strrchr(lastline, ']')) != 0) 3634. Sprintf(p, "; passage #%d]", targetpassage); 3635. putmsghistory(lastline, FALSE); 3636. grasped = TRUE; 3637. } 3638. destroy_nhwindow(tribwin); 3639. } 3640. if (!grasped) 3641. /* multi-line window, problem */ 3642. pline("It seems to be %s of \"%s\"!", badtranslation, tribtitle); 3643. } 3644. return grasped; 3645. } 3646. 3647. boolean 3648. Death_quote(char *buf, int bufsz) 3649. { 3650. unsigned death_oid = 1; /* chance of oid #1 being a novel is negligible */ 3651. 3652. return read_tribute("Death", "Death Quotes", 0, buf, bufsz, death_oid); 3653. } 3654. 3655. /* ---------- END TRIBUTE ----------- */ 3656. #endif /* !SFCTOOL */ 3657. 3658. #ifdef LIVELOG 3659. #define LLOG_SEP "\t" /* livelog field separator, as a string literal */ 3660. #define LLOG_EOL "\n" /* end-of-line, for abstraction consistency */ 3661. 3662. /* Locks the live log file and writes 'buffer' 3663. * iff the ll_type matches sysopt.livelog mask. 3664. * lltype is included in LL entry for post-process filtering also. 3665. */ 3666. void 3667. livelog_add(long ll_type, const char *str) 3668. { 3669. FILE *livelogfile; 3670. time_t now; 3671. int gindx, aindx; 3672. 3673. if (!(ll_type & sysopt.livelog)) 3674. return; 3675. 3676. if (lock_file(LIVELOGFILE, SCOREPREFIX, 10)) { 3677. if (!(livelogfile = fopen_datafile(LIVELOGFILE, "a", SCOREPREFIX))) { 3678. pline("Cannot open live log file!"); 3679. unlock_file(LIVELOGFILE); 3680. return; 3681. } 3682. 3683. now = getnow(); 3684. gindx = flags.female ? 1 : 0; 3685. /* note on alignment designation: 3686. aligns[] uses [0] lawful, [1] neutral, [2] chaotic; 3687. u.ualign.type uses -1 chaotic, 0 neutral, 1 lawful; 3688. so subtracting from 1 converts from either to the other */ 3689. aindx = 1 - u.ualign.type; 3690. /* format relies on STD C's implicit concatenation of 3691. adjacent string literals */ 3692. (void) fprintf(livelogfile, 3693. "lltype=%ld" LLOG_SEP "name=%s" LLOG_SEP 3694. "role=%s" LLOG_SEP "race=%s" LLOG_SEP 3695. "gender=%s" LLOG_SEP "align=%s" LLOG_SEP 3696. "turns=%ld" LLOG_SEP "starttime=%ld" LLOG_SEP 3697. "curtime=%ld" LLOG_SEP "message=%s" LLOG_EOL, 3698. (ll_type & sysopt.livelog), svp.plname, 3699. gu.urole.filecode, gu.urace.filecode, 3700. genders[gindx].filecode, aligns[aindx].filecode, 3701. svm.moves, timet_to_seconds(ubirthday), 3702. timet_to_seconds(now), str); 3703. (void) fclose(livelogfile); 3704. unlock_file(LIVELOGFILE); 3705. } 3706. } 3707. #undef LLOG_SEP 3708. #undef LLOG_EOL 3709. 3710. #else 3711. 3712. void 3713. livelog_add(long ll_type UNUSED, const char *str UNUSED) 3714. { 3715. /* nothing here */ 3716. } 3717. 3718. #endif /* !LIVELOG */ 3719. 3720. /*files.c*/