-
Notifications
You must be signed in to change notification settings - Fork 51
/
Copy pathfile.c
2852 lines (2528 loc) · 79.7 KB
/
file.c
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
/*
* file - file I/O routines callable by users
*
* Copyright (C) 1999-2007,2018,2021-2023 David I. Bell and Landon Curt Noll
*
* Primary author: David I. Bell
*
* Calc is open software; you can redistribute it and/or modify it under
* the terms of the version 2.1 of the GNU Lesser General Public License
* as published by the Free Software Foundation.
*
* Calc is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
* or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
* Public License for more details.
*
* A copy of version 2.1 of the GNU Lesser General Public License is
* distributed with calc under the filename COPYING-LGPL. You should have
* received a copy with calc; if not, write to Free Software Foundation, Inc.
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*
* Under source code control: 1991/07/20 00:21:56
* File existed as early as: 1991
*
* chongo <was here> /\oo/\ http://www.isthe.com/chongo/
* Share and enjoy! :-) http://www.isthe.com/chongo/tech/comp/calc/
*/
#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdlib.h>
#include "have_unistd.h"
#if defined(HAVE_UNISTD_H)
# include <unistd.h>
#endif /* HAVE_UNISTD_H */
#include <ctype.h>
#include "calc.h"
#include "alloc.h"
#include "longbits.h"
#include "have_fgetsetpos.h"
#include "have_fpos_pos.h"
#include "fposval.h"
#include "file.h"
#include "strl.h"
#if defined(_WIN32) || defined(_WIN64)
# include <io.h>
#endif
#include "errtbl.h"
#include "banned.h" /* include after system header <> includes */
#define READSIZE 1024 /* buffer size for reading */
#define MIN(a,b) (((a) <= (b)) ? (a) : (b))
/*
* external STDIO functions
*/
E_FUNC void math_setfp(FILE *fp);
E_FUNC FILE *f_open(char *name, char *mode);
/*
* Table of opened files.
* The first three entries always correspond to stdin, stdout, and stderr,
* and cannot be closed. Their file ids are always 0, 1, and 2.
*/
STATIC FILEIO files[MAXFILES] = {
{FILEID_STDIN, NULL, (dev_t)0, (ino_t)0,
"(stdin)", true, false, false, false, 'r', "r"},
{FILEID_STDOUT, NULL, (dev_t)0, (ino_t)0,
"(stdout)", false, true, false, false, 'w', "w"},
{FILEID_STDERR, NULL, (dev_t)0, (ino_t)0,
"(stderr)", false, true, false, false, 'w', "w"}
};
STATIC int ioindex[MAXFILES] = {0,1,2}; /* Indices for FILEIO table */
STATIC FILEID lastid = FILEID_STDERR; /* Last allocated file id */
STATIC int idnum = 3; /* Number of allocated file ids */
/* forward static declarations */
S_FUNC ZVALUE filepos2z(FILEPOS pos);
S_FUNC FILEPOS z2filepos(ZVALUE pos);
S_FUNC int set_open_pos(FILE *fp, ZVALUE zpos);
S_FUNC int get_open_pos(FILE *fp, ZVALUE *res);
S_FUNC ZVALUE off_t2z(off_t siz);
S_FUNC ZVALUE dev2z(dev_t dev);
S_FUNC ZVALUE inode2z(ino_t inode);
S_FUNC void getscanfield(FILE *fp, bool skip, unsigned int width,
int scannum, char *scanptr, char **strptr);
S_FUNC void getscanwhite(FILE *fp, bool skip, unsigned int width,
int scannum, char **strptr);
S_FUNC int fscanfile(FILE *fp, char *fmt, int count, VALUE **vals);
S_FUNC void freadnum(FILE *fp, VALUE *valptr);
S_FUNC void freadsum(FILE *fp, VALUE *valptr);
S_FUNC void freadprod(FILE *fp, VALUE *valptr);
S_FUNC void fskipnum(FILE *fp);
/*
* file_init - perform needed initialization work
*
* On some systems, one cannot initialize a pointer to a FILE *.
* This routine, called once at startup is a work-a-round for
* systems with such bogons.
*
* We will also probe for any open files beyond stderr and set them up.
*/
void
file_init(void)
{
STATIC int done = 0; /* 1 => routine already called */
struct stat sbuf; /* file status */
FILEIO *fiop;
FILE *fp;
int i;
if (!done) {
/*
* setup the default set
*/
files[0].fp = stdin;
files[1].fp = stdout;
files[2].fp = stderr;
for (i = 0; i < 3; ++i) {
if (fstat(i, &sbuf) >= 0) {
files[i].dev = sbuf.st_dev;
files[i].inode = sbuf.st_ino;
}
}
/*
* note any other files that we can find
*/
fiop = &files[3];
for (i = 3; i < MAXFILES; fiop++, ++i) {
char *tname;
fiop->name = NULL;
files[idnum].reading = true;
files[idnum].writing = true;
files[idnum].action = 0;
memset(files[idnum].mode, 0, MODE_LEN+1);
/*
* stat the descriptor to see what we have
*/
if (fstat(i, &sbuf) >= 0) {
size_t snprintf_len; /* malloced snprintf length */
fp = (FILE *) fdopen(i,"r+"); /*guess mode*/
if (fp) {
strlcpy(files[idnum].mode, "r+",
sizeof(files[idnum].mode));
} else {
fp = (FILE *) fdopen(i, "r");
if (fp) {
strlcpy(files[idnum].mode, "r",
sizeof(files[idnum].mode));
files[idnum].writing = false;
} else {
fp = (FILE *) fdopen(i, "w");
if (fp) {
strlcpy(files[idnum].mode, "w",
sizeof(files[idnum].mode));
files[idnum].reading = false;
}
else
continue;
}
}
snprintf_len =
sizeof("descriptor[12345678901234567890]") + 1;
tname = (char *)malloc(snprintf_len+1);
if (tname == NULL) {
math_error("Out of memory for init_file");
not_reached();
}
snprintf(tname, snprintf_len, "descriptor[%d]", i);
tname[snprintf_len] = '\0'; /* paranoia */
files[idnum].name = tname;
files[idnum].id = idnum;
files[idnum].fp = fp;
files[idnum].dev = sbuf.st_dev;
files[idnum].inode = sbuf.st_ino;
ioindex[idnum] = idnum;
idnum++;
lastid++;
}
}
done = 1;
}
}
/*
* init_fileio - initialize a FILEIO structure
*
* This function initializes a calc FILEIO structure. It will optionally
* malloc the filename string if given an non-NULL associated filename.
* It will canonicalize the file open mode string.
*
* given:
* fiop pointer to FILEIO structure to initialize
* name associated filename (NULL => caller will setup filename)
* mode open mode (one of {r,w,a}{,b}{,+})
* sbufp pointer to stat of open file
* id calc file ID
* fp open file stream
*/
S_FUNC void
init_fileio(FILEIO *fiop, char *name, char *mode,
struct stat *sbufp, FILEID id, FILE *fp)
{
char modestr[MODE_LEN+1]; /* mode [rwa]b?\+? */
size_t namelen; /* length of name */
/* clear modestr */
memset(modestr, 0, sizeof(modestr));
/* allocate filename if requested */
namelen = 0;
if (name != NULL) {
namelen = strlen(name);
fiop->name = (char *)malloc(namelen + 1);
if (fiop->name == NULL) {
math_error("No memory for filename");
not_reached();
}
}
/* initialize FILEIO structure */
if (name != NULL) {
strlcpy(fiop->name, name, namelen+1);
}
fiop->id = id;
fiop->fp = fp;
fiop->dev = sbufp->st_dev;
fiop->inode = sbufp->st_ino;
fiop->reading = false;
fiop->writing = false;
fiop->appending = false;
fiop->binary = false;
fiop->action = 0;
memset(fiop->mode, 0, sizeof(fiop->mode));
/*
* determine file open mode
*
* While a leading 'r' is for reading and a leading 'w' is
* for writing, the presence of a '+' in the string means
* both reading and writing. A leading 'a' means append
* which is writing.
*/
/* canonicalize read modes */
if (mode[0] == 'r') {
/* note read mode */
strlcpy(modestr, "r", sizeof(modestr));
fiop->reading = true;
/* note binary mode even though mode is not used / ignored */
if (strchr(mode, 'b') != NULL) {
strlcat(modestr, "b", sizeof(modestr));
}
/* note if reading and writing */
if (strchr(mode, '+') != NULL) {
fiop->writing = true;
strlcat(modestr, "+", sizeof(modestr));
}
/* canonicalize write modes */
} else if (mode[0] == 'w') {
/* note write mode */
strlcpy(modestr, "w", sizeof(modestr));
fiop->writing = true;
/* note binary mode even though mode is not used / ignored */
if (strchr(mode, 'b') != NULL) {
strlcat(modestr, "b", sizeof(modestr));
}
/* note if reading and writing */
if (strchr(mode, '+') != NULL) {
fiop->reading = true;
strlcat(modestr, "+", sizeof(modestr));
}
/* canonicalize append modes */
} else if (mode[0] == 'a') {
/* note append mode */
strlcpy(modestr, "a", sizeof(modestr));
fiop->writing = true;
fiop->appending = true;
/* note binary mode even though mode is not used / ignored */
if (strchr(mode, 'b') != NULL) {
strlcat(modestr, "b", sizeof(modestr));
}
/* note if reading and writing */
if (strchr(mode, '+') != NULL) {
fiop->reading = true;
strlcat(modestr, "+", sizeof(modestr));
}
/* canonicalize no I/O modes */
} else {
modestr[0] = '\0';
}
modestr[MODE_LEN] = '\0'; /* firewall */
/* record canonical open mode string */
strlcpy(fiop->mode, modestr, sizeof(fiop->mode));
}
/*
* openid - open the specified file name for reading or writing
*
* given:
* name file name
* mode open mode (one of {r,w,a}{,b}{,+})
*
* returns:
* >=3 FILEID which can be used to do I/O to the file
* <0 if the open failed
*
* NOTE: This function will not return 0, 1 or 2 since they are
* reserved for stdin, stdout, stderr. In fact, it must not
* return 0, 1, or 2 because it will confuse those who call
* the opensearchfile() function
*/
FILEID
openid(char *name, char *mode)
{
FILEIO *fiop; /* file structure */
FILEID id; /* new file id */
FILE *fp;
struct stat sbuf; /* file status */
int i;
/* find the next open slot in the files array */
if (idnum >= MAXFILES)
return -E_MANYOPEN;
fiop = &files[3];
for (i = 3; i < MAXFILES; fiop++,i++) {
if (fiop->name == NULL)
break;
}
if (i == MAXFILES)
math_error("This should not happen in openid()!!!");
/* open the file */
fp = f_open(name, mode);
if (fp == NULL) {
return FILEID_NONE;
}
if (fstat(fileno(fp), &sbuf) < 0) {
math_error("bad fstat");
not_reached();
}
/* get a new FILEID */
id = ++lastid;
ioindex[idnum++] = i;
/* initialize FILEIO structure */
init_fileio(fiop, name, mode, &sbuf, id, fp);
/* return calc open file ID */
return id;
}
/*
* openpathid - open the specified base filename, or
* relative filename along a search path
*
* given:
* name file name
* mode open mode (one of {r,w,a}{,b}{,+})
* pathlist list of colon separated paths (or NULL)
*
* returns:
* >=3 FILEID which can be used to do I/O to the file
* <0 if the open failed
*
* NOTE: This function will not return 0, 1 or 2 since they are
* reserved for stdin, stdout, stderr. In fact, it must not
* return 0, 1, or 2 because it will confuse those who call
* the opensearchfile() function
*/
FILEID
openpathid(char *name, char *mode, char *pathlist)
{
FILEIO *fiop; /* file structure */
FILEID id; /* new file id */
FILE *fp;
struct stat sbuf; /* file status */
char *openpath; /* malloc copy of path that was opened */
int i;
/* find the next open slot in the files array */
if (idnum >= MAXFILES)
return -E_MANYOPEN;
fiop = &files[3];
for (i = 3; i < MAXFILES; fiop++,i++) {
if (fiop->name == NULL)
break;
}
if (i == MAXFILES)
math_error("This should not happen in openpathid()!!!");
/* open a file - searching along a path */
openpath = NULL;
fp = f_pathopen(name, mode, pathlist, &openpath);
if (fp == NULL) {
if (openpath != NULL) {
/* should not happen, but just in case */
free(openpath);
}
return FILEID_NONE;
}
if (fstat(fileno(fp), &sbuf) < 0) {
if (openpath != NULL) {
free(openpath);
}
math_error("bad fstat");
not_reached();
}
if (openpath == NULL) {
fclose(fp);
math_error("bad openpath");
not_reached();
}
/* get a new FILEID */
id = ++lastid;
ioindex[idnum++] = i;
/* initialize FILEIO structure */
init_fileio(fiop, NULL, mode, &sbuf, id, fp);
fiop->name = openpath; /* already malloced by f_pathopen */
/* return calc open file ID */
return id;
}
/*
* reopenid - reopen a FILEID
*
* given:
* id FILEID to reopen
* mode new mode to open as
* mode new mode to open as (one of "r", "w", "a", "r+", "w+", "a+")
* name name of new file
*
* returns:
* FILEID which can be used to do I/O to the file
* <0 if the open failed
*/
FILEID
reopenid(FILEID id, char *mode, char *name)
{
FILEIO *fiop; /* file structure */
FILE *fp;
struct stat sbuf;
int i;
/* firewall */
if ((id == FILEID_STDIN) || (id == FILEID_STDOUT) ||
(id == FILEID_STDERR)) {
math_error("Cannot freopen stdin, stdout, or stderr");
not_reached();
}
/* reopen the file */
fiop = NULL;
for (i = 3; i < idnum; i++) {
fiop = &files[ioindex[i]];
if (fiop->id == id)
break;
}
if (i == idnum) {
if (name == NULL) {
fprintf(stderr, "File not open, need file name\n");
return FILEID_NONE;
}
if (idnum >= MAXFILES) {
fprintf(stderr, "Too many open files\n");
return FILEID_NONE;
}
for (fiop = &files[3], i = 3; i < MAXFILES; fiop++, i++) {
if (fiop->name == NULL)
break;
}
if (i >= MAXFILES) {
math_error("This should not happen in reopenid");
not_reached();
}
fp = f_open(name, mode);
if (fp == NULL) {
fprintf(stderr, "Cannot open file\n");
return FILEID_NONE;
}
ioindex[idnum++] = i;
fiop->id = id;
} else {
if (name == NULL)
fp = freopen(fiop->name, mode, fiop->fp);
else
fp = freopen(name, mode, fiop->fp);
if (fp == NULL) {
free(fiop->name);
fiop->name = NULL;
idnum--;
for (; i < idnum; i++)
ioindex[i] = ioindex[i + 1];
return FILEID_NONE;
}
}
if (fstat(fileno(fp), &sbuf) < 0) {
math_error("bad fstat");
not_reached();
}
/* initialize FILEIO structure */
if (name == NULL) {
if (fiop->name == NULL) {
math_error("old and new reopen filenames are NULL");
}
} else if (fiop->name != NULL) {
free(fiop->name);
fiop->name = NULL;
}
init_fileio(fiop, name, mode, &sbuf, id, fp);
/* return calc open file ID */
return id;
}
/*
* Find the file I/O structure for the specified file id, and verifies that
* it is opened in the required manner (0 for reading or 1 for writing).
* If writable is -1, then no open checks are made at all and NULL is then
* returned if the id represents a closed file.
*/
FILEIO *
findid(FILEID id, int writable)
{
FILEIO *fiop; /* file structure */
int i;
fiop = NULL;
if ((id < 0) || (id > lastid))
return NULL;
for (i = 0; i < idnum; i++) {
fiop = &files[ioindex[i]];
if (fiop->id == id)
break;
}
if (i == idnum)
return NULL;
if (writable >= 0) {
if ((writable && !fiop->writing) ||
(!writable && !fiop->reading)) {
return NULL;
}
}
return fiop;
}
/*
* Return whether or not a file id is valid. This is used for if tests.
*/
bool
validid(FILEID id)
{
return (findid(id, -1) != NULL);
}
/*
* Return the file with id = index if this is the id of a file that has been
* opened (it may have since been closed). Otherwise returns FILEID_NONE.
*/
FILEID
indexid(long index)
{
FILEID id;
id = (FILEID) index;
if ((index < 0) || (id > lastid))
return FILEID_NONE;
return id;
}
/*
* Close the specified file id. Returns true if there was an error.
* Closing of stdin, stdout, or stderr is illegal, but closing of already
* closed files is allowed.
*/
int
closeid(FILEID id)
{
FILEIO *fiop; /* file structure */
int i;
int err;
fiop = NULL;
/* firewall */
if ((id == FILEID_STDIN) || (id == FILEID_STDOUT) ||
(id == FILEID_STDERR)) {
math_error("Cannot close stdin, stdout, or stderr");
not_reached();
}
/* get file structure */
for (i = 3; i < idnum; i++) {
fiop = &files[ioindex[i]];
if (fiop->id == id)
break;
}
if (i == idnum)
return 1; /* File not open */
idnum--;
for (; i < idnum; i++)
ioindex[i] = ioindex[i + 1];
free(fiop->name);
fiop->name = NULL;
/* close file and note error state */
err = ferror(fiop->fp);
err |= fclose(fiop->fp);
fiop->fp = NULL;
/* return success or failure */
return (err ? EOF : 0);
}
int
closeall(void)
{
FILEIO *fiop;
int i;
int err;
err = 0;
for (i = 3; i < idnum; i++) {
fiop = &files[ioindex[i]];
if (fiop->fp) {
free(fiop->name);
fiop->name = NULL;
err |= fclose(fiop->fp);
}
}
idnum = 3;
return err;
}
/*
* Return whether or not an error occurred to a file.
*/
bool
errorid(FILEID id)
{
FILEIO *fiop; /* file structure */
fiop = findid(id, -1);
if (fiop == NULL)
return EOF;
return (ferror(fiop->fp) != 0);
}
/*
* Return whether or not end of file occurred to a file.
*/
bool
eofid(FILEID id)
{
FILEIO *fiop; /* file structure */
fiop = findid(id, -1);
if (fiop == NULL)
return EOF;
return (feof(fiop->fp) != 0);
}
/*
* Flush output to an opened file.
*/
int
flushid(FILEID id)
{
FILEIO *fiop; /* file structure */
fiop = findid(id, -1);
if (fiop == NULL)
return 0;
if (!fiop->writing || fiop->action == 'r')
return 0;
return fflush(fiop->fp);
}
#if !defined(_WIN32) && !defined(_WIN64)
int
flushall(void)
{
FILEIO *fiop;
int i;
int err;
err = 0;
for (i = 3; i < idnum; i++) {
fiop = &files[ioindex[i]];
if (fiop->writing && fiop->action != 'r')
err |= fflush(fiop->fp);
}
return err;
}
#endif /* Windows free systems */
/*
* Read the next line, string or word from an opened file.
* Returns a pointer to an allocated string holding a null-terminated
* or newline terminated string. Where reading stops is controlled by
* flags:
*
* bit 0: at newline
* bit 1: at null character
* bit 2: at white space (also skips leading white space)
*
* If neither '\n' nor '\0' is encountered reading continues until EOF.
* If bit 3 is set the stop character is removed.
*
* given:
* id file to read from
* flags read flags (see above)
* retstr returned pointer to string
*/
int
readid(FILEID id, int flags, STRING **retstr)
{
FILEIO *fiop; /* file structure */
FILE *fp;
char *str; /* current string */
unsigned long n; /* current number characters read into buf */
unsigned long totlen; /* total length of string copied from buf */
char buf[READSIZE]; /* temporary buffer */
char *b;
int c;
bool nlstop, nullstop, wsstop, rmstop, done;
FILEPOS fpos;
STRING *newstr;
totlen = 0;
str = NULL;
fiop = findid(id, false);
if (fiop == NULL)
return 1;
nlstop = (flags & 1);
nullstop = (flags & 2);
wsstop = (flags & 4);
rmstop = (flags & 8);
fp = fiop->fp;
if (fiop->action == 'w') {
f_tell(fp, &fpos);
fflush(fp);
if (f_seek_set(fp, &fpos) < 0)
return 3;
}
fiop->action = 'r';
if (wsstop) {
while (isspace(c = fgetc(fp)));
ungetc(c, fp);
}
for (;;) {
b = buf;
n = 0;
do {
c = fgetc(fp);
if (c == EOF)
break;
n++;
if (nlstop && c == '\n')
break;
if (nullstop && c == '\0')
break;
if (wsstop && isspace(c))
break;
*b++ = c;
} while (n < READSIZE);
done = ((nlstop && c == '\n') || (nullstop && c == '\0') ||
(wsstop && isspace(c)) || c == EOF);
if (done && rmstop && c != EOF)
n--;
if (totlen)
str = (char *)realloc(str, totlen + n + 1);
else
str = (char *)malloc(n + 1);
if (str == NULL) {
math_error("Out of memory for readid");
not_reached();
}
if (n > 0)
memcpy(&str[totlen], buf, n);
totlen += n;
if (done)
break;
}
if (totlen == 0 && c == EOF) {
free(str);
return EOF;
}
if ((nlstop && c == '\n') && !rmstop)
str[totlen - 1] = '\n';
if ((nullstop && c == '\0') && !rmstop)
str[totlen - 1] = '\0';
str[totlen] = '\0';
newstr = stralloc();
newstr->s_len = totlen;
newstr->s_str = str;
*retstr = newstr;
return 0;
}
/*
* Return the next character from an opened file.
* Returns EOF if there was an error or end of file.
*/
int
getcharid(FILEID id)
{
FILEIO *fiop;
FILEPOS fpos;
fiop = findid(id, false);
if (fiop == NULL)
return -2;
if (fiop->action == 'w') {
f_tell(fiop->fp, &fpos);
fflush(fiop->fp);
if (f_seek_set(fiop->fp, &fpos) < 0)
return -3;
}
fiop->action = 'r';
return fgetc(fiop->fp);
}
/*
* Print out the name of an opened file.
* If the file has been closed, a null name is printed.
* If flags contain PRINT_UNAMBIG then extra information is printed
* identifying the output as a file and some data about it.
*/
int
printid(FILEID id, int flags)
{
FILEIO *fiop; /* file structure */
FILE *fp;
ZVALUE pos; /* file position */
/*
* filewall - file is closed
*/
fiop = findid(id, -1);
if (fiop == NULL) {
if (flags & PRINT_UNAMBIG)
math_fmt("FILE %ld closed", id);
else
math_str("\"\"");
return 1;
}
/*
* print quoted filename and mode
*/
if ((flags & PRINT_UNAMBIG) == 0) {
math_chr('"');
math_str(fiop->name);
math_chr('"');
return 0;
}
math_fmt("FILE %ld \"%s\" (%s", id, fiop->name, fiop->mode);
/*
* print file position
*/
fp = fiop->fp;
if (get_open_pos(fp, &pos) < 0) {
if (fileno(fp) > 2)
math_str("Error while determining file position!");
math_chr(')');
return 0;
}
math_str(", pos ");
zprintval(pos, 0, 0);
zfree(pos);
/*
* report special status
*/
if (ferror(fp))
math_str(", error");
if (feof(fp))
math_str(", eof");
math_chr(')');
printf(" fileno: %d ", fileno(fp));
return 0;
}
/*
* Print a formatted string similar to printf. Various formats of output
* are possible, depending on the format string AND the actual types of the
* values. Mismatches do not cause errors, instead something reasonable is
* printed instead. The output goes to the file with the specified id.
*
* given:
* id file id to print to
* count print count
* fmt standard format string
* vals table of values to print
*/
int
idprintf(FILEID id, char *fmt, int count, VALUE **vals)
{
FILEIO *fiop;
VALUE *vp;
char *str;
int ch;
size_t len;
int oldmode, newmode;
long olddigits, newdigits;
long width, precision;
bool didneg, didprecision;
FILEPOS fpos;
bool printstring;
bool printchar;
fiop = findid(id, true);
if (fiop == NULL)
return 1;
if (fiop->action == 'r') {
f_tell(fiop->fp, &fpos);
if (f_seek_set(fiop->fp, &fpos) < 0)
return 3;
}
fiop->action = 'w';
printstring = false;
printchar = false;
math_setfp(fiop->fp);
while ((ch = *fmt++) != '\0') {
if (ch != '%') {
math_chr(ch);
continue;
}