-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathbugetdupupto.c
105 lines (96 loc) · 2.07 KB
/
bugetdupupto.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
/*
* This file is part of "jelio".
* jelio is an input/output library that replaces the standard C IO library.
*
* Copyright: Jens Låås, SLU 2004
* Copyright license: According to GPL, see file COPYING in this directory.
*
*/
#include <stdlib.h>
#include <string.h>
#include "jelio.h"
#include "jelio_internal.h"
/*
Upto "upto" EOF maxlen.
NULL term.
"upto" stored in buffer if encountered.
Overflow is lost.
eof marked if EOF encountered.
-1 on error.
upto_len == 0, upto_len = strlen(upto)
*/
int bugetdupupto(int fd, int *opt_eof, int maxlen, int *opt_lost, char **dbuf, char *upto, int upto_len)
{
int count=0;
int buf;
struct jelio_buf *jb;
size_t mlen=0;
char *tbuf = NULL, *rbuf = *dbuf;
if(opt_eof) *opt_eof = 0;
if(opt_lost) *opt_lost = 0;
if(maxlen<2)
{
if(*dbuf) **dbuf = 0;
return 0;
}
if(!rbuf)
{
/* create a dynamic buffer */
mlen = 32;
rbuf = malloc(mlen+1);
if(!rbuf)
return -1;
}
maxlen--;
if(upto_len == 0) upto_len = strlen(upto);
jb = jelio_buffer(fd);
if(!jb) return -1;
while(1)
{
buf = jelio_buffer_getc(jb);
if(buf == -1)
{
if(opt_eof) *opt_eof = 1;
break;
}
if(count < maxlen)
{
if(mlen && count >= mlen)
{
/* this is a dynamic buffer which needs to be increased */
mlen = mlen << 1;
rbuf = realloc(rbuf, mlen+1);
if(!rbuf) return -1;
}
*(rbuf+count) = buf;
count++;
if(count >= upto_len)
if(!memcmp(rbuf+count-upto_len, upto, upto_len))
break;
}
else
{
if(!tbuf)
{
/* load tail buffer */
tbuf = malloc(upto_len);
if(!tbuf) return -1;
if(count >= upto_len)
memcpy(tbuf, rbuf+count-upto_len, upto_len);
else
memcpy(tbuf+upto_len-count, rbuf, count);
}
if(opt_lost) (*opt_lost)++;
memmove(tbuf, tbuf+1, upto_len-1);
*(tbuf+upto_len-1) = buf;
if(!memcmp(tbuf, upto, upto_len))
{
break;
}
}
}
if(tbuf) free(tbuf);
if(mlen) *dbuf = rbuf;
*(rbuf+count) = 0;
return count;
}