Skip to content

Instantly share code, notes, and snippets.

@nl0x
Created May 17, 2016 09:11
Show Gist options
  • Save nl0x/d0e51a861d634ec4524e3d6789307120 to your computer and use it in GitHub Desktop.
Save nl0x/d0e51a861d634ec4524e3d6789307120 to your computer and use it in GitHub Desktop.
bool babeld_handle_in(struct context *ctx, int fd) {
size_t old_len = ctx->babeld_buffer == NULL ? 0 : strlen(ctx->babeld_buffer);
size_t new_len = old_len + CHUNKSIZE + 1;
ctx->babeld_buffer = realloc(ctx->babeld_buffer, new_len);
if (ctx->babeld_buffer == NULL)
exit_errno("Cannot allocate buffer");
ssize_t len = read(fd, ctx->babeld_buffer + old_len, CHUNKSIZE);
if (len == 0)
return false;
if (len == -1 && errno == EAGAIN)
return true;
ctx->babeld_buffer[old_len + len] = 0;
char *stringp, *line;
while (1) {
stringp = ctx->babeld_buffer;
line = strsep(&stringp, "\n");
if (stringp == NULL)
break; // no line found
babeld_parse_line(ctx, line);
memmove(ctx->babeld_buffer, stringp, strlen(stringp) + 1);
ctx->babeld_buffer = realloc(ctx->babeld_buffer, strlen(ctx->babeld_buffer) + 1);
}
return true;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment