comparison stream/stream_ftp.c @ 35335:85003f7fa621

stream ftp: readline: Always try to read complete lines If there is not enough space in the provided line buffer just skip the remaining bytes until reaching EOL. Usually we are only interested in the first 5 characters and for everything else the (on-stack) response buffer should still be big enough.
author al
date Tue, 20 Nov 2012 22:18:25 +0000
parents 3397976a029b
children 9c334690f765
comparison
equal deleted inserted replaced
35334:3397976a029b 35335:85003f7fa621
102 return select(fd+1, &fds, NULL, NULL, &tv) > 0; 102 return select(fd+1, &fds, NULL, NULL, &tv) > 0;
103 } 103 }
104 104
105 /* 105 /*
106 * read a line of text 106 * read a line of text
107 *
108 * If the line is too long to fit in the buffer, provided via parameters
109 * buf and max, the remaining characters are skipped. So the next call to
110 * this function is synchronized to the start of the following response
111 * line.
107 * 112 *
108 * The parameter buf will always be initialized as long as max is bigger 113 * The parameter buf will always be initialized as long as max is bigger
109 * then 1. If nothing is read it will contain an empty string. 114 * then 1. If nothing is read it will contain an empty string.
110 * 115 *
111 * return -1 on error or bytecount 116 * return -1 on error or bytecount
142 } 147 }
143 break; 148 break;
144 } 149 }
145 } 150 }
146 if (max == 1) { 151 if (max == 1) {
147 *buf = '\0'; 152 char *q = memchr(ctl->cget, '\n', ctl->cavail);
148 break; 153
154 if (q) { // found EOL: update state and return
155 ++q;
156 ctl->cavail -= q - ctl->cget;
157 ctl->cget = q;
158
159 break;
160 }
161
162 // receive more data to find end of current line
163 ctl->cget = ctl->cput;
149 } 164 }
150 if (ctl->cput == ctl->cget) { 165 if (ctl->cput == ctl->cget) {
151 ctl->cput = ctl->cget = ctl->buf; 166 ctl->cput = ctl->cget = ctl->buf;
152 ctl->cavail = 0; 167 ctl->cavail = 0;
153 ctl->cleft = BUFSIZE; 168 ctl->cleft = BUFSIZE;