|
|
1.1 root 1: #include <stdio.h>
2: #include "pad.h"
3: #define NPCBL 8 /* Number of entries in the circular list */
4:
5: PAD *
6: Pfopen(f)
7: FILE *f;
8: {
9: register PAD *p=NULL;
10: register i;
11: if(f!=NULL && (p=(PAD *)malloc(sizeof (PAD)))!=NULL){
12: p->Pcbl=(_PCBL *)malloc(NPCBL*sizeof(_PCBL));
13: if(p->Pcbl==NULL){
14: free(p);
15: return(NULL);
16: }
17: for(i=0; i<NPCBL; i++){
18: p->Pcbl[i].Pnextp= &p->Pcbl[(i+1)%NPCBL];
19: p->Pcbl[(i+1)%NPCBL].Pprevp= &p->Pcbl[i];
20: p->Pcbl[i].Pbase=NULL;
21: p->Pcbl[i].Phiwat=NULL;
22: }
23: p->Pfile=f;
24: f->_cnt=0; /* To be safe */
25: f->_flag|=_IOREAD;
26: }
27: return(p);
28: }
29:
30: int
31: _Pfilbuf(p)
32: register PAD *p;
33: {
34: register _PCBL *pcbl;
35: register FILE *f;
36:
37: pcbl = p->Pcbl;
38: f = p->Pfile;
39: if ((f->_flag & _IOREAD) == 0 || f->_flag & _IOSTRG)
40: return (EOF);
41:
42: /*
43: * if this buffer is full, use next one
44: */
45: if (pcbl->Phiwat >= pcbl->Pbase + BUFSIZ) {
46: p->Pcbl = pcbl = pcbl->Pnextp;
47: f->_base = f->_ptr = pcbl->Pbase;
48: f->_cnt = pcbl->Phiwat - pcbl->Pbase;
49: }
50:
51: if (pcbl->Pbase == NULL) {
52: if ((pcbl->Pbase = (char *) malloc(BUFSIZ)) == NULL) {
53: fprintf(stderr,"pad: can't malloc\n"); /* ? */
54: f->_flag |= _IOERR;
55: return (EOF);
56: }
57: pcbl->Phiwat = f->_base = pcbl->Pbase;
58: f->_flag |= _IOMYBUF;
59: }
60:
61: /*
62: * if no more saved characters in this buffer,
63: * read some new ones
64: */
65: if (f->_cnt <= 0) {
66: f->_ptr = pcbl->Phiwat;
67: f->_cnt = read(fileno(f),f->_ptr,(pcbl->Pbase + BUFSIZ) - f->_ptr);
68: if (f->_cnt > 0) {
69: pcbl->Phiwat += f->_cnt;
70: pcbl->Pnextp->Phiwat = pcbl->Pnextp->Pbase;
71: }
72: }
73:
74: if (--f->_cnt < 0) {
75: f->_flag |= (f->_cnt == -1 ? _IOEOF : _IOERR);
76: f->_cnt = 0;
77: return (EOF);
78: }
79: return (*f->_ptr++);
80: }
81:
82: Pclose(p)
83: register PAD *p;
84: {
85: register i;
86: register _PCBL *pcbl=p->Pcbl;
87: register _PCBL *old;
88: for(i=0; i<NPCBL; i++){
89: if(pcbl->Pbase)
90: free(pcbl->Pbase);
91: old=pcbl;
92: pcbl=pcbl->Pnextp;
93: free(old);
94: }
95: fclose(p->Pfile);
96: free(p);
97: }
98:
99: int
100: Pbackc(p)
101: register PAD *p;
102: {
103: register _PCBL *pcbl;
104: register FILE *f;
105: f=p->Pfile;
106: pcbl=p->Pcbl;
107: ++f->_cnt;
108: if(--f->_ptr < pcbl->Pbase){
109: pcbl=pcbl->Pprevp; /* Only local... */
110: if(pcbl->Phiwat <= pcbl->Pbase){
111: --f->_cnt;
112: ++f->_ptr;
113: return(EOF);
114: }
115: p->Pcbl=pcbl; /* ...until now */
116: f->_ptr=pcbl->Phiwat-1;
117: f->_cnt=1;
118: }
119: return(*f->_ptr);
120: }
121:
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.