-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfileio.c
More file actions
1411 lines (1204 loc) · 28.6 KB
/
fileio.c
File metadata and controls
1411 lines (1204 loc) · 28.6 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
/*
* The routines in this file read and write ASCII files from the disk. All of
* the knowledge about files are here.
*/
#include <stdio.h>
#include "estruct.h"
#include "edef.h"
#if USG | BSD | (MSDOS & (MSC | GNUC | ZORTECH))
#include <sys/types.h>
#endif
#if USG | BSD | (MSDOS & (MSC | TURBO | GNUC | ZORTECH))
#include <sys/stat.h>
#endif
#if MSDOS & (C86)
#include <stat.h>
#endif
#if VMS
#include <stat.h>
#include <fab.h>
#include <rab.h>
#include <rmsdef.h>
/* names for record attributes and record formats */
CONST int rms_maxrat = 3;
CONST char *rms_rat[] = {"ftn", "cr", "prn", "blk"};
CONST int rms_maxrfm = 6;
CONST char *rms_rfm[] =
/* {"udf", "fix", "var", "vfc", "stm", "stmlf", "stmcr"} */
{"stmlf","stmlf","var", "vfc", "stm", "stmlf", "stmcr"};
#define successful(s) ((s) & 1)
#define unsuccessful(s) (!successful(s))
static struct FAB fab; /* a file access block */
static struct RAB rab; /* a record access block */
#endif
#if USG | FREEBSD2
#include <fcntl.h>
#endif
#if FREEBSD2 || defined(__linux__)
#include <unistd.h>
#endif
#ifndef O_RDONLY
#define O_RDONLY 0
#endif
#ifndef O_WRONLY
#define O_WRONLY 1
#endif
#ifndef O_APPEND
#define O_APPEND 0010
#endif
/* Make getc and putc macroes if they are missing */
#ifndef getc
#define getc(f) fgetc(f)
#endif
#ifndef putc
#define putc(c,f) fputc(c,f)
#endif
#if defined(_IOFBF) && defined(BUFSIZ) && (defined(M_UNIX) || !defined(M_XENIX))
#define HAS_SETVBUF 1
#else
#define HAS_SETVBUF 0
#endif
static FILE *ffp; /* File pointer, all functions. */
static int eofflag; /* end-of-file flag */
#define isbinary ((curbp->b_mode&MDBINARY) != 0)
#if SYMEXPAND
/* expand $ symbols in file names
*
* name = { segment }
* segment = <any character but $>
* % maps to the character
* | "$$"
* % maps to a single "$"
* | "$" name | "$(" name ")" | "${" name "}"
* % maps to the environment value of 'name'
* % do not count on treatment of trailing blanks in environment symbols
* name = { <letter or digit or _> }
*
* char *symexpand(name)
* char *name;
*
* For an indirect symbol replacement with passes > 1, the first symbol
* may need to have the second symbol inside $() to produce the desired results.
*
* name is left unchanged.
* symexpand returns a pointer to a static area.
*
* For example,
* str = symexpand("$(bin)name");
* where
* bin = $(home)bin/
* home = /u/william/
*
*/
static char *symexpand PP((CONSTA char *inname));
static char *symexpand(inname)
CONSTA char *inname;
{
int i, j, len, addlen, save, more, passes, maxlen;
char paren;
char sym[NFILEN];
CONST char *value;
static char FAR name[NFILEN];
maxlen = NFILEN - 1;
bytecopy(name, inname, maxlen-1);
passes = 20;
more = 1;
while (passes-- > 0 && more) {
more = 0;
for(i = 0; i < maxlen && name[i] != '\0'; i++) {
if (name[i] == '$') {
more = 1;
save = i;
i++;
if (name[i] == '$') {
for(j=i; j+1 < maxlen && name[j+1]; j++)
name[j] = name[j+1];
name[j] = '\0';
--i;
continue;
}
j = 0;
if (name[i] == '(') {paren = ')'; i++;}
else if (name[i] == '{') {paren = '}'; i++;}
else paren = 0;
while (name[i] != '\0' && name[i] != paren &&
(paren ||
isletter(name[i]) ||
(name[i] >= '0' && name[i] <= '9') ||
name[i] == '_')) {
if (j < (int) sizeof(sym))
sym[j++] = name[i];
i++;
}
if (paren != 0 && name[i] == paren)
i++;
if (j < (int) sizeof(sym))
sym[j] = '\0';
value = getenv(sym);
if (value == NULL)
value = "";
len = (int) strlen(value);
if (i + len >= maxlen)
len = maxlen - i - 1;
addlen = len - (i - save);
if (addlen > 0) {
for(j = maxlen-addlen-1; j >= i; j--)
name[j + addlen] = name[j];
} else if (addlen < 0) {
addlen = -addlen;
for(j = i-addlen; j+addlen < maxlen; j++)
name[j] = name[j + addlen];
name[j] = '\0';
}
i = save;
for(j = 0; j < len; j++)
name[i++] = value[j];
--i;
}
}
if (i >= maxlen) i = maxlen - 1;
name[i] = '\0';
}
return(name);
}
#endif
/*
* Open a file for reading.
*/
int ffropen(fn, getstat)
CONSTA char *fn;
int getstat;
{
#if VMS | USG | BSD
int ffd; /* File descriptor */
struct stat s;
#if VMS
unsigned long status;
int maxlen;
#endif
#if SYMEXPAND
fn = symexpand(fn);
#endif
#if VMS
if (!isbinary) {
/* initialize structures */
fab=cc$rms_fab;
rab=cc$rms_rab;
fab.fab$l_fna = fn;
fab.fab$b_fns = strlen(fn);
fab.fab$b_fac = FAB$M_GET;
fab.fab$b_shr = FAB$M_SHRGET;
fab.fab$l_fop = FAB$M_SQO;
rab.rab$l_fab = &fab;
rab.rab$l_rop = RAB$M_RAH; /* read-ahead for multibuffering */
status=SYS$OPEN(&fab);
if (status==RMS$_FLK)
{
/*
* File locking problem:
* Add the SHRPUT option, allowing shareability
* with other writers. This lets us read batch
* logs and stuff like that. I don't turn it on
* automatically since adding this sharing
* eliminates the read-ahead
*/
fab.fab$b_shr |= FAB$M_SHRPUT;
status=SYS$OPEN(&fab);
}
if (unsuccessful(status))
return(FIOFNF);
if (unsuccessful(SYS$CONNECT(&rab)))
{
SYS$CLOSE(&fab);
return(FIOFNF);
}
if (getstat) {
if (stat(fn, &s) == 0)
curbp->b_fmode = s.st_mode;
curbp->b_fab_rfm = fab.fab$b_rfm;
curbp->b_fab_rat = fab.fab$b_rat;
curbp->b_fab_fsz = fab.fab$b_fsz;
curbp->b_fab_mrs = fab.fab$w_mrs;
}
maxlen = fab.fab$w_mrs?fab.fab$w_mrs+1:32768;
if (maxlen < NSTRING) maxlen = NSTRING;
if (fline == NULL || flen < maxlen) {
if (fline != NULL) {
free(fline);
fline = NULL;
}
fline = malloc(flen = maxlen);
}
return(FIOSUC);
}
#endif
ffd = open(fn, O_RDONLY);
if (getstat && fstat(ffd, &s) == 0) {
curbp->b_fmode = s.st_mode;
#if VMS
curbp->b_fab_rfm = s.st_fab_rfm;
curbp->b_fab_rat = s.st_fab_rat;
curbp->b_fab_fsz = s.st_fab_fsz;
curbp->b_fab_mrs = s.st_fab_mrs;
#endif
}
if (ffd < 0 || (ffp=fdopen(ffd, "r")) == NULL)
#else
#if MSDOS
if ((ffp=fopen(fn, (isbinary? "rb": "r"))) == NULL)
#else
if ((ffp=fopen(fn, "r")) == NULL)
#endif
#endif
return (FIOFNF);
#if HAS_SETVBUF
setvbuf(ffp, NULL, _IOFBF, (BUFSIZ > 4096? BUFSIZ: 4096));
#endif
eofflag = FALSE;
return (FIOSUC);
}
/*
* Open a file for writing. Return TRUE if all is well, and FALSE on error
* (cannot create).
*/
int ffwopen(fn, mode)
CONSTA char *fn;
CONSTA char *mode; /* mode to open file for */
{
#if VMS | USG | BSD
int ffd; /* File descriptor */
#if VMS
int i, n;
char ratstr[80], rfmstr[80];
unsigned long status;
if (!isbinary) {
/* initialize structures */
fab=cc$rms_fab;
rab=cc$rms_rab;
fab.fab$l_fna = fn;
fab.fab$b_fns = strlen(fn);
fab.fab$b_fac = FAB$M_PUT; /* writing this file */
fab.fab$b_shr = FAB$M_NIL; /* no other writers */
fab.fab$l_fop = FAB$M_SQO; /* sequential ops only */
fab.fab$b_rat = curbp->b_fab_rat;
fab.fab$b_rfm = curbp->b_fab_rfm;
fab.fab$b_fsz = curbp->b_fab_fsz;
if (fab.fab$b_rfm == FAB$C_FIX)
fab.fab$w_mrs = curbp->b_fab_mrs;
rab.rab$l_fab = &fab;
rab.rab$l_rop = RAB$M_WBH; /* write behind - multibuffer */
if (*mode == 'a')
{
/* append mode */
rab.rab$l_rop = RAB$M_EOF;
status=SYS$OPEN(&fab);
if (status == RMS$_FNF)
status=SYS$CREATE(&fab);
}
else /* *mode == 'w' */
{
/* write mode */
fab.fab$l_fop |= FAB$M_MXV; /* always make a new version */
status=SYS$CREATE(&fab);
}
if (successful(status))
{
status=SYS$CONNECT(&rab);
if (unsuccessful(status)) SYS$CLOSE(&fab);
}
if (unsuccessful(status)) {
mlwrite("Cannot open file for writing [RMS %d]",status);
return(FIOERR);
}
return(FIOSUC);
}
n = curbp->b_fab_rfm;
if (n < 0 || n > rms_maxrfm) n = 2;
sprintf(rfmstr, "rfm=%s", rms_rfm[ n ]);
n = 0;
strcpy(ratstr, "rat=");
for(i = 0; i <= rms_maxrat; i++)
if (curbp->b_fab_rat & (1 << i)) {
if (n++ > 0) strcat(ratstr, ",");
strcat(ratstr, rms_rat[i]);
}
if (n == 0) strcat(ratstr, rms_rat[1]);
ffd = creat(fn, curbp->b_fmode, rfmstr, ratstr);
#else
#if SYMEXPAND
fn = symexpand(fn);
#endif
ffd = (*mode == 'w'?
creat(fn, curbp->b_fmode):
open(fn, (int) (O_WRONLY|O_APPEND), curbp->b_fmode));
#endif
if (ffd < 0 || (ffp=fdopen(ffd, mode)) == NULL) {
#else
#if MSDOS
char newmode[3];
newmode[0] = mode[0];
newmode[1] = (isbinary? 'b': '\0');
newmode[2] = '\0';
if ((ffp=fopen(fn, newmode)) == NULL) {
#else
if ((ffp=fopen(fn, mode)) == NULL) {
#endif
#endif
mlwrite("Cannot open file for writing");
return (FIOERR);
}
#if HAS_SETVBUF
setvbuf(ffp, NULL, _IOFBF, (BUFSIZ > 4096? BUFSIZ: 4096));
#endif
return (FIOSUC);
}
/*
* Close a file. Should look at the status in all systems.
*/
static int ffclose PP(( int for_writing ));
static int ffclose(for_writing)
int for_writing;
{
#if VMS
unsigned long status;
#endif
/* free this since we do not need it anymore */
if (fline) {
free(fline);
fline = NULL;
}
#if MSDOS & CTRLZ
if (for_writing) {
putc(26, ffp); /* add a ^Z at the end of the file */
}
#endif
#if VMS
if (!isbinary) {
status = SYS$DISCONNECT(&rab);
if (successful(status)) status = SYS$CLOSE(&fab);
else SYS$CLOSE(&fab);
if (unsuccessful(status)) {
mlwrite("Error closing file [RMS %d]", status);
return(FIOERR);
}
return(FIOSUC);
}
#endif
#if V7 | USG | BSD | (MSDOS & (LATTICE | MSC | TURBO | C86 | GNUC | ZORTECH))
if (fclose(ffp) != FALSE) {
mlwrite("Error closing file");
return(FIOERR);
}
if (for_writing) {
#if MSDOS
#if !GNUC
bdos(0x0D,0,0); /* force MSDOS file system update */
#endif
#endif
#if V7 | USG | BSD
#if defined(__sun)
/* skip sync */
#else
#if defined(__INTERIX) || defined(__MINGW32__)
/* sync not present */
#else
if ( dosync ) {
sync(); /* force Unix file system update */
sync();
}
#endif
#endif
#endif
}
return(FIOSUC);
#else
fclose(ffp);
return (FIOSUC);
#endif
}
/*
* Close a file opened for reading
*/
int ffrclose()
{
return ffclose( 0 );
}
/*
* Close a file opened for writing
*/
int ffwclose()
{
return ffclose( 1 );
}
/*
* Write a line to the already opened file. The "buf" points to the buffer,
* and the "nbuf" is its length, less the free newline. Return the status.
* Check only at the newline.
*/
int ffputline(buf, nbuf)
char *buf;
int nbuf;
{
register int i;
char *obuf;
obuf = buf;
#if CRYPT
if (cryptflag) {
/* get a reasonable buffer */
if (fline && flen < nbuf)
{
free(fline);
fline = NULL;
}
if (fline == NULL &&
(fline=malloc(flen = nbuf+NSTRING)) == NULL)
return(FIOMEM);
/* copy data */
for(i = 0; i < nbuf; i++)
fline[i] = buf[i];
/* repoint output buffer */
obuf=fline;
me_crypt(obuf, nbuf);
}
#endif
#if VMS
if (!isbinary) {
/* set output buffer */
rab.rab$l_rbf = obuf;
rab.rab$w_rsz = nbuf;
i = SYS$PUT(&rab);
if (unsuccessful(i)) {
mlwrite("Write I/O error [RMS %d]", i);
return(FIOERR);
}
return(FIOSUC);
}
#endif
#if USG | BSD | MSDOS
if (nbuf > 0)
fwrite(obuf, 1, nbuf, ffp);
#else
for (i = 0; i < nbuf; ++i)
putc(obuf[i]&0xFF, ffp);
#endif
#if ST520
putc('\r', ffp);
#endif
putc('\n', ffp);
if (ferror(ffp)) {
mlwrite("Write I/O error");
return (FIOERR);
}
return (FIOSUC);
}
/*
* Read a line from a file, and store the bytes in the supplied buffer. The
* "nbuf" is the length of the buffer. Complain about long lines and lines
* at the end of the file that don't have a newline present. Check for I/O
* errors too. Return status.
*/
int ffgetline()
{
register int c; /* current character read */
register int i; /* current index into fline */
register char *tmpline; /* temp storage for expanding line */
int tmplen;
FILE *fp;
#if VMS
unsigned long status;
#endif
frlen = 0;
fp = ffp; /* avoid reference to extern variable */
#if VMS
if (!isbinary) {
if (fline == NULL)
return(FIOMEM);
/* read the line in */
rab.rab$l_ubf=fline;
rab.rab$w_usz=flen;
status=SYS$GET(&rab);
if (status == RMS$_EOF) return(FIOEOF);
if (unsuccessful(status)) {
mlwrite("File read error [RMS %d]", status);
return(FIOERR);
}
/* terminate and decrypt the string */
frlen = rab.rab$w_rsz;
fline[frlen] = 0;
#if CRYPT
if (cryptflag)
me_crypt(fline, frlen);
#endif
return(FIOSUC);
}
#endif
/* if we are at the end...return it */
if (eofflag)
return(FIOEOF);
/* dump fline if it ended up too big */
if (sizeof(int) < 4 && flen > 2 * NSTRING && fline != NULL) {
free(fline);
fline = NULL;
}
/* if we don't have an fline, allocate one */
if (fline == NULL)
if ((fline = malloc(flen = NSTRING)) == NULL)
return(FIOMEM);
/* read the line in */
i = 0;
#if defined(M_UNIX)
/* stdio hack, probably not worth the effort... */
if ((fp->__flag & (_IORW | _IOREAD)) == 0) {
#if !defined(_SCO_DS)
errno = _EBADF;
#endif
c = EOF;
} else {
tmplen = fp->__cnt;
if (tmplen >= flen) tmplen = flen-1;
while (i < tmplen && fp->__ptr[i] != '\n') {
i++;
}
if (i > 0) {
memcpy(fline, fp->__ptr, i);
fp->__cnt -= i;
fp->__ptr += i;
}
if (i < tmplen) {
c = '\n';
fp->__cnt--;
fp->__ptr++;
} else {
while (--fp->__cnt < 0 ?
((c = _filbuf(fp)) != EOF && c != '\n') :
((c = (int) *fp->__ptr++) != '\n'))
#else
while ((c = getc(fp)) != EOF && c != '\n')
#endif
{
fline[i++] = (char) c;
/* if it's longer, get more room */
if (i >= flen) {
tmplen = (sizeof(int) < 4? flen+NSTRING: flen+flen);
if ((tmpline = malloc(tmplen)) == NULL)
return(FIOMEM);
if (flen > 0) memcpy(tmpline, fline, flen);
i = flen;
flen = tmplen;
free(fline);
fline = tmpline;
}
}
#if defined(M_UNIX)
}
}
#endif
#if ST520
if(fline[i-1] == '\r')
i--;
#endif
/* test for any errors that may have occured */
if (c == EOF) {
if (ferror(fp)) {
mlwrite("File read error");
return(FIOERR);
}
if (i != 0)
eofflag = TRUE;
else
return(FIOEOF);
}
/* terminate and decrypt the string */
frlen = i;
fline[i] = 0;
#if CRYPT
if (cryptflag)
me_crypt(fline, frlen);
#endif
return(FIOSUC);
}
int fexist(fname) /* does <fname> exist on disk? */
char *fname; /* file to check for existance */
{
FILE *fp;
#if SYMEXPAND
fname = symexpand(fname);
#endif
/* try to open the file for reading */
fp = fopen(fname, "r");
/* if it fails, just return false! */
if (fp == NULL)
return(FALSE);
/* otherwise, close it and report true */
fclose(fp);
return(TRUE);
}
#if AZTEC & MSDOS
#undef fgetc
/* a1getc: Get an ascii char from the file input stream
but DO NOT strip the high bit
*/
int a1getc(fp)
FILE *fp;
{
int c; /* translated character */
c = getc(fp); /* get the character */
/* if its a <LF> char, throw it out */
while (c == 10)
c = getc(fp);
/* if its a <RETURN> char, change it to a LF */
if (c == '\r')
c = '\n';
/* if its a ^Z, its an EOF */
if (c == 26)
c = EOF;
return(c);
}
#endif
#if ABACKUP
/*
* Test if a file is newer than its auto-saved version
*/
int ffisnewer(fn)
char *fn;
{
#if VMS | USG | BSD | (MSDOS & (MSC | TURBO | C86 | GNUC | ZORTECH))
char asvname[NFILEN + 4];
#if C86
struct fbuf asvstat, fnstat;
#else
struct stat asvstat, fnstat;
#endif
makeasvname(asvname, fn);
if (stat(asvname, &asvstat) != 0) return(TRUE);
if (stat(fn, &fnstat) != 0) return(FALSE);
if (asvstat.st_ctime > fnstat.st_ctime) return(FALSE);
#endif
return(TRUE);
}
#endif
#ifdef NEEDCOMPL
#undef NEEDCOMPL
#endif
#define NEEDCOMPL 1
/* FILE Directory routines */
#if (BCOMPL & (USG | BSD))
#undef NEEDCOMPL
#define NEEDCOMPL 0
#if USG | FREEBSD2
# if USGV3 || FREEBSD2 || defined(__AUX__) || defined(sun)
# include <dirent.h>
# define direct dirent
# else
# include <sys/ndir.h>
# endif
#else
# include <sys/dir.h>
#endif
static DIR *dirptr = NULL; /* pointer to the current directory being searched */
static char FAR path[NFILEN]; /* path of file to find */
static char FAR rbuf[NFILEN]; /* return file buffer */
static char *nameptr; /* ptr past end of path in rbuf */
static int namelen; /* length we can write into nameptr */
static int zapdot; /* true if ./ must be removed */
#if SYMEXPAND
static int pathlen; /* length of expanded path */
#endif
static int in_arg;
static char FAR argbuf[NFILEN];
static int arglen;
/* do a wild card directory search (for file name completion) */
char *getffile(fspec, match_len, exact, is_arg)
CONSTA char *fspec; /* pattern to match */
int match_len;
int exact;
int is_arg;
{
register int index; /* index into various strings */
/* first parse the file path off the file spec */
bytecopy(path, fspec, NFILEN-2);
in_arg = is_arg;
if (is_arg) {
/* only for testing expandarg under unix */
index = 0;
while (path[index] != '\0' && path[index] != '*')
index++;
path[index] = '\0';
strcpy(argbuf, path);
arglen = index;
}
index = (int) (strlen(path) - 1);
while (index >= 0 && path[index] != '/' &&
path[index] != '\\' && path[index] != ':')
--index;
zapdot = (index < 0);
if (zapdot) { index = 0; path[0] = '.'; }
if (path[index] == '/') {
if (index > 0) --index;
else { zapdot = 1; index = 1; path[1] = '.'; }
}
path[index+1] = '\0';
if (path[0] == '/' && path[1] == '/')
return(NULL);
/* open the directory pointer */
if (dirptr != NULL) {
closedir(dirptr);
dirptr = NULL;
}
strcpy(rbuf, path);
#if SYMEXPAND
strcpy(path, symexpand(path));
#endif
dirptr = opendir(path);
if (dirptr == NULL)
return(NULL);
rbuf[index+1] = '/';
nameptr = &rbuf[index+2];
namelen = NFILEN - 4 - index;
#if SYMEXPAND
pathlen = (int) strlen(path);
path[pathlen] = '/';
if (pathlen > index+1) namelen = NFILEN - 3 - pathlen;
#endif
if (namelen <= 0)
return(NULL);
/* and call for the first file */
return(getnfile(fspec, match_len, exact));
}
/*
** Find the next name
** match_name, match_len and exact are only suggestions to reduce the
** number of calls to stat().
** The caller is responsible for doing the full comparisons.
*/
char *getnfile(match_name, match_len, exact)
CONSTA char *match_name;
int match_len;
int exact;
{
register struct direct *dp; /* directory entry pointer */
register int index; /* index into various strings */
struct stat fstatus;
char *rptr;
if (match_len > 0 && match_name[ match_len-1 ] == '/') {
match_len--;
}
/* and call for the next file */
nxtdir: dp = readdir(dirptr);
if (dp == NULL)
return(NULL);
/* check to make sure we skip directory entries */
bytecopy(nameptr, dp->d_name, namelen-1);
#if SYMEXPAND
bytecopy(&path[pathlen+1], dp->d_name, namelen-1);
#endif
/* strip leading './' or '/.' */
rptr = rbuf;
if (zapdot != 0 && rptr[0] != '\0' && rptr[1] != '\0') rptr += 2;
if (in_arg && arglen > 0) {
if (strncmp(rptr, argbuf, arglen) != 0)
goto nxtdir;
}
if (match_len > 0) {
if (exact) {
if (strncmp(rptr, match_name, match_len) != 0) {
goto nxtdir;
}
} else {
if (strcompare(rptr, match_name, match_len) != 0) {
goto nxtdir;
}
}
}
#if SYMEXPAND
if (stat(path, &fstatus) == -1)
goto nxtdir;
#else
if (stat(rbuf, &fstatus) == -1)
goto nxtdir;
#endif
if ((fstatus.st_mode & S_IFMT) == S_IFDIR) {
index = (int) strlen(nameptr);
nameptr[index] = '/';
nameptr[index+1] = '\0';
} else if ((fstatus.st_mode & S_IFMT) != S_IFREG) {
goto nxtdir;
}
/* return the next file name! */
return(rptr);
}
#endif
#if (BCOMPL & (TURBO | GNUC | ZORTECH))
#undef NEEDCOMPL
#define NEEDCOMPL 0
#if ZORTECH
struct FIND *fileblock; /* structure for directory searches */
#else
#include <dir.h>
struct ffblk fileblock; /* structure for directory searches */
#endif
char path[NFILEN]; /* path of file to find */
char rbuf[NFILEN]; /* return file buffer */
char *getffile(fspec, match_len, exact, is_arg)
CONSTA char *fspec; /* pattern to match */
int match_len;
int exact;
int is_arg;
{
register int index; /* index into various strings */
register int point; /* index into other strings */
register int extflag; /* does the file have an extention? */