Skip to content
Snippets Groups Projects
Commit 03c1a4ad authored by Daniel Salzman's avatar Daniel Salzman
Browse files

Fix fgetln usage

refs #2137
parent 7f37a6ec
No related branches found
No related tags found
No related merge requests found
......@@ -17,8 +17,14 @@
#include "common/getline_wrap.h"
#include "config.h" // HAVE_
// FreeBSD POSIX2008 getline
#ifndef _WITH_GETLINE
#define _WITH_GETLINE
#endif
#include <stdio.h> // getline or fgetln
#include <stdlib.h> // free
#include <string.h> // memcpy
char* getline_wrap(FILE *stream, size_t *len)
{
......@@ -35,24 +41,24 @@ char* getline_wrap(FILE *stream, size_t *len)
return buf;
#elif HAVE_FGETLN
buf = fgetln(stream, len);
buf = fgetln(stream, *len);
if (buf == NULL) {
return NULL;
}
if (buf[len - 1] == '\n') {
buf[len - 1] = '\0';
if (buf[*len - 1] == '\n') {
buf[*len - 1] = '\0';
} else {
char *lbuf = NULL;
if ((lbuf = (char *)malloc(len + 1)) == NULL) {
if ((lbuf = (char *)malloc(*len + 1)) == NULL) {
free(buf);
return NULL;
}
memcpy(lbuf, buf, len);
lbuf[len] = '\0';
memcpy(lbuf, buf, *len);
lbuf[*len] = '\0';
free(buf);
buf = lbuf;
}
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment