-
Notifications
You must be signed in to change notification settings - Fork 635
Expand file tree
/
Copy pathfiles.c
More file actions
3727 lines (3360 loc) · 105 KB
/
Copy pathfiles.c
File metadata and controls
3727 lines (3360 loc) · 105 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/* NetHack 5.0 files.c $NHDT-Date: 1781973049 2026/06/20 16:30:49 $ $NHDT-Branch: NetHack-5.0 $:$NHDT-Revision: 1.448 $ */
/* Copyright (c) Stichting Mathematisch Centrum, Amsterdam, 1985. */
/*-Copyright (c) Derek S. Ray, 2015. */
/* NetHack may be freely redistributed. See license for details. */
#define NEED_VARARGS
#if defined(WIN32)
#include "win32api.h"
#endif
#include "hack.h"
#include "dlb.h"
#ifdef SFCTOOL
#ifdef TTY_GRAPHICS
#undef TTY_GRAPHICS
#endif
#ifdef mark_synch
#undef mark_synch
#endif
#define mark_synch()
#ifdef raw_print
#undef raw_print
#endif
#define raw_print(a)
#ifdef WINDOWPORT
#undef WINDOWPORT
#endif
#define WINDOWPORT(x) FALSE
#ifdef clear_nhwindow
#undef clear_nhwindow
#endif
#define clear_nhwindow(x)
#endif /* SFCTOOL */
#ifdef TTY_GRAPHICS
#include "wintty.h" /* more() */
#endif
#if (!defined(MAC68K) && !defined(O_WRONLY) && !defined(AZTEC_C)) \
|| defined(USE_FCNTL)
#include <fcntl.h>
#endif
#include <errno.h>
#ifdef _MSC_VER /* MSC 6.0 defines errno quite differently */
#if (_MSC_VER >= 600)
#define SKIP_ERRNO
#endif
#else
#ifdef NHSTDC
#define SKIP_ERRNO
#endif
#endif
#ifndef SKIP_ERRNO
#ifdef _DCC
const
#endif
extern int errno;
#endif
#ifdef ZLIB_COMP /* RLC 09 Mar 1999: Support internal ZLIB */
#include "zlib.h"
#ifndef COMPRESS_EXTENSION
#define COMPRESS_EXTENSION ".gz"
#endif
#endif
#if defined(UNIX) && defined(SELECTSAVED)
#include <sys/types.h>
#include <dirent.h>
#endif
#if defined(UNIX) || defined(VMS) || !defined(NO_SIGNAL)
#include <signal.h>
#endif
#if defined(MSDOS) || defined(OS2) || defined(TOS) || defined(WIN32)
#if !defined(__DJGPP__) && !defined(__MINT__)
#include <sys\stat.h>
#else
#include <sys/stat.h>
#endif
#endif
#ifndef O_BINARY /* used for micros, no-op for others */
#define O_BINARY 0
#endif
#ifdef PREFIXES_IN_USE
#define FQN_NUMBUF 8
static char fqn_filename_buffer[FQN_NUMBUF][FQN_MAX_FILENAME];
#endif
#if !defined(SAVE_EXTENSION)
#ifdef MICRO
#define SAVE_EXTENSION ".sav"
#endif
#ifdef WIN32
#define SAVE_EXTENSION ".NetHack-saved-game"
#endif
#endif
#if defined(WIN32)
#include <share.h>
#include <io.h>
#define F_OK 0
#define access _access
#endif
#ifdef AMIGA
extern char PATH[]; /* see sys/amiga/amidos.c */
#include <libraries/dos.h>
extern void amii_set_text_font(char *, int);
#endif
#if defined(WIN32) || defined(MSDOS)
#ifdef MSDOS
#define Delay(a) msleep(a)
#endif
#define Close close
#ifndef WIN_CE
#ifdef DeleteFile
#undef DeleteFile
#endif
#define DeleteFile unlink
#endif
#ifdef WIN32
/*from windsys.c */
extern char *translate_path_variables(const char *, char *);
extern boolean get_user_home_folder(char *, size_t);
#endif
#endif
#ifdef MAC68K
#undef unlink
#define unlink macunlink
#endif
#if (defined(macintosh) && (defined(__SC__) || defined(__MRC__))) \
|| defined(__MWERKS__)
#define PRAGMA_UNUSED
#endif
#ifndef SFCTOOL
staticfn NHFILE *new_nhfile(void);
staticfn void free_nhfile(NHFILE *);
#else
NHFILE *new_nhfile(void);
void free_nhfile(NHFILE *);
#endif /* SFCTOOL */
#ifdef SELECTSAVED
staticfn int QSORTCALLBACK strcmp_wrap(const void *, const void *);
#endif
staticfn char *set_bonesfile_name(char *, d_level *);
staticfn char *set_bonestemp_name(void);
#ifdef COMPRESS
staticfn void redirect(const char *, const char *, FILE *, boolean);
#endif
#if defined(COMPRESS) || defined(ZLIB_COMP)
staticfn void docompress_file(const char *, boolean);
#endif
#if defined(ZLIB_COMP)
staticfn boolean make_compressed_name(const char *, char *);
#endif
staticfn NHFILE *problematic_savefile(int, const char *);
#ifndef SFCTOOL
staticfn int doconvert_file(const char *, int, boolean);
#endif /* SFCTOOL */
staticfn boolean make_converted_name(const char *);
#ifndef SFCTOOL
staticfn NHFILE *viable_nhfile(NHFILE *);
#ifdef SELECTSAVED
staticfn int QSORTCALLBACK strcmp_wrap(const void *, const void *);
#endif
staticfn char *set_bonesfile_name(char *, d_level *);
staticfn char *set_bonestemp_name(void);
#ifndef USE_FCNTL
staticfn char *make_lockname(const char *, char *);
#endif
staticfn FILE *fopen_wizkit_file(void);
staticfn void wizkit_addinv(struct obj *);
boolean proc_wizkit_line(char *buf);
void read_wizkit(void); /* in extern.h; why here too? */
staticfn FILE *fopen_sym_file(void);
staticfn NHFILE *viable_nhfile(NHFILE *);
#endif /* !SFCTOOL */
/* return a file's name without its path and optionally trailing 'type' */
const char *
nh_basename(const char *fname, boolean keep_suffix)
{
#ifndef VMS
static char basebuf[80];
const char *p;
if ((p = strrchr(fname, '/')) != 0)
fname = p + 1;
#if defined(WIN32) || defined(MSDOS)
if ((p = strrchr(fname, '\\')) != 0)
fname = p + 1;
#endif
if ((p = strrchr(fname, '.')) != 0 && !keep_suffix) {
size_t ln = (size_t) (p - fname);
/* note that without path, name should be reasonable length;
it is expected to refer to a source file name or run-time
configuration file name and those aren't arbitrarily long;
if "name" part of "name.suffix" is too long for 'basebuf[]',
just return that as-is without stripping off ".suffix" */
if (ln < sizeof basebuf) {
strncpy(basebuf, fname, ln);
basebuf[ln] = '\0';
fname = basebuf;
}
}
#else /* VMS */
fname = vms_basename(fname, keep_suffix);
#endif
return fname;
}
/*
* fname_encode()
*
* Args:
* legal zero-terminated list of acceptable file name characters
* quotechar lead-in character used to quote illegal characters as
* hex digits
* s string to encode
* callerbuf buffer to house result
* bufsz size of callerbuf
*
* Notes:
* The hex digits 0-9 and A-F are always part of the legal set due to
* their use in the encoding scheme, even if not explicitly included in
* 'legal'.
*
* Sample:
* The following call:
* (void) fname_encode("ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz",
* '%', "This is a % test!", buf, 512);
* results in this encoding:
* "This%20is%20a%20%25%20test%21"
*/
char *
fname_encode(
const char *legal,
char quotechar,
char *s,
char *callerbuf,
int bufsz)
{
char *sp, *op;
int cnt = 0;
static char hexdigits[] = "0123456789ABCDEF";
sp = s;
op = callerbuf;
*op = '\0';
while (*sp) {
/* Do we have room for one more character or encoding? */
if ((bufsz - cnt) <= 4)
return callerbuf;
if (*sp == quotechar) {
(void) sprintf(op, "%c%02X", quotechar, *sp);
op += 3;
cnt += 3;
} else if ((strchr(legal, *sp) != 0)
|| (strchr(hexdigits, *sp) != 0)) {
*op++ = *sp;
*op = '\0';
cnt++;
} else {
(void) sprintf(op, "%c%02X", quotechar, *sp);
op += 3;
cnt += 3;
}
sp++;
}
return callerbuf;
}
/*
* fname_decode()
*
* Args:
* quotechar lead-in character used to quote illegal characters as
* hex digits
* s string to decode
* callerbuf buffer to house result
* bufsz size of callerbuf
*/
char *
fname_decode(char quotechar, char *s, char *callerbuf, int bufsz)
{
char *sp, *op;
int k, calc, cnt = 0;
static char hexdigits[] = "0123456789ABCDEF";
sp = s;
op = callerbuf;
*op = '\0';
while (*sp) {
/* Do we have room for one more character? */
if ((bufsz - cnt) <= 2)
return callerbuf;
if (*sp == quotechar) {
sp++;
for (k = 0; k < 16; ++k)
if (*sp == hexdigits[k])
break;
if (k >= 16)
return callerbuf; /* impossible, so bail */
calc = k << 4;
sp++;
for (k = 0; k < 16; ++k)
if (*sp == hexdigits[k])
break;
if (k >= 16)
return callerbuf; /* impossible, so bail */
calc += k;
sp++;
*op++ = calc;
*op = '\0';
} else {
*op++ = *sp++;
*op = '\0';
}
cnt++;
}
return callerbuf;
}
#ifdef PREFIXES_IN_USE
#define UNUSED_if_not_PREFIXES_IN_USE /*empty*/
#else
#define UNUSED_if_not_PREFIXES_IN_USE UNUSED
#endif
/*ARGSUSED*/
const char *
fqname(const char *basenam,
int whichprefix UNUSED_if_not_PREFIXES_IN_USE,
int buffnum UNUSED_if_not_PREFIXES_IN_USE)
{
#ifdef PREFIXES_IN_USE
char *bufptr;
#endif
#ifdef WIN32
char tmpbuf[BUFSZ];
#endif
#ifndef PREFIXES_IN_USE
return basenam;
#else
if (!basenam || whichprefix < 0 || whichprefix >= PREFIX_COUNT)
return basenam;
if (!gf.fqn_prefix[whichprefix])
return basenam;
if (buffnum < 0 || buffnum >= FQN_NUMBUF) {
impossible("Invalid fqn_filename_buffer specified: %d", buffnum);
buffnum = 0;
}
bufptr = gf.fqn_prefix[whichprefix];
#ifdef WIN32
if (strchr(gf.fqn_prefix[whichprefix], '%')
|| strchr(gf.fqn_prefix[whichprefix], '~'))
bufptr = translate_path_variables(gf.fqn_prefix[whichprefix], tmpbuf);
#endif
if (strlen(bufptr) + strlen(basenam) >= FQN_MAX_FILENAME) {
impossible("fqname too long: %s + %s", bufptr, basenam);
return basenam; /* XXX */
}
Strcpy(fqn_filename_buffer[buffnum], bufptr);
return strcat(fqn_filename_buffer[buffnum], basenam);
#endif /* !PREFIXES_IN_USE */
}
#ifndef SFCTOOL
/* reasonbuf must be at least BUFSZ, supplied by caller */
int
validate_prefix_locations(char *reasonbuf)
{
#if defined(NOCWD_ASSUMPTIONS)
FILE *fp;
const char *filename;
int prefcnt, failcount = 0;
char panicbuf1[BUFSZ], panicbuf2[BUFSZ];
const char *details;
#endif
if (reasonbuf)
reasonbuf[0] = '\0';
#if defined(NOCWD_ASSUMPTIONS)
for (prefcnt = 1; prefcnt < PREFIX_COUNT; prefcnt++) {
/* don't test writing to configdir or datadir; they're readonly */
if (prefcnt == SYSCONFPREFIX || prefcnt == CONFIGPREFIX
|| prefcnt == DATAPREFIX)
continue;
filename = fqname("validate", prefcnt, 3);
if ((fp = fopen(filename, "w"))) {
fclose(fp);
(void) unlink(filename);
} else {
if (reasonbuf) {
if (failcount)
Strcat(reasonbuf, ", ");
Strcat(reasonbuf, fqn_prefix_names[prefcnt]);
}
/* the paniclog entry gets the value of errno as well */
Sprintf(panicbuf1, "Invalid %s", fqn_prefix_names[prefcnt]);
#if defined(NHSTDC) && !defined(NOTSTDC)
if (!(details = strerror(errno)))
#endif
details = "";
Sprintf(panicbuf2, "\"%s\", (%d) %s",
gf.fqn_prefix[prefcnt], errno, details);
paniclog(panicbuf1, panicbuf2);
failcount++;
}
}
if (failcount)
return 0;
else
#endif
return 1;
}
/* fopen a file, with OS-dependent bells and whistles */
/* NOTE: a simpler version of this routine also exists in util/dlb_main.c */
FILE *
fopen_datafile(const char *filename, const char *mode, int prefix)
{
FILE *fp;
filename = fqname(filename, prefix, prefix == TROUBLEPREFIX ? 3 : 0);
fp = fopen(filename, mode);
return fp;
}
#endif /* !SFCTOOL */
/* ---------- EXTERNAL FILE SUPPORT ----------- */
/* determine byte order */
static const int bei = 1;
#define IS_BIGENDIAN() ( (*(char*)&bei) == 0 )
void
init_nhfile(NHFILE *nhfp)
{
if (nhfp->structlevel) {
if (nhfp->fd != -1) {
impossible("Warning - Unclosed structlevel file being reinitialized");
(void) nhclose(nhfp->fd);
}
} else if (nhfp->fpdef) {
if (nhfp->fpdef) {
impossible("Warning - Unclosed fieldlevel file being reinitialized");
(void) fclose(nhfp->fpdef);
}
}
nhfp->fd = -1;
nhfp->fpdef = (FILE *) 0;
nhfp->mode = COUNTING;
nhfp->structlevel = TRUE;
nhfp->fieldlevel = FALSE;
nhfp->addinfo = FALSE;
nhfp->bendian = IS_BIGENDIAN();
nhfp->fplog = (FILE *) 0;
nhfp->fpdebug = (FILE *) 0;
nhfp->rcount = nhfp->wcount = 0;
nhfp->eof = FALSE;
nhfp->fnidx = 0;
nhfp->style.deflt = FALSE;
nhfp->style.binary = TRUE;
nhfp->nhfpconvert = 0;
}
#ifndef SFCTOOL
staticfn
#endif
NHFILE *
new_nhfile(void)
{
NHFILE *nhfp = (NHFILE *) alloc(sizeof *nhfp);
memset((genericptr_t) nhfp, 0, sizeof *nhfp);
init_nhfile(nhfp);
return nhfp;
}
#ifndef SFCTOOL
staticfn
#endif
void
free_nhfile(NHFILE *nhfp)
{
if (nhfp) {
init_nhfile(nhfp);
free((genericptr_t) nhfp);
}
}
void
close_nhfile(NHFILE *nhfp)
{
if (nhfp->structlevel && nhfp->fd != -1)
(void) nhclose(nhfp->fd), nhfp->fd = -1;
else if (nhfp->fpdef)
(void) fclose(nhfp->fpdef), nhfp->fpdef = (FILE *) 0;
if (nhfp->fplog)
(void) fprintf(nhfp->fplog, "# closing\n");
if (nhfp->fplog)
(void) fclose(nhfp->fplog);
if (nhfp->fpdebug)
(void) fclose(nhfp->fpdebug);
free_nhfile(nhfp);
}
void
rewind_nhfile(NHFILE *nhfp)
{
if (nhfp->structlevel) {
#ifdef BSD
(void) lseek(nhfp->fd, 0L, 0);
#else
(void) lseek(nhfp->fd, (off_t) 0, 0);
#endif
} else {
rewind(nhfp->fpdef);
}
}
#ifndef SFCTOOL
staticfn NHFILE *
viable_nhfile(NHFILE *nhfp)
{
/* perform some sanity checks before returning
the pointer to the nethack file descriptor */
if (nhfp) {
/* check for no open file at all,
* not a structlevel legacy file,
* nor a fieldlevel file.
*/
if (((nhfp->fd == -1) && !nhfp->fpdef)
|| (nhfp->structlevel && nhfp->fd < 0)
|| (nhfp->fieldlevel && !nhfp->fpdef)) {
/* not viable, start the cleanup */
if (nhfp->fieldlevel) {
if (nhfp->fpdef) {
(void) fclose(nhfp->fpdef);
nhfp->fpdef = (FILE *) 0;
}
if (nhfp->fplog) {
(void) fprintf(nhfp->fplog, "# closing, not viable\n");
(void) fclose(nhfp->fplog);
}
if (nhfp->fpdebug)
(void) fclose(nhfp->fpdebug);
}
free_nhfile(nhfp);
nhfp = (NHFILE *) 0;
}
}
return nhfp;
}
#endif /* !SFCTOOL */
int
nhclose(int fd)
{
int retval = 0;
if (fd >= 0) {
if (close_check(fd))
bclose(fd);
else
retval = close(fd);
}
return retval;
}
/* ---------- BEGIN LEVEL FILE HANDLING ----------- */
#ifndef SFCTOOL
/* Construct a file name for a level-type file, which is of the form
* something.level (with any old level stripped off).
* This assumes there is space on the end of 'file' to append
* a two digit number. This is true for 'level'
* but be careful if you use it for other things -dgk
*/
void
set_levelfile_name(char *file, int lev)
{
char *tf;
tf = strrchr(file, '.');
if (!tf)
tf = eos(file);
Sprintf(tf, ".%d", lev);
#ifdef VMS
Strcat(tf, ";1");
#endif
return;
}
NHFILE *
create_levelfile(int lev, char errbuf[])
{
const char *fq_lock;
NHFILE *nhfp = (NHFILE *) 0;
if (errbuf)
*errbuf = '\0';
set_levelfile_name(gl.lock, lev);
fq_lock = fqname(gl.lock, LEVELPREFIX, 0);
nhfp = new_nhfile();
if (nhfp) {
nhfp->ftype = NHF_LEVELFILE;
nhfp->mode = WRITING;
nhfp->structlevel = TRUE; /* do set this TRUE for levelfiles */
nhfp->fieldlevel = FALSE; /* don't set this TRUE for levelfiles */
nhfp->addinfo = FALSE;
nhfp->style.deflt = FALSE;
nhfp->style.binary = TRUE;
nhfp->fnidx = historical;
nhfp->fd = -1;
nhfp->fpdef = (FILE *) 0;
#if defined(MICRO) || defined(WIN32)
/* Use O_TRUNC to force the file to be shortened if it already
* exists and is currently longer.
*/
nhfp->fd = open(fq_lock, O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
FCMASK);
#else
#ifdef MAC68K
nhfp->fd = maccreat(fq_lock, LEVL_TYPE);
#else
nhfp->fd = creat(fq_lock, FCMASK);
#endif
#endif /* MICRO || WIN32 */
if (nhfp->fd >= 0)
svl.level_info[lev].flags |= LFILE_EXISTS;
else if (errbuf) /* failure explanation */
Sprintf(errbuf,
"Cannot create file \"%s\" for level %d (errno %d).",
gl.lock, lev, errno);
#if defined(MSDOS) || defined(WIN32)
if (nhfp->fd >= 0)
(void) setmode(nhfp->fd, O_BINARY);
#endif
}
nhfp = viable_nhfile(nhfp);
return nhfp;
}
NHFILE *
open_levelfile(int lev, char errbuf[])
{
const char *fq_lock;
NHFILE *nhfp = (NHFILE *) 0;
if (errbuf)
*errbuf = '\0';
set_levelfile_name(gl.lock, lev);
fq_lock = fqname(gl.lock, LEVELPREFIX, 0);
nhfp = new_nhfile();
if (nhfp) {
nhfp->mode = READING;
nhfp->structlevel = TRUE; /* do set this TRUE for levelfiles */
nhfp->fieldlevel = FALSE; /* do not set this TRUE for levelfiles */
nhfp->addinfo = FALSE;
nhfp->style.deflt = FALSE;
nhfp->style.binary = TRUE;
nhfp->ftype = NHF_LEVELFILE;
nhfp->fnidx = historical;
nhfp->fd = -1;
nhfp->fpdef = (FILE *) 0;
}
if (nhfp && nhfp->structlevel) {
#ifdef MAC68K
nhfp->fd = macopen(fq_lock, O_RDONLY | O_BINARY, LEVL_TYPE);
#else
nhfp->fd = open(fq_lock, O_RDONLY | O_BINARY, 0);
#endif
/* for failure, return an explanation that our caller can use;
settle for `lock' instead of `fq_lock' because the latter
might end up being too big for nethack's BUFSZ */
if (nhfp->fd < 0 && errbuf)
Sprintf(errbuf,
"Cannot open file \"%s\" for level %d (errno %d).",
gl.lock, lev, errno);
#if defined(MSDOS) || defined(WIN32)
if (nhfp->fd >= 0)
(void) setmode(nhfp->fd, O_BINARY);
#endif
}
nhfp = viable_nhfile(nhfp);
return nhfp;
}
void
delete_levelfile(int lev)
{
/*
* Level 0 might be created by port specific code that doesn't
* call create_levfile(), so always assume that it exists.
*/
if (lev == 0 || (svl.level_info[lev].flags & LFILE_EXISTS)) {
set_levelfile_name(gl.lock, lev);
(void) unlink(fqname(gl.lock, LEVELPREFIX, 0));
svl.level_info[lev].flags &= ~LFILE_EXISTS;
}
}
void
clearlocks(void)
{
int x;
#ifdef HANGUPHANDLING
if (program_state.preserve_locks)
return;
#endif
#ifndef NO_SIGNAL
(void) signal(SIGINT, SIG_IGN);
#if defined(UNIX) || defined(VMS)
sethanguphandler((void (*)(int)) SIG_IGN);
#endif
#endif /* NO_SIGNAL */
/* can't access maxledgerno() before dungeons are created -dlc */
for (x = (svn.n_dgns ? maxledgerno() : 0); x >= 0; x--)
delete_levelfile(x); /* not all levels need be present */
}
#if defined(SELECTSAVED)
/* qsort comparison routine */
staticfn int QSORTCALLBACK
strcmp_wrap(const void *p, const void *q)
{
return strcmp(*(char **) p, *(char **) q);
}
#endif
/* ---------- END LEVEL FILE HANDLING ----------- */
/* ---------- BEGIN BONES FILE HANDLING ----------- */
/* set up "file" to be file name for retrieving bones, and return a
* bonesid to be read/written in the bones file.
*/
staticfn char *
set_bonesfile_name(char *file, d_level *lev)
{
s_level *sptr;
char *dptr;
/*
* "bonD0.nn" = bones for level nn in the main dungeon;
* "bonM0.T" = bones for Minetown;
* "bonQBar.n" = bones for level n in the Barbarian quest;
* "bon3D0.nn" = \
* "bon3M0.T" = > same as above, but for bones pool #3.
* "bon3QBar.n" = /
*
* Return value for content validation skips "bon" and the
* pool number (if present), making it feasible for the admin
* to manually move a bones file from one pool to another by
* renaming it.
*/
Strcpy(file, "bon");
#ifdef SYSCF
if (sysopt.bones_pools > 1) {
unsigned poolnum = min((unsigned) sysopt.bones_pools, 10);
poolnum = (unsigned) ubirthday % poolnum; /* 0..9 */
Sprintf(eos(file), "%u", poolnum);
}
#endif
dptr = eos(file);
/* when this naming scheme was adopted, 'filecode' was one letter;
3.3.0 turned it into a three letter string for quest levels */
Sprintf(dptr, "%c%s", svd.dungeons[lev->dnum].boneid,
In_quest(lev) ? gu.urole.filecode : "0");
if ((sptr = Is_special(lev)) != 0)
Sprintf(eos(dptr), ".%c", sptr->boneid);
else
Sprintf(eos(dptr), ".%d", lev->dlevel);
#ifdef VMS
Strcat(dptr, ";1");
#endif
return dptr;
}
/* set up temporary file name for writing bones, to avoid another game's
* trying to read from an uncompleted bones file. we want an uncontentious
* name, so use one in the namespace reserved for this game's level files.
* (we are not reading or writing level files while writing bones files, so
* the same array may be used instead of copying.)
*/
staticfn char *
set_bonestemp_name(void)
{
char *tf;
tf = strrchr(gl.lock, '.');
if (!tf)
tf = eos(gl.lock);
Sprintf(tf, ".bn");
#ifdef VMS
Strcat(tf, ";1");
#endif
return gl.lock;
}
NHFILE *
create_bonesfile(d_level *lev, char **bonesid, char errbuf[])
{
const char *file;
NHFILE *nhfp = (NHFILE *) 0;
int failed = 0;
#if defined(WIN32)
errno_t err;
#endif
if (errbuf)
*errbuf = '\0';
*bonesid = set_bonesfile_name(gb.bones, lev);
file = set_bonestemp_name();
file = fqname(file, BONESPREFIX, 0);
nhfp = new_nhfile();
if (nhfp) {
nhfp->ftype = NHF_BONESFILE;
nhfp->mode = WRITING;
nhfp->structlevel = TRUE;
nhfp->fieldlevel = FALSE;
nhfp->addinfo = TRUE;
nhfp->style.deflt = TRUE;
nhfp->style.binary = TRUE;
nhfp->fnidx = historical;
nhfp->fd = -1;
nhfp->fpdef = (FILE *) 0;
if (nhfp->fpdef) {
#ifdef SAVEFILE_DEBUGGING
nhfp->fpdebug = fopen("create_bonesfile-debug.log", "a");
#endif
} else {
failed = errno;
}
if (nhfp->structlevel) {
#if defined(MICRO)
/* Use O_TRUNC to force the file to be shortened if it already
* exists and is currently longer.
*/
nhfp->fd = open(file,
O_WRONLY | O_CREAT | O_TRUNC | O_BINARY, FCMASK);
#elif defined(WIN32)
err = _sopen_s(&nhfp->fd, file,
O_WRONLY | O_CREAT | O_TRUNC | O_BINARY,
_SH_DENYRW, _S_IREAD | _S_IWRITE);
#else /* ?MICRO || WIN32 */
/* implies UNIX or MAC68K (MAC68K is for OS9 or earlier) */
#ifdef MAC68K
nhfp->fd = maccreat(file, BONE_TYPE);
#else
nhfp->fd = creat(file, FCMASK);
#endif /* ?MAC68K */
#endif /* ?MICRO || WIN32 */
if (nhfp->fd < 0)
failed = errno;
#if defined(MSDOS) || defined(WIN32)
if (nhfp->fd >= 0)
(void) setmode(nhfp->fd, O_BINARY);
#endif
}
if (failed && errbuf) /* failure explanation */
Sprintf(errbuf, "Cannot create bones \"%s\", id %s (errno %d).",
gl.lock, *bonesid, errno);
}
#if defined(VMS) && !defined(SECURE)
/*
Re-protect bones file with world:read+write+execute+delete access.
umask() doesn't seem very reliable; also, vaxcrtl won't let us set
delete access without write access, which is what's really wanted.
Can't simply create it with the desired protection because creat
ANDs the mask with the user's default protection, which usually
denies some or all access to world.
*/
(void) chmod(file, FCMASK | 007); /* allow other users full access */
#endif /* VMS && !SECURE */
nhfp = viable_nhfile(nhfp);
return nhfp;
}
/* move completed bones file to proper name */
void
commit_bonesfile(d_level *lev)
{
const char *fq_bones, *tempname;
int ret;
(void) set_bonesfile_name(gb.bones, lev);
fq_bones = fqname(gb.bones, BONESPREFIX, 0);
tempname = set_bonestemp_name();
tempname = fqname(tempname, BONESPREFIX, 1);
#if (defined(SYSV) && !defined(SVR4)) || defined(GENIX)
/* old SYSVs don't have rename. Some SVR3's may, but since they
* also have link/unlink, it doesn't matter. :-)
*/
(void) unlink(fq_bones);
ret = link(tempname, fq_bones);
ret += unlink(tempname);
#else
ret = rename(tempname, fq_bones);
#endif
if (wizard && ret != 0)
pline("couldn't rename %s to %s.", tempname, fq_bones);
}
NHFILE *
open_bonesfile(d_level *lev, char **bonesid)
{
const char *fq_bones;
NHFILE *nhfp = (NHFILE *) 0;
#if defined(WIN32)
errno_t err UNUSED;
#endif
*bonesid = set_bonesfile_name(gb.bones, lev);
fq_bones = fqname(gb.bones, BONESPREFIX, 0);
nh_uncompress(fq_bones); /* no effect if nonexistent */
nhfp = new_nhfile();
if (nhfp) {
#if defined(WIN32) && defined(DEBUG)
if (nhfp->fd >= 0)
impossible("bones file NHFILE * has odd fd (%d)", nhfp->fd);
#endif
nhfp->structlevel = TRUE;
nhfp->fieldlevel = FALSE;
nhfp->ftype = NHF_BONESFILE;
nhfp->mode = READING;
nhfp->addinfo = TRUE;
nhfp->style.deflt = TRUE;
nhfp->style.binary = (sysopt.bonesformat[0] != exportascii);
nhfp->fnidx = sysopt.bonesformat[0];
nhfp->fd = -1;
nhfp->fpdef = (FILE *) 0;
if (nhfp->fpdef) {
#ifdef SAVEFILE_DEBUGGING
nhfp->fpdebug = fopen("open_bonesfile-debug.log", "a");
#endif
}
if (nhfp->structlevel) {
#if defined(MAC68K)
nhfp->fd = macopen(fq_bones, O_RDONLY | O_BINARY, BONE_TYPE);
#elif defined(WIN32)
err = _sopen_s(&nhfp->fd, fq_bones, _O_RDONLY | _O_BINARY,
_SH_DENYRW, _S_IREAD | _S_IWRITE);
#else
nhfp->fd = open(fq_bones, O_RDONLY | O_BINARY, 0);
#endif
#if defined(MSDOS) || defined(WIN32)
if (nhfp->fd >= 0)
(void) setmode(nhfp->fd, O_BINARY);
#endif
}
}
nhfp = viable_nhfile(nhfp);
return nhfp;
}
int
delete_bonesfile(d_level *lev)
{
int reslt;
(void) set_bonesfile_name(gb.bones, lev);
reslt = unlink(fqname(gb.bones, BONESPREFIX, 0));
delete_convertedfile(fqname(gb.bones, BONESPREFIX, 0));
return !(reslt < 0);
}
/* assume we're compressing the recently read or created bonesfile, so the
* file name is already set properly */