Make reads() more robust and easier to follow
This commit is contained in:
parent
0599f873dc
commit
703fc67636
1 changed files with 12 additions and 10 deletions
|
@ -60,19 +60,21 @@ double round( double a ) {
|
||||||
|
|
||||||
|
|
||||||
int reads(int fd, char *buf, unsigned int len) {
|
int reads(int fd, char *buf, unsigned int len) {
|
||||||
char c;
|
unsigned int i = 0;
|
||||||
int res;
|
int res;
|
||||||
unsigned int i;
|
char c;
|
||||||
for (i=0;
|
|
||||||
(i < (len-1)) && ((res = read(fd, &c, 1)) != 0)
|
|
||||||
&& ((c != '\n') && (c != '\r'));
|
|
||||||
i++)
|
|
||||||
buf[i] = c;
|
|
||||||
|
|
||||||
if (buf[i] == '\r')
|
len--;
|
||||||
buf[i] = '\n';
|
while ( (i < len) && ((res = read(fd, &c, 1)) != 0)
|
||||||
|
&& ((c != '\n') && (c != '\r')) )
|
||||||
|
{
|
||||||
|
buf[i++] = c;
|
||||||
|
}
|
||||||
|
|
||||||
buf[++i] = '\0';
|
if ((c == '\r') || (c == '\n'))
|
||||||
|
buf[i++] = '\n';
|
||||||
|
|
||||||
|
buf[i] = '\0';
|
||||||
|
|
||||||
return res;
|
return res;
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue