2
/* Copyright (c) 2000-2001 Fabien Tassin <fta@sofaraway.org> */
15
FIO *FIO_init(int fh) {
18
fio = (FIO *) malloc(sizeof(struct _FIO));
20
fio->size = FIO_BUFFER;
21
fio->buffer = (char *) malloc(fio->size);
22
fio->start = fio->end = fio->buffer;
28
void FIO_free(FIO *fio) {
33
/* read a line in the given filehandle. Remove trailing '\n' or '\r\n' */
34
char *FIO_readline(FIO *fio) {
39
if (fio->end > fio->start) {
40
/* the buffer is not empty. Look for the first newline */
41
p = memchr(fio->start, '\n', fio->end - fio->start);
45
if (p > fio->start && *(p - 1) == '\r')
49
fio->start = p + (f ? 2 : 1);
52
fprintf(stderr, "<<< \"%s%s\\n\"\n", old, f ? "\\r" : "");
55
if (fio->start != fio->buffer) {
56
/* move unused data at the beginning of the buffer */
57
for (p = fio->buffer, q = fio->start; q < fio->end; )
59
fio->start = fio->buffer;
68
/* read new data from the filehandle */
69
sz = read(fio->fh, p, fio->size - (fio->end - fio->start));
71
perror("read() failed");
72
fio->status = FIO_ERROR;
76
return(NULL); /* nothing to read */
78
fio->start = fio->buffer;
81
/* Look for the first newline */
82
p = memchr(fio->start, '\n', fio->end - fio->start);
86
if (p > fio->start && *(p - 1) == '\r')
90
fio->start = p + (f ? 2 : 1);
93
fprintf(stderr, "<<< \"%s%s\\n\"\n", old, f ? "\\r" : "");
96
if ((fio->end - fio->start) >= fio->size) {
97
/* not found but the buffer is full. This line is too long */
98
fio->status = FIO_TOOLONG;
99
fio->start = fio->end;
106
/* write the buffer into the filehandle (no '\r\n' added) */
107
int FIO_write(FIO *fio, char* buf) {
108
int len = strlen(buf);
110
fprintf(stderr, ">>> '%s'\n", buf);
111
return(write(fio->fh, buf, len) == len);
114
/* write the buffer into the filehandle ('\r\n' added) */
115
int FIO_writeline(FIO *fio, char* buf) {
119
s = (char *) malloc(strlen(buf) + 2);
122
fprintf(stderr, ">>> \"%s\\r\\n\"\n", buf);
125
r = FIO_write(fio, strcat(s, "\r\n"));