-
Notifications
You must be signed in to change notification settings - Fork 17
/
fileio.cc
1523 lines (1258 loc) · 35.7 KB
/
fileio.cc
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 i/o server modification
*/
#define FILE_IO 1
#include <stdio.h>
#include "my-stat.h"
#include <dirent.h>
/* some things are not defined in stdio on all systems -- AAB 06/03/97 */
#include <sys/types.h>
#include <errno.h>
#include "my-unistd.h"
#include "my-ctype.h"
#include "my-string.h"
#include "structures.h"
#include "bf_register.h"
#include "functions.h"
#include "list.h"
#include "storage.h"
#include "utils.h"
#include "streams.h"
#include "server.h"
#include "network.h"
#include "tasks.h"
#include "log.h"
#include "fileio.h"
/******************************************************
* Module-internal data structures
*****************************************************/
/*
* File types are either TEXT or BINARY
*/
typedef struct fileio_file_type *file_type;
struct fileio_file_type {
const char* (*in_filter)(const char *data, int buflen);
const char* (*out_filter)(const char *data, int *buflen);
};
file_type file_type_binary = NULL;
file_type file_type_text = NULL;
#define FILE_O_READ 1
#define FILE_O_WRITE 2
#define FILE_O_FLUSH 4
typedef unsigned char file_mode;
typedef struct file_handle file_handle;
struct file_handle {
char valid; /* Is this a valid entry? */
char *name; /* pathname of the file */
file_type type; /* text or binary, sir? */
file_mode mode; /* readin', writin' or both */
FILE *file; /* the actual file handle */
};
typedef struct line_buffer line_buffer;
struct line_buffer {
char *line;
struct line_buffer *next;
};
/***************************************************************
* Version and package informaion
***************************************************************/
char file_package_name[] = "FIO";
char file_package_version[] = "1.5p4";
/***************************************************************
* File <-> FHANDLE descriptor table interface
***************************************************************/
file_handle file_table[FILE_IO_MAX_FILES];
char file_handle_valid(Var fhandle) {
int32 i = fhandle.v.num;
if(fhandle.type != TYPE_INT)
return 0;
if((i < 0) || (i >= FILE_IO_MAX_FILES))
return 0;
return file_table[i].valid;
}
FILE *file_handle_file(Var fhandle) {
int32 i = fhandle.v.num;
return file_table[i].file;
}
const char *file_handle_name(Var fhandle) {
int32 i = fhandle.v.num;
return file_table[i].name;
}
file_type file_handle_type(Var fhandle) {
int32 i = fhandle.v.num;
return file_table[i].type;
}
file_mode file_handle_mode(Var fhandle) {
int32 i = fhandle.v.num;
return file_table[i].mode;
}
void file_handle_destroy(Var fhandle) {
int32 i = fhandle.v.num;
file_table[i].file = NULL;
file_table[i].valid = 0;
free_str(file_table[i].name);
}
int32 file_allocate_next_handle(void) {
static int32 current_handle = 0;
int32 wrapped = current_handle;
if(current_handle > FILE_IO_MAX_FILES)
wrapped = current_handle = 0;
while(current_handle < FILE_IO_MAX_FILES) {
if(!file_table[current_handle].valid)
break;
current_handle++;
if(current_handle > FILE_IO_MAX_FILES)
current_handle = 0;
if(current_handle == wrapped)
current_handle = FILE_IO_MAX_FILES;
}
if(current_handle == FILE_IO_MAX_FILES) {
current_handle = 0;
return -1;
}
return current_handle;
}
Var file_handle_new(const char *name, file_type type, file_mode mode) {
Var r;
int32 handle = file_allocate_next_handle();
r.type = TYPE_INT;
r.v.num = handle;
if(handle >= 0) {
file_table[handle].valid = 1;
file_table[handle].name = str_dup(name);
file_table[handle].type = type;
file_table[handle].mode = mode;
}
return r;
}
void file_handle_set_file(Var fhandle, FILE *f) {
int32 i = fhandle.v.num;
file_table[i].file = f;
}
/***************************************************************
* Interface for modestrings
***************************************************************/
/*
* Convert modestring to settings for type and mode.
* Returns pointer to stdio modestring if successfull and
* NULL if not.
*/
const char *file_modestr_to_mode(const char *s, file_type *type, file_mode *mode) {
static char buffer[4] = {0, 0, 0, 0};
int p = 0;
file_type t;
file_mode m = 0;
if(!file_type_binary) {
file_type_binary = (struct fileio_file_type *)mymalloc(sizeof(struct fileio_file_type), M_STRING);
file_type_text = (struct fileio_file_type *)mymalloc(sizeof(struct fileio_file_type), M_STRING);
file_type_binary->in_filter = raw_bytes_to_binary;
file_type_binary->out_filter = binary_to_raw_bytes;
file_type_text->in_filter = raw_bytes_to_clean;
file_type_text->out_filter = clean_to_raw_bytes;
}
if(strlen(s) != 4)
return 0;
if(s[0] == 'r') m |= FILE_O_READ;
else if(s[0] == 'w') m |= FILE_O_WRITE;
else if(s[0] == 'a') m |= FILE_O_WRITE;
else
return NULL;
buffer[p++] = s[0];
if(s[1] == '+') {
m |= (s[0] == 'r') ? FILE_O_WRITE : FILE_O_READ;
buffer[p++] = '+';
} else if (s[1] != '-') {
return NULL;
}
if(s[2] == 't') t = file_type_text;
else if (s[2] == 'b') {
t = file_type_binary;
buffer[p++] = 'b';
} else
return NULL;
if(s[3] == 'f') m |= FILE_O_FLUSH;
else if (s[3] != 'n')
return NULL;
*type = t; *mode = m;
buffer[p] = 0;
return buffer;
}
/***************************************************************
* Various error handlers
***************************************************************/
package
file_make_error(const char *errtype, const char *msg) {
package p;
Var value;
value.type = TYPE_STR;
value.v.str = str_dup(errtype);
p.kind = package::BI_RAISE;
p.u.raise.code.type = TYPE_ERR;
p.u.raise.code.v.err = E_FILE;
p.u.raise.msg = str_dup(msg);
p.u.raise.value = value;
return p;
}
package file_raise_errno(const char *value_str) {
char *strerr;
if(errno) {
strerr = strerror(errno);
return file_make_error(value_str, strerr);
} else {
return file_make_error("End of file", "End of file");
}
}
package file_raise_notokcall(const char *funcid, Objid progr) {
return make_error_pack(E_PERM);
}
package file_raise_notokfilename(const char *funcid, const char *pathname) {
Var p;
p.type = TYPE_STR;
p.v.str = str_dup(pathname);
return make_raise_pack(E_INVARG, "Invalid pathname", p);
}
/***************************************************************
* Security verification
***************************************************************/
int file_verify_caller(Objid progr) {
return is_wizard(progr);
}
int file_verify_path(const char *pathname) {
/*
* A pathname is OK does not contain a
* any of instances the substring "/."
*/
if(pathname[0] == '\0')
return 1;
if((strlen(pathname) > 1) && (pathname[0] == '.') && (pathname[1] == '.'))
return 0;
if(strindex(pathname, strlen(pathname), "/.", 2, 0))
return 0;
return 1;
}
/***************************************************************
* Common code for FHANDLE-using functions
**************************************************************/
FILE *file_handle_file_safe(Var handle) {
if(!file_handle_valid(handle))
return NULL;
else
return file_handle_file(handle);
}
const char *file_handle_name_safe(Var handle) {
if(!file_handle_valid(handle))
return NULL;
else
return file_handle_name(handle);
}
/***************************************************************
* Common code for file opening functions
***************************************************************/
const char *file_resolve_path(const char *pathname) {
static Stream *s = 0;
if(!s)
s = new_stream(strlen(pathname) + strlen(FILE_SUBDIR) + 1);
if(!file_verify_path(pathname))
return NULL;
stream_add_string(s, FILE_SUBDIR);
if(pathname[0] == '/')
stream_add_string(s, pathname + 1);
else
stream_add_string(s, pathname);
return reset_stream(s);
}
/***************************************************************
* Built in functions
* file_version
***************************************************************/
static package
bf_file_version(Var arglist, Byte next, void *vdata, Objid progr)
{
char tmpbuffer[50];
Var rv;
sprintf(tmpbuffer, "%s/%s", file_package_name, file_package_version);
rv.type = TYPE_STR;
rv.v.str = str_dup(tmpbuffer);
return make_var_pack(rv);
}
/***************************************************************
* File open and close.
***************************************************************/
/*
* FHANDLE file_open(STR name, STR mode)
*/
static package
bf_file_open(Var arglist, Byte next, void *vdata, Objid progr)
{
package r;
Var fhandle;
const char *real_filename;
const char *filename = arglist.v.list[1].v.str;
const char *mode = arglist.v.list[2].v.str;
const char *fmode;
file_mode rmode;
file_type type;
FILE *f;
errno = 0;
if(!file_verify_caller(progr))
r = file_raise_notokcall("file_open", progr);
else if ((real_filename = file_resolve_path(filename)) == NULL)
r = file_raise_notokfilename("file_open", filename);
else if ((fmode = file_modestr_to_mode(mode, &type, &rmode)) == NULL)
r = make_raise_pack(E_INVARG, "Invalid mode string", var_ref(arglist.v.list[2]));
else if ((fhandle = file_handle_new(filename, type, rmode)).v.num < 0)
r = make_raise_pack(E_QUOTA, "Too many files open", zero);
else if ((f = fopen(real_filename, fmode)) == NULL) {
file_handle_destroy(fhandle);
r = file_raise_errno("file_open");
} else {
/* phew, we actually got a successfull open */
file_handle_set_file(fhandle, f);
r = make_var_pack(fhandle);
}
free_var(arglist);
return r;
}
/*
* void file_close(FHANDLE handle);
*/
static package
bf_file_close(Var arglist, Byte next, void *vdata, Objid progr)
{
package r;
Var fhandle = arglist.v.list[1];
FILE *f;
errno = 0;
if(!file_verify_caller(progr))
r = file_raise_notokcall("file_close", progr);
else if ((f = file_handle_file_safe(fhandle)) == NULL)
r = make_raise_pack(E_INVARG, "Invalid FHANDLE", var_ref(fhandle));
else {
fclose(f);
file_handle_destroy(fhandle);
r = no_var_pack();
}
free_var(arglist);
return r;
}
/*
* STR file_name(FHANDLE handle)
*/
static package
bf_file_name(Var arglist, Byte next, void *vdata, Objid progr)
{
package r;
Var fhandle = arglist.v.list[1];
const char *name;
Var rv;
if(!file_verify_caller(progr)) {
r = file_raise_notokcall("file_name", progr);
} else if ((name = file_handle_name_safe(fhandle)) == NULL) {
r = make_raise_pack(E_INVARG, "Invalid FHANDLE", var_ref(fhandle));
} else {
rv.type = TYPE_STR;
rv.v.str = str_dup(name);
r = make_var_pack(rv);
}
free_var(arglist);
return r;
}
static package
bf_file_openmode(Var arglist, Byte next, void *vdata, Objid progr)
{
package r;
Var fhandle = arglist.v.list[1];
char buffer[5] = {0, 0, 0, 0, 0};
file_mode mode;
file_type type;
Var rv;
if(!file_verify_caller(progr)) {
r = file_raise_notokcall("file_name", progr);
} else if (!file_handle_valid(fhandle)) {
r = make_raise_pack(E_INVARG, "Invalid FHANDLE", var_ref(fhandle));
} else {
type = file_handle_type(fhandle);
mode = file_handle_mode(fhandle);
if(mode & FILE_O_READ) {
buffer[0] = 'r';
} else if(mode & FILE_O_WRITE) {
buffer[0] = 'w';
}
if(mode & (FILE_O_READ | FILE_O_WRITE))
buffer[1] = '+';
else
buffer[1] = '-';
if(type == file_type_binary)
buffer[2] = 'b';
else
buffer[2] = 't';
if(mode & FILE_O_FLUSH)
buffer[3] = 'f';
else
buffer[3] = 'n';
rv.type = TYPE_STR;
rv.v.str = str_dup(buffer);
r = make_var_pack(rv);
}
free_var(arglist);
return r;
}
/**********************************************************
* string (line-based) i/o
**********************************************************/
/*
* common functionality of file_readline and file_readlines
*/
static const char *file_read_line(Var fhandle, int *count) {
static Stream *str = 0;
FILE *f;
int c;
f = file_handle_file(fhandle);
if(str == 0)
str = new_stream(FILE_IO_BUFFER_LENGTH);
while((c = fgetc(f)) != EOF && c != '\n')
stream_add_char(str, c);
if(c == EOF && stream_length(str) == 0) {
reset_stream(str);
*count = 0;
return NULL;
}
*count = stream_length(str);
return reset_stream(str);
}
/*
* STR file_readline(FHANDLE handle)
*/
static package
bf_file_readline(Var arglist, Byte next, void *vdata, Objid progr)
{
package r;
Var fhandle = arglist.v.list[1];
Var rv;
int len;
file_mode mode;
file_type type;
const char *line;
errno = 0;
if(!file_verify_caller(progr)) {
r = file_raise_notokcall("file_readline", progr);
} else if (!file_handle_valid(fhandle)) {
r = make_raise_pack(E_INVARG, "Invalid FHANDLE", var_ref(fhandle));
} else if (!(mode = file_handle_mode(fhandle)) & FILE_O_READ)
r = make_raise_pack(E_INVARG, "File is open write-only", var_ref(fhandle));
else {
type = file_handle_type(fhandle);
if((line = file_read_line(fhandle, &len)) == NULL)
r = file_raise_errno("readline");
else {
rv.type = TYPE_STR;
rv.v.str = str_dup((type->in_filter)(line, len));
r = make_var_pack(rv);
}
}
free_var(arglist);
return r;
}
/*
* STR file_readlines(FHANDLE handle, INT start, INT end)
*/
void free_line_buffer(line_buffer *head, int strings_too) {
line_buffer *next;
if(head) {
next = head->next;
myfree(head, M_STRUCT);
head = next;
while(head != NULL) {
next = head->next;
if(strings_too)
free_str(head->line);
myfree(head, M_STRUCT);
head = next;
}
}
}
line_buffer *new_line_buffer(char *line) {
line_buffer *p = (line_buffer *)mymalloc(sizeof(line_buffer), M_STRUCT);
p->line = line;
p->next = NULL;
return p;
}
static package
bf_file_readlines(Var arglist, Byte next, void *vdata, Objid progr)
{
package r;
Var fhandle = arglist.v.list[1];
int32 begin = arglist.v.list[2].v.num;
int32 end = arglist.v.list[3].v.num;
int32 begin_loc = 0, linecount = 0;
file_type type;
file_mode mode;
Var rv;
int current_line = 0, len = 0, i = 0;
const char *line = NULL;
FILE *f;
line_buffer *linebuf_head = NULL, *linebuf_cur = NULL;
errno = 0;
if((begin < 1) || (begin > end))
return make_error_pack(E_INVARG);
if(!file_verify_caller(progr)) {
r = file_raise_notokcall("file_readlines", progr);
} else if ((f = file_handle_file_safe(fhandle)) == NULL) {
r = make_raise_pack(E_INVARG, "Invalid FHANDLE", var_ref(fhandle));
} else if (!(mode = file_handle_mode(fhandle)) & FILE_O_READ)
r = make_raise_pack(E_INVARG, "File is open write-only", var_ref(fhandle));
else {
/* Back to the beginning ... */
rewind(f);
/* "seek" to that line */
begin--;
while((current_line != begin)
&& ((line = file_read_line(fhandle, &len)) != NULL))
current_line++;
if(((begin != 0) && (line == NULL)) || ((begin_loc = ftell(f)) == -1))
r = file_raise_errno("read_line");
else {
type = file_handle_type(fhandle);
/*
* now that we have where to begin, it's time to slurp lines
* and seek to EOF or to the end_line, whichever comes first
*/
linebuf_head = linebuf_cur = new_line_buffer(NULL);
while((current_line != end)
&& ((line = file_read_line(fhandle, &len)) != NULL)) {
linebuf_cur->next = new_line_buffer(str_dup((type->in_filter)(line, len)));
linebuf_cur = linebuf_cur->next;
current_line++;
}
linecount = current_line - begin;
linebuf_cur = linebuf_head->next;
if(fseek(f, begin_loc, SEEK_SET) == -1) {
free_line_buffer(linebuf_head, 1);
r = file_raise_errno("seeking");
} else {
rv = new_list(linecount);
i = 1;
while(linebuf_cur != NULL) {
rv.v.list[i].type = TYPE_STR;
rv.v.list[i].v.str = linebuf_cur->line;
linebuf_cur = linebuf_cur->next;
i++;
}
free_line_buffer(linebuf_head, 0);
r = make_var_pack(rv);
}
}
}
free_var(arglist);
return r;
}
/*
* void file_writeline(FHANDLE handle, STR line)
*/
static package
bf_file_writeline(Var arglist, Byte next, void *vdata, Objid progr)
{
package r;
Var fhandle = arglist.v.list[1];
const char *buffer = arglist.v.list[2].v.str;
const char *rawbuffer;
file_mode mode;
file_type type;
int len;
FILE *f;
errno = 0;
if(!file_verify_caller(progr)) {
r = file_raise_notokcall("file_writeline", progr);
} else if ((f = file_handle_file_safe(fhandle)) == NULL) {
r = make_raise_pack(E_INVARG, "Invalid FHANDLE", var_ref(fhandle));
} else if (!(mode = file_handle_mode(fhandle)) & FILE_O_WRITE)
r = make_raise_pack(E_INVARG, "File is open read-only", var_ref(fhandle));
else {
type = file_handle_type(fhandle);
if((rawbuffer = (type->out_filter)(buffer, &len)) == NULL)
r = make_raise_pack(E_INVARG, "Invalid binary string", var_ref(fhandle));
else if((fputs(rawbuffer, f) == EOF) || (fputc('\n', f) != '\n'))
r = file_raise_errno(file_handle_name(fhandle));
else {
if(mode & FILE_O_FLUSH) {
fflush(f);
}
r = no_var_pack();
}
}
free_var(arglist);
return r;
}
/********************************************************
* binary i/o
********************************************************/
/*
* STR file_read(FHANDLE handle, INT record_length)
*/
static package
bf_file_read(Var arglist, Byte next, void *vdata, Objid progr)
{
package r;
Var fhandle = arglist.v.list[1];
file_mode mode;
file_type type;
int32 record_length = arglist.v.list[2].v.num;
int32 read_length;
char buffer[FILE_IO_BUFFER_LENGTH];
Var rv;
static Stream *str = 0;
int len = 0, read = 0;
FILE *f;
errno = 0;
read_length = (record_length > sizeof(buffer)) ? sizeof(buffer) : record_length;
if(str == 0)
str = new_stream(FILE_IO_BUFFER_LENGTH);
if(!file_verify_caller(progr)) {
r = file_raise_notokcall("file_read", progr);
} else if ((f = file_handle_file_safe(fhandle)) == NULL) {
r = make_raise_pack(E_INVARG, "Invalid FHANDLE", var_ref(fhandle));
} else if (!(mode = file_handle_mode(fhandle)) & FILE_O_READ)
r = make_raise_pack(E_INVARG, "File is open write-only", var_ref(fhandle));
else {
type = file_handle_type(fhandle);
try_again:
read = fread(buffer, sizeof(char), read_length, f);
if(!read && !len) {
/*
* No more to read. This is only an error if nothing
* has been read so far.
*
*/
r = file_raise_errno(file_handle_name(fhandle));
} else if (read && ((len += read) < record_length)){
/*
* We got something this time, but it isn't enough.
*/
stream_add_string(str, (type->in_filter)(buffer, read));
read = 0;
goto try_again;
} else {
/*
* We didn't get anything last time, but we have something already
* OR
* We got everything we need.
*/
stream_add_string(str, (type->in_filter)(buffer, read));
rv.type = TYPE_STR;
rv.v.str = str_dup(reset_stream(str));
r = make_var_pack(rv);
}
}
free_var(arglist);
return r;
}
/*
* void file_flush(FHANDLE handle)
*/
static package
bf_file_flush(Var arglist, Byte next, void *vdata, Objid progr)
{
package r;
Var fhandle = arglist.v.list[1];
FILE *f;
errno = 0;
if(!file_verify_caller(progr)) {
r = file_raise_notokcall("file_flush", progr);
} else if ((f = file_handle_file_safe(fhandle)) == NULL) {
r = make_raise_pack(E_INVARG, "Invalid FHANDLE", var_ref(fhandle));
} else {
if(fflush(f))
r = file_raise_errno("flushing");
else
r = no_var_pack();
}
free_var(arglist);
return r;
}
/*
* INT file_write(FHANDLE fh, STR data)
*/
static package
bf_file_write(Var arglist, Byte next, void *vdata, Objid progr)
{
package r;
Var fhandle = arglist.v.list[1], rv;
const char *buffer = arglist.v.list[2].v.str;
const char *rawbuffer;
file_mode mode;
file_type type;
int len;
int written;
FILE *f;
errno = 0;
if(!file_verify_caller(progr)) {
r = file_raise_notokcall("file_write", progr);
} else if ((f = file_handle_file_safe(fhandle)) == NULL) {
r = make_raise_pack(E_INVARG, "Invalid FHANDLE", var_ref(fhandle));
} else if (!(mode = file_handle_mode(fhandle)) & FILE_O_WRITE)
r = make_raise_pack(E_INVARG, "File is open read-only", var_ref(fhandle));
else {
type = file_handle_type(fhandle);
if((rawbuffer = (type->out_filter)(buffer, &len)) == NULL)
r = make_raise_pack(E_INVARG, "Invalid binary string", var_ref(fhandle));
else if(!(written = fwrite(rawbuffer, sizeof(char), len, f)))
r = file_raise_errno(file_handle_name(fhandle));
else {
if(mode & FILE_O_FLUSH)
fflush(f);
rv.type = TYPE_INT;
rv.v.num = written;
r = make_var_pack(rv);
}
}
free_var(arglist);
return r;
}
/************************************************
* navigating the file
************************************************/
/*
* void file_seek(FHANDLE handle, FLOC location, STR whence)
* whence in {"SEEK_SET", "SEEK_CUR", "SEEK_END"}
*/
static package
bf_file_seek(Var arglist, Byte next, void *vdata, Objid progr)
{
package r;
Var fhandle = arglist.v.list[1];
int32 seek_to = arglist.v.list[2].v.num;
const char *whence = arglist.v.list[3].v.str;
int whnce = 0, whence_ok = 1;
FILE *f;
errno = 0;
if(!mystrcasecmp(whence, "SEEK_SET"))
whnce = SEEK_SET;
else if (!mystrcasecmp(whence, "SEEK_CUR"))
whnce = SEEK_CUR;
else if (!mystrcasecmp(whence, "SEEK_END"))
whnce = SEEK_END;
else
whence_ok = 0;
if(!file_verify_caller(progr)) {
r = file_raise_notokcall("file_seek", progr);
} else if ((f = file_handle_file_safe(fhandle)) == NULL) {
r = make_raise_pack(E_INVARG, "Invalid FHANDLE", var_ref(fhandle));
} else if (!whence_ok) {
r = make_raise_pack(E_INVARG, "Invalid whence", zero);
} else {
if(fseek(f, seek_to, whnce))
r = file_raise_errno(file_handle_name(fhandle));
else
r = no_var_pack();
}
free_var(arglist);
return r;
}
/*
* FLOC file_tell(FHANDLE handle)
*/
static package
bf_file_tell(Var arglist, Byte next, void *vdata, Objid progr)
{
package r;
Var fhandle = arglist.v.list[1];
Var rv;
FILE *f;
errno = 0;
if(!file_verify_caller(progr)) {
r = file_raise_notokcall("file_tell", progr);
} else if ((f = file_handle_file_safe(fhandle)) == NULL) {
r = make_raise_pack(E_INVARG, "Invalid FHANDLE", var_ref(fhandle));
} else {
rv.type = TYPE_INT;
if((rv.v.num = ftell(f)) < 0)
r = file_raise_errno(file_handle_name(fhandle));
else
r = make_var_pack(rv);
}
free_var(arglist);
return r;
}
/*
* INT file_eof(FHANDLE handle)
*/
static package
bf_file_eof(Var arglist, Byte next, void *vdata, Objid progr)
{
package r;
Var fhandle = arglist.v.list[1];
Var rv;
FILE *f;
errno = 0;
if(!file_verify_caller(progr)) {
r = file_raise_notokcall("file_eof", progr);
} else if ((f = file_handle_file_safe(fhandle)) == NULL) {
r = make_raise_pack(E_INVARG, "Invalid FHANDLE", var_ref(fhandle));
} else {
rv.type = TYPE_INT;
rv.v.num = feof(f);
r = make_var_pack(rv);
}
free_var(arglist);
return r;
}
/*****************************************************************
* Functions that stat()
*****************************************************************/
/*
* (internal) int(statok) file_stat(Var filespec, package *r, struct stat *buf)