-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_file.c
More file actions
2165 lines (2100 loc) · 58.1 KB
/
Copy pathload_file.c
File metadata and controls
2165 lines (2100 loc) · 58.1 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
/*
* Copyright (C) 1992 by Rush Record
* Copyright (C) 1993 by Charles Sandmann (sandmann@clio.rice.edu)
*
* This file is part of ED.
*
* ED is free software; you can redistribute it and/or modify it under the terms
* of the GNU General Public License as published by the Free Software Foundation.
*
* ED 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with ED
* (see the file COPYING). If not, write to the Free Software Foundation, 675
* Mass Ave, Cambridge, MA 02139, USA.
*/
#include "opsys.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <time.h>
#ifndef NO_MMAP
#include <sys/types.h>
#include <sys/mman.h>
#endif
#include "unistd.h"
#include "memory.h"
#include "file.h"
#include "ctyp_dec.h"
#define FILENAME_OFFSET 40 /* this is the offset from the beginning of diredit records to the file name */
#ifndef O_BINARY
#define O_BINARY 0
#endif
/* NOTE: BLOCK_SIZE cannot be larger than BUFFER_SIZE */
#define BLOCK_SIZE 512
/* NOTE: BLOCK_SIZE must be an even multiple of BINARY_RECSIZE */
#define BUFFER_SIZE 2048
#include "rec.h"
#include "window.h"
#include "ed_dec.h"
typedef struct mmap_str *mmap_ptr;
typedef struct mmap_str
{
mmap_ptr next;
Char *adr;
Int len;
} mmap_node;
static mmap_ptr base = NULL;
static Char months[] = "JanFebMarAprMayJunJulAugSepOctNovDec";
static Char ftpbin = 0; /* flags a binary ftp'ed file was just loaded */
static Char foundusr[128];
static Char foundpwd[128];
static Char fileonly = 0; /* flag to prevent interpretation of fname as dirname */
static Char forcebinary = 0; /* flag to force binary transfer of file */
static Char maperror = 0; /* flag that a map_file error other than ENOENT has occurred */
extern Char *extension();
extern Char *filename();
extern Char *ftp_cwd();
extern Char *last_message();
#ifndef VMS
extern char *sys_errlist[];
#endif
/******************************************************************************\
|Routine: filename_offset
|Callby: command edit
|Purpose: Returns the offset to the file name in diredit records.
|Arguments:
| none
\******************************************************************************/
Int filename_offset()
{
return(FILENAME_OFFSET);
}
/******************************************************************************\
|Routine: mode_string
|Callby: read_in_diredit
|Purpose: Returns a string expressing the file's permissions, as in: rwxr-xr-x.
|Arguments:
| mode is the file mode.
\******************************************************************************/
Char *mode_string(mode)
Int mode;
{
static char rbuf[10];
memset(rbuf,'-',9);
rbuf[9] = '\0';
if(mode & 1)
rbuf[8] = 'x';
if(mode & 2)
rbuf[7] = 'w';
if(mode & 4)
rbuf[6] = 'r';
if(mode & 8)
rbuf[5] = 'x';
if(mode & 16)
rbuf[4] = 'w';
if(mode & 32)
rbuf[3] = 'r';
if(mode & 64)
rbuf[2] = 'x';
if(mode & 128)
rbuf[1] = 'w';
if(mode & 256)
rbuf[0] = 'r';
return(rbuf);
}
/******************************************************************************\
|Routine: size_string
|Callby: read_in_diredit
|Purpose: Returns a string expressing the file size in 6 bytes.
|Arguments:
| size is the size of the file in bytes.
\******************************************************************************/
Char *size_string(size)
Int size;
{
static char rbuf[9];
if(size > 99999999)
sprintf(rbuf,"%5dmeg",size / 1000000);
else
sprintf(rbuf,"%8d",size);
return(rbuf);
}
/******************************************************************************\
|Routine: parse_vms_perms
|Callby: parse_vms_mode read_in_diredit
|Purpose: Parses expressions like RWED. Returns unix-style mode.
|Arguments:
| p is a pointer in the string that presumably contains the permissions. This
| string may be empty, in which case it is terminated by , or \0 or ).
| mode is the returned mode, as in rwx.
\******************************************************************************/
void parse_vms_perms(p,mode)
Char *p,*mode;
{
memset(mode,'-',3);
while(1)
switch(*p++)
{
case 'R':
mode[0] = 'r';
break;
case 'W':
mode[1] = 'w';
break;
case 'E':
mode[2] = 'x';
break;
case 'D':
break;
default:
return;
}
}
/******************************************************************************\
|Routine: parse_vms_mode
|Callby: read_in_diredit
|Purpose: Parses expressions like (RWED,RE,RE,). Returns unix-style mode.
|Arguments:
| p is a pointer in the string that presumably contains the permissions.
| mode is the returned mode, as in rwxr-xr-w.
\******************************************************************************/
void parse_vms_mode(p,mode)
Char *p,*mode;
{
Char *q;
mode[9] = '\0';
if((q = strchr(p,'(')))
{
if((p = strchr(q,',')))
{
parse_vms_perms(++p,mode);
if((q = strchr(p,',')))
{
parse_vms_perms(++q,mode + 3);
if((p = strchr(q,',')))
parse_vms_perms(++p,mode + 6);
else
goto badmode;
}
else
goto badmode;
}
else
goto badmode;
return;
}
badmode:
strcpy(mode,"?????????");
}
/******************************************************************************\
|Routine: fix_vms_dir
|Callby: read_in_diredit
|Purpose: Converts *.dir;1 or *.DIR;1 to [.dir] or [.DIR]. Also strips device
| and directory from the name, and packages it in [].
|Arguments:
| file is the input file name.
| ret is the returned file name (same as file if .dir not present).
\******************************************************************************/
void fix_vms_dir(file,ret)
Char *file,*ret;
{
Char buf[512],*p,*q;
strcpy(buf,file);
q = buf;
if((p = strstr(buf,".DIR;1")))
{
*p = '\0';
while(q != p && *p != ']' && *p != '>')
p--;
if(q != p)
p++;
sprintf(ret,"[.%s]",p);
}
else if((p = strstr(buf,".dir;1"))) /* oddball lowercase VMS system */
{
*p = '\0';
while(q != p && *p != ']' && *p != '>')
p--;
if(q != p)
p++;
sprintf(ret,"[.%s]",++p);
}
else /* just strip device/directory */
{
for(p = buf,q = buf + strlen(buf) - 1;p != q && *q != ']' && *q != '>';)
q--;
if(q != p)
q++;
strcpy(ret,q);
}
}
/******************************************************************************\
|Routine: fake_date
|Callby: read_in_diredit
|Purpose: Makes up a fake set of information about a file when parsing fails.
|Arguments:
| none
\******************************************************************************/
void fake_date(size,year,month,day,hour,minute)
Int *size,*year;
Char *month;
Int *day,*hour,*minute;
{
*size = 0;
*year = 1955;
strcpy(month,"Feb");
*day = 8;
*hour = 6;
*minute = 35;
}
/******************************************************************************\
|Routine: parse_monthyear
|Callby: read_in_diredit
|Purpose: Deals with month and year in VMS listings.
|Arguments:
| monthyear is the string that combines the month and year.
| size is the (possibly) returned file size in bytes.
| year is the (possibly) returned year.
| month is the returned month.
| day is the (possibly) returned day.
| ihour is the (possibly) returned hour.
| minute is the (possibly) returned minute.
| mode is the returned file mode.
| p is a pointer to the listing record.
\******************************************************************************/
void parse_monthyear(monthyear,size,year,month,day,ihour,minute,mode,p)
Char *monthyear;
Int *size,*year;
Char *month;
Int *day,*ihour,*minute;
Char *mode;
Char *p;
{
Char *q;
if((q = strchr(monthyear,'-')))
{
*q++ = '\0';
strcpy(month,monthyear);
if(my_sscanf(q,"%d",year) == 1)
{
parse_vms_mode(p,mode);
return;
}
}
fake_date(size,year,month,day,ihour,minute); /* use fake date info */
}
/******************************************************************************\
|Routine: force_binary
|Callby: edit
|Purpose: Flags that the next file opened will be in binary mode.
|Arguments:
| none
\******************************************************************************/
void force_binary()
{
forcebinary = 1;
}
/******************************************************************************\
|Routine: ftp_fileonly
|Callby: edit
|Purpose: Flags that the next file opened will skip the directory check.
|Arguments:
| none
\******************************************************************************/
void ftp_fileonly()
{
fileonly = 1;
}
/******************************************************************************\
|Routine: netrc_password
|Callby: ftp_open
|Purpose: Returns netrc password or NULL string.
|Arguments:
| none
\******************************************************************************/
Char *netrc_password()
{
return(foundpwd);
}
/******************************************************************************\
|Routine: netrc_token
|Callby: find_netrc
|Purpose: Gets a token from the .netrc file.
|Arguments:
| fp is the file pointer of the .netrc file.
\******************************************************************************/
Char *netrc_token(fp)
FILE *fp;
{
static Char tok[512],*p;
Int c;
while(1)
{
if((c = fgetc(fp)) < 0)
{
fclose(fp);
return(NULL);
}
if(isspace(c))
continue;
p = tok;
*p++ = c;
while(1)
{
if((c = fgetc(fp)) < 0)
{
*p++ = '\0';
return(tok);
}
if(isspace(c))
{
*p++ = '\0';
return(tok);
}
*p++ = c;
}
}
}
/******************************************************************************\
|Routine: find_netrc
|Callby: extract_host
|Purpose: Looks up a host in the user's netrc file. Returns pointer to username,
| or NULL if no matching host is found.
|Arguments:
| host is the host we're looking for.
\******************************************************************************/
Char *find_netrc(host)
Char *host;
{
FILE *fp;
Char *p,file[512];
struct stat statbuf;
foundpwd[0] = '\0'; /* we check for netrc password when opening connection */
#ifdef VMS
strcpy(file,"SYS$LOGIN:.NETRC;");
#else
strcpy(file,"$HOME/.netrc");
#endif
envir_subs(file);
if(!stat(file,&statbuf))
{
if((statbuf.st_mode & 0x1ff) == 0x180) /* require rw------- permission */
{
if((fp = fopen(file,"r"))) /* open the .netrc file */
{
while((p = netrc_token(fp))) /* get tokens */
{
if(!inscmp(p,"machine"))
{
if(!(p = netrc_token(fp))) /* get machine name */
return(NULL);
if(!inscmp(host,p)) /* a hit on the machine name */
{
while((p = netrc_token(fp)))
{
if(!inscmp(p,"password"))
{
if(!(p = netrc_token(fp)))
return(NULL);
fclose(fp);
strcpy(foundpwd,p);
return(foundusr);
}
else if(!inscmp(p,"login"))
{
if(!(p = netrc_token(fp)))
return(NULL);
strcpy(foundusr,p);
}
else if(!inscmp(p,"machine"))
{
fclose(fp);
return(foundusr); /* user but no password, prompt them later */
}
}
return(foundusr); /* user but no password, prompt them later */
}
}
}
}
}
}
return(NULL);
}
/******************************************************************************\
|Routine: was_binary
|Callby: load_file
|Purpose: Tells caller whether the last file loaded was loaded in BINARY mode.
|Arguments:
| none
\******************************************************************************/
Int was_binary()
{
if(ftpbin)
{
ftpbin = 0;
return(1);
}
return(0);
}
/******************************************************************************\
|Routine: extract_host
|Callby: output_file read_in_diredit
|Purpose: Extracts host and user names from file spec, and pointer to path.
|Arguments:
| i is the offset in lbuf of the path name.
| lbuf is the complete file name.
| host is the returned host name.
| usr is the returned user name.
\******************************************************************************/
Char *extract_host(i,lbuf,host,usr)
Int i;
Char *lbuf,*host,*usr;
{
Int l;
Char *p,*q,*r;
p = lbuf + i; /* points to directory name */
memcpy(host,lbuf + 1,(l = p - lbuf - 2));
host[l] = '\0';
if((q = strchr(host,'@')))
{
foundpwd[0] = '\0'; /* prevent old password from being used for new user/host */
memcpy(usr,host,(l = q - host));
usr[l] = '\0';
for(q++,r = host;(*r++ = *q++);); /* eliminate the username from the host string */
}
else /* no user specified, check .netrc */
{
if((q = find_netrc(host)))
strcpy(usr,q);
else
strcpy(usr,"anonymous"); /* no .netrc, use "anonymous" */
}
return(p);
}
#ifndef NO_FTP
/******************************************************************************\
|Routine: host_filename
|Callby: output_file read_in_diredit
|Purpose: Parses filenames depending on host type.
|Arguments:
| i is the offset in lbuf of the path name.
| lbuf is the complete file name.
| fname is the returned, processed file name.
\******************************************************************************/
Char *host_filename(i,lbuf,fname)
Int i;
Char *lbuf,*fname;
{
Int ftpsystem;
Char *p,*q,*r,buf[512];
ftpsystem = ftp_system(lbuf);
p = lbuf + i; /* points to directory name */
if(ftp_unixy(ftpsystem)) /* if server system is unix or dos or os/2 or wnt... */
{
if(!*p)
strcpy(p,"."); /* turn /xxx.yyy.zzz: into /xxx.yyy.zzz:/ */
while((q = strstr(lbuf + i,"/../"))) /* handle '..' present in file name */
{
for(r = q - 1;r > lbuf && *r != '/' && *r != ':';r--);
if(*r == ':') /* they tried to back up from top level */
{
*++r = '/';
*++r = '\0';
}
else if(r != lbuf && *r == '/') /* eat everything from preceeding / to last . in /../ */
for(q += 3;(*r++ = *q++););
}
if(p[strlen(p) - 1] != '/') /* be sure dirname ends with '/' */
strcat(p,"/");
if(!strncmp(p,"./",2))
{
sprintf(buf,"%s/%s",ftp_cwd(),p + 2);
sprintf(p,"%s",buf);
while((q = strstr(lbuf,"//")))
for(r = q + 1;(*q++ = *r++););
}
}
else /* server is VMS or VM */
{
if(!*p) /* nothing present past /host: */
strcpy(p,ftp_cwd());
else if(!strchr(p,'['))
{
strcpy(buf,ftp_cwd());
strcat(buf,p);
strcpy(p,buf);
}
if(p[strlen(p) - 1] == '/') /* strip trailing '/' from dirname */
p[strlen(p) - 1] = '\0';
while((q = strstr(p,"][."))) /* collapse things like [a][.b] to [a.b] */
while((*q = *(q + 2)))
q++;
if((q = strstr(p,"[-]"))) /* move up in directory hierarchy if indicated */
{
if(ftpsystem == 'I' || ftpsystem == 'T') /* VM and MTS systems don't have 'up' */
*q = '\0';
else
{
while(q > p)
{
if(*--q == '.')
{
strcpy(q,"]");
break;
}
if(*q == ':') /* they tried to move up from top level */
{
strcpy(q,ftp_cwd());
break;
}
}
if(q == p)
strcpy(p,ftp_cwd());
}
}
else if(!strncmp(p,"[]",2))
{
sprintf(buf,"%s%s",ftp_cwd(),p + 2);
sprintf(p,"%s",buf);
}
else if(!strncmp(p,"[.",2))
{
strcpy(buf,ftp_cwd());
strcat(buf,p);
strcpy(p,buf);
while((q = strstr(p,"][."))) /* collapse things like [a][.b] to [a.b] */
while((*q = *(q + 2)))
q++;
}
}
strcpy(fname,lbuf); /* return the actual name */
return(p);
}
/******************************************************************************\
|Routine: ftp_date
|Callby: load_file
|Purpose: Converts a directory listing date/time entry into yyyy mm-dd hh:mm.
|Arguments:
| month is the month, one of Jan, Feb,...
| day is the day of the month.
| hour is hh:mm or yyyy.
\******************************************************************************/
Char *ftp_date(month,day,hour)
Char *month,*hour;
Int day;
{
Char *buf;
static Char buf2[128];
time_t l;
/* get the year */
if(hour[2] == ':')
{
l = time(0);
buf = (Char *)ctime(&l);
memcpy(buf2,buf + 20,4);
}
else
memcpy(buf2,hour,4);
buf2[4] = ' ';
/* do the month */
sprintf(buf2 + 5,"%02d-",(int)((strstr(months,month) - months) / 3) + 1);
/* do the day */
sprintf(buf2 + 8,"%02d",day);
buf2[10] = ' ';
/* do hours, minutes */
if(hour[2] != ':')
strcpy(buf2 + 11,"00:00");
else
memcpy(buf2 + 11,hour,5);
buf2[16] = '\0';
return(buf2);
}
#endif
/******************************************************************************\
|Routine: proper_date
|Callby: read_in_diredit
|Purpose: Converts a statbuf mtime entry into yyyy mm-dd hh:mm.
|Arguments:
| mtime is the statbuf mtime entry.
\******************************************************************************/
Char *proper_date(mtime)
time_t mtime;
{
Char *buf,monbuf[4];
static Char buf2[64];
buf = ctime(&mtime);
if(!buf)
{
strcpy(buf2,"1960 04-18 02:35"); /* CWS if mtime bogus */
return(buf2);
}
memcpy(buf2,buf + 20,4);
buf2[4] = ' ';
memcpy(monbuf,buf + 4,3);
monbuf[3] = '\0';
sprintf(buf2 + 5,"%02d-",(int)((strstr(months,monbuf) - months) / 3) + 1);
memcpy(buf2 + 8,buf + 8,8);
if(buf2[8] == ' ')
buf2[8] = '0';
buf2[16] = '\0';
return(buf2);
}
/******************************************************************************\
|Routine: map_file
|Callby: load_file
|Purpose: Maps a file into memory.
|Arguments:
| file is the name of the file.
| bytes is the returned size of the mapped area in bytes.
\******************************************************************************/
Char *map_file(file,bytes)
Char *file;
Int *bytes;
{
struct stat statbuf;
Int fd;
Char *p,*errmsg,buf[512];
mmap_ptr m;
#ifdef VMS
Char *q;
Int remain,nbytes;
#endif
#ifdef NO_MMAP
Int l;
#endif
/* get the file's size in bytes */
if(stat(file,&statbuf) < 0)
{
#ifdef VMS
return(0); /* VMS doesn't really support errno, nor even vaxc$errno */
#else
report_error:
if(errno == ENOENT)
return(0);
sprintf(buf,"Unable to open file '%s'.",file);
slip_message(buf);
errmsg = (Char *)sys_errlist[errno];
slip_message(errmsg);
wait_message();
maperror = 1;
return(0);
#endif
}
/* open the file */
#ifdef VMS
if((fd = open(file,O_RDONLY,0,"ctx=rec","rfm=fix","mrs=512","shr=get,put,del,mse,upi,upd")) < 0)
return(0);
#else
if((fd = open(file,O_RDONLY | O_BINARY,0)) < 0)
goto report_error;
#endif
#ifdef NO_MMAP
if(!(p = (Char *)imalloc(statbuf.st_size)))
{
close(fd);
return(0);
}
#ifdef VMS
q = p;
remain = statbuf.st_size;
while(remain > 0)
{
if((nbytes = read(fd,q,remain)) <= 0)
break;
q += nbytes;
remain -= nbytes;
}
statbuf.st_size -= remain;
#else
if((l = read(fd,p,statbuf.st_size)) != statbuf.st_size)
{
slip_message("That file didn't read in correctly.");
wait_message();
statbuf.st_size = l;
}
#endif
close(fd);
#else
p = (Char *)mmap(0,statbuf.st_size,PROT_READ | PROT_WRITE,MAP_PRIVATE,fd,0);
close(fd);
if(p == (Char *)(-1))
return(0);
#endif
for(m = (mmap_ptr)&base;m->next;m = m->next);
m->next = (mmap_ptr)imalloc(sizeof(mmap_node));
m = m->next;
m->next = NULL;
m->adr = p;
*bytes = m->len = statbuf.st_size;
return(p);
}
/******************************************************************************\
|Routine: unmap_file
|Callby: include_file main
|Purpose: Unmaps a file.
|Arguments:
| adr is the base address of the mapped region.
\******************************************************************************/
void unmap_file(adr)
Char *adr;
{
mmap_ptr m,p;
for(p = (mmap_ptr)&base,m = base;m;p = m,m = m->next)
if(m->adr == adr)
{
#ifdef NO_MMAP
ifree(adr);
#else
munmap(adr,m->len);
#endif
p->next = m->next;
ifree(m);
return;
}
vtend();
printf("\nA call to unmap_file passed an unrecognized address.\n");
cleanup(0);
exit(0);
}
/******************************************************************************\
|Routine: checkbook
|Callby: load_file
|Purpose: Checks whether a file has a bookmark. Returns the position found in
| the bookmark file, or 0.
|Arguments:
| editfile is the name of the file.
| bookmark is the returned bookmark file name, or null string.
| byteoffset is the byte offset within the record.
\******************************************************************************/
Int checkbook(editfile,bookmark,byteoffset)
Char *editfile,*bookmark;
Int *byteoffset;
{
#ifdef GNUDOS
bookmark[0] = '\0';
return(0);
#else
FILE *fp;
Int initline;
strcpy(bookmark,editfile);
strcat(bookmark,"-EDbookmark");
initline = 0;
if((fp = fopen(bookmark,"r")))
{
if(my_fscanf(fp,"%d,%d",&initline,byteoffset) == 2)
{
if(initline < 1)
initline = 1;
fclose(fp);
}
else
{
fclose(fp);
bookmark[0] = '\0';
}
}
else
bookmark[0] = '\0';
return(initline);
#endif
}
/******************************************************************************\
|Routine: read_stdin
|Callby: load_file
|Purpose: Reads an ASCII file from standard input, creating records.
|Arguments:
| fname is the buffer that receives the file name.
| base is the base of the file's record queue.
\******************************************************************************/
Int read_stdin(fname,base)
Char *fname;
rec_ptr base;
{
Char buffer[BUFFER_SIZE];
register rec_ptr new;
register Int i,nrecs;
nrecs = 0;
strcpy(fname," Standard Input ");
new = NULL;
while(gets(buffer))
{
if((i = strlen(buffer)) == BUFFER_SIZE)
i--;
buffer[i++] = '\n';
nrecs++;
new = (rec_ptr)imalloc(sizeof(rec_node)); /* get new record */
insq(new,base->prev);
new->data = (Char *)imalloc(i); /* get buffer for record */
if(--i > 0) /* copy record data if present */
memcpy(new->data,buffer,i);
new->data[i] = 0;
new->length = i;
new->recflags = 1;
}
return(nrecs);
}
/******************************************************************************\
|Routine: read_stdin_binary
|Callby: load_file
|Purpose: Reads a binary file from standard input, creating records.
|Arguments:
| fname is the buffer that receives the file name.
| base is the base of the file's record queue.
\******************************************************************************/
Int read_stdin_binary(fname,base)
Char *fname;
rec_ptr base;
{
Char buffer[BUFFER_SIZE];
register Char *p,*stop;
register rec_ptr new;
register Int i,nrecs;
nrecs = 0;
strcpy(fname," Standard Input ");
for(p = buffer,stop = buffer + BINARY_RECSIZE;(i = getchar()) != EOF;)
{
*p++ = i;
if(p == stop)
{
nrecs++;
new = (rec_ptr)imalloc(sizeof(rec_node));
insq(new,base->prev);
new->data = (Char *)imalloc(BINARY_RECSIZE);
memcpy(new->data,(p = buffer),BINARY_RECSIZE);
new->length = BINARY_RECSIZE;
new->recflags = 1;
}
}
if((i = p - buffer))
{
nrecs++;
new = (rec_ptr)imalloc(sizeof(rec_node));
insq(new,base->prev);
new->data = (Char *)imalloc(i);
memcpy(new->data,buffer,i);
new->length = i;
new->recflags = 1;
}
return(nrecs);
}
/******************************************************************************\
|Routine: read_in
|Callby: load_file
|Purpose: Reads an ASCII file, creating records.
|Arguments:
| mapped is the mapped file pointer.
| nbytes is the size of the mapped region.
| base is the base of the file's record queue.
\******************************************************************************/
Int read_in(mapped,nbytes,base)
Char *mapped;
Int nbytes;
rec_ptr base;
{
rec_ptr new;
register Int nrecs,inprog;
register Char *p,*q,*stop;
nrecs = 0;
new = NULL;
inprog = 0;
for(stop = mapped + nbytes,q = p = mapped;p != stop;p++)
if(*p == '\n')
{
nrecs++;
new = (rec_ptr)imalloc(sizeof(rec_node)); /* get new record */
insq(new,base->prev);
new->data = q; /* use mapped address as record data */
if((new->length = p - q) > 0)
if(new->data[new->length - 1] == '\r') /* NULL-terminate the buffer (mapped data are MAP_PRIVATE) */
new->length--;
new->data[new->length] = '\0';
new->recflags = 0; /* don't free this buffer, it's mapped file data */
q = p + 1;
inprog = 0;
}
else
inprog = 1;
if(inprog) /* the last record doesn't have newline termination, handle it specially */
{
#ifdef GNUDOS
for(stop = q;stop != p;stop++)
if(*stop == 26)
break;
if(stop != p)
{
*stop = '\0';
if(!strlen(q))
return(nrecs);
p = stop;
}
#endif
nrecs++;
new = (rec_ptr)imalloc(sizeof(rec_node)); /* get new record */
insq(new,base->prev);
new->length = p - q;
new->data = (Char *)imalloc(new->length + 1); /* use mapped address as record data */
memcpy(new->data,q,new->length); /* copy data to buffer */
new->data[new->length] = 0; /* NULL-terminate the buffer (mapped data are MAP_PRIVATE) */
new->recflags = 1; /* this buffer should be freed */
}
return(nrecs);
}
/******************************************************************************\