Skip to content

Commit

Permalink
readline.c added
Browse files Browse the repository at this point in the history
  • Loading branch information
cnphil committed Apr 5, 2013
1 parent 771f238 commit 01622c5
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 additions & 0 deletions readline.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "unp.h"

ssize_t
readline(int fd, void *vptr, size_t maxlen)
{
ssize_t n, rc;
char c, *ptr;

ptr = vptr;
for (n = 1; n < maxlen; n++) {
if ( (rc = read(fd, &c, 1)) == 1) {
*ptr++ = c;
if (c == '\n')
break;
} else if (rc == 0) {
if (n == 1)
return(0); /* EOF, no data read */
else
break; /* EOF, some data was read */
} else
return(-1); /* error */
}

*ptr = 0;
return(n);
}

0 comments on commit 01622c5

Please sign in to comment.