File: buffer.c

package info (click to toggle)
diablo 1.13-1
  • links: PTS
  • area: non-free
  • in suites: hamm
  • size: 804 kB
  • ctags: 875
  • sloc: ansic: 8,308; perl: 1,908; sh: 186; csh: 81; makefile: 67
file content (290 lines) | stat: -rw-r--r-- 5,847 bytes parent folder | download
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

/*
 * LIB/BUFFER.C
 *
 * (c)Copyright 1997, Matthew Dillon, All Rights Reserved.  Refer to
 *    the COPYRIGHT file in the base directory of this distribution 
 *    for specific rights granted.
 *
 * Buffered I/O, may be used for reading or writing, but not both.
 */

#include "defs.h"

Prototype Buffer *bopen(int fd);
Prototype void bclose(Buffer *b, int closeFd);
Prototype void efree(char **pcopy, int *pcopymax);
Prototype char *bgets(Buffer *b, int *pbytes);
Prototype char *egets(Buffer *b, int *pbytes);
Prototype void bwrite(Buffer *b, const void *data, int bytes);
Prototype void bflush(Buffer *b);
Prototype int  berror(Buffer *b);
Prototype off_t btell(Buffer *b);
Prototype int bunget(Buffer *b, int n);
Prototype int bextfree(Buffer *b);

Buffer *
bopen(int fd)
{
    Buffer *b;
    int pgSize;

    b = pagealloc(&pgSize, 1);
    bzero(b, sizeof(Buffer));
    b->bu_BufMax = pgSize - offsetof(Buffer, bu_Buf[0]) - sizeof(int);
    b->bu_DataMax = b->bu_BufMax;
    b->bu_Data = b->bu_Buf;
    b->bu_Fd = fd;

    return(b);
}

void
bclose(Buffer *b, int closeFd)
{
    if (b != NULL) {
	if (closeFd && b->bu_Fd >= 0) {
	    close(b->bu_Fd);
	    b->bu_Fd = -1;
	}
	b->bu_Beg = 0;
	b->bu_NLScan = 0;
	b->bu_End = 0;
	(void)bextfree(b);
	pagefree(b, 1);
    }
}

/*
 * bgets() - get a line from the input.  Return NULL if no data
 *	     ready to read (non-blocking only), -1 on EOF, or a
 *	     pointer to the data.  If ptr[bytes-1] != '\n', a full
 *	     line could not be returned due to the limited buffer size.
 *
 *	     if a real pointer is returned, *pbytes will be non-zero. 
 */

char *
bgets(Buffer *b, int *pbytes)
{
    int i;

    if (b->bu_Beg == b->bu_End) {
	b->bu_Beg = 0;
	b->bu_NLScan = 0;
	b->bu_End = 0;
    }

    for (;;) {
	/*
	 * look for a newline, return the line as appropriate.  NLScan
	 * is an optimization so we do not rescan portions of the input
	 * buffer that we have already checked.
	 */

	for (i = b->bu_NLScan; i < b->bu_End; ++i) {
	    if (b->bu_Data[i] == '\n') {
		char *p = b->bu_Data + b->bu_Beg;

		/*
		 * terminate buffer, removing newline, or return
		 * exact size of buffer with nothing removed or terminated.
		 */

		if (pbytes) {
		    *pbytes = i + 1 - b->bu_Beg;
		} else {
		    b->bu_Data[i] = 0;
		}
		/*
		 * note: cannot reset to 0/0 or bunget will not work.
		 */
		b->bu_Beg = i + 1;
		b->bu_NLScan = b->bu_Beg;
		return(p);
	    }
	}
	b->bu_NLScan = i;

	/*
	 * If there is no room to append new data, attempt to
	 * make some.
	 */
	if (b->bu_End == b->bu_DataMax && b->bu_Beg != 0) {
	    memmove(b->bu_Data, b->bu_Data + b->bu_Beg, b->bu_End - b->bu_Beg);
	    b->bu_End -= b->bu_Beg;
	    b->bu_NLScan -= b->bu_Beg;
	    b->bu_Beg = 0;
	}

	/*
	 * If the buffer is full, we have an overflow problem.
	 */
	if (b->bu_End == b->bu_DataMax) {
	    char *p = b->bu_Data + b->bu_Beg;

	    if (pbytes == NULL) {
		syslog(LOG_ERR, "Line buffer overflow fd %d", b->bu_Fd);
		p = (void *)-1;
	    } else {
		*pbytes = b->bu_End - b->bu_Beg;
		/*
		 * note: cannot reset to 0 or bunget will not work
		 */
		b->bu_Beg = b->bu_NLScan = b->bu_End;
	    }
	    return(p);
	}
	/*
	 * otherwise read some data and repeat
	 */
	{
	    int n;

	    n = read(b->bu_Fd, b->bu_Data + b->bu_End, b->bu_DataMax - b->bu_End);

	    /*
	     * EOF
	     */
	    if (n == 0) {
		return((void *)-1);
	    }
	    if (n < 0) {
		if (errno == EAGAIN || errno == EINTR)
		    return(NULL);
		return((void *)-1);
	    }
	    b->bu_End += n;
	}
    }
    /* not reached */
    return((void *)-1);
}

/*
 * egets() - same as bgets, but will allocate an extended buffer to
 *	     fit the line if necessary.
 */

char *
egets(Buffer *b, int *pbytes)
{
    char *ptr = NULL;

    while ((ptr = bgets(b, pbytes)) != NULL && ptr != (char *)-1) {
	/*
	 * looking for newline
	 */
	if (ptr[*pbytes-1] == '\n') {
	    break;
	}
	/*
	 * if an overflow occurs, b->bu_Beg is guarenteed to be 0
	 */
	{
	    int cpyLen;
	    char *cpy = pagealloc(&cpyLen, *pbytes * 3 / 2);

	    bcopy(b->bu_Data, cpy, b->bu_End);
	    if (b->bu_Data != b->bu_Buf) 
		pagefree(b->bu_Data, b->bu_DataMax);
	    b->bu_Data = cpy;
	    b->bu_DataMax = cpyLen;
	}
    }
    return(ptr);
}

/*
 * bunget() partially undoes the most recent read.  You cannot bunget more
 * then the number of bytes in the last read.
 */

int
bunget(Buffer *b, int n)
{
    int r = -1;

    if (n <= b->bu_Beg) {
	b->bu_Beg -= n;
	r = 0;
    }
    return(r);
}

void
bwrite(Buffer *b, const void *data, int bytes)
{
    while (bytes > 0) {
	int n = b->bu_DataMax - b->bu_End;

	if (n > bytes)
	    n = bytes;
	memcpy(b->bu_Data + b->bu_End, data, n);
	b->bu_End += n;
	data = (const char *)data + n;
	bytes -= n;

	if (b->bu_End == b->bu_DataMax)
	    bflush(b);
    }
}

/*
 * bfreebuf() - free extended buffer if possible.  Return
 *		0 on success, -1 on failure.
 */

int
bextfree(Buffer *b)
{
    int r = 0;

    if (b->bu_Data != b->bu_Buf) {
	r = -1;
	if (b->bu_End - b->bu_Beg <= b->bu_BufMax) {
	    r = 0;
	    if (b->bu_Beg != b->bu_End)
		bcopy(b->bu_Data, b->bu_Buf, b->bu_End - b->bu_Beg);
	    b->bu_NLScan -= b->bu_Beg;
	    b->bu_End -= b->bu_Beg;
	    b->bu_Beg = 0;
	    pagefree(b->bu_Data, b->bu_DataMax);
	    b->bu_Data = b->bu_Buf;	  /* not really necessary */
	    b->bu_DataMax = b->bu_BufMax; /* not really necessary */
	}
    }
    return(r);
}

void
bflush(Buffer *b)
{
    int n;

    if (b->bu_Error == 0) {
	while (b->bu_Beg != b->bu_End) {
	    n = write(b->bu_Fd, b->bu_Data + b->bu_Beg, b->bu_End - b->bu_Beg);
	    if (n < 0) {
		b->bu_Error = 1;
		break;
	    }
	    b->bu_Beg += n;
	}
    }
    b->bu_Beg = b->bu_End = b->bu_NLScan = 0;
    (void)bextfree(b);
}

off_t
btell(Buffer *b)
{
    return(lseek(b->bu_Fd, 0L, 1) + (b->bu_End - b->bu_Beg));
}

int
berror(Buffer *b)
{
    return(b->bu_Error);
}