]> git.ipfire.org Git - thirdparty/libbsd.git/commitdiff
Switch fparseln() implementation from fgetln() to getline()
authorGuillem Jover <guillem@hadrons.org>
Sat, 13 Dec 2014 20:28:36 +0000 (21:28 +0100)
committerGuillem Jover <guillem@hadrons.org>
Wed, 23 Sep 2015 05:59:34 +0000 (07:59 +0200)
man/fparseln.3
src/fparseln.c

index 1c215acc99ac4eb9e4d0cfe0f3b8a6e5fea49ec0..9170417f8c8b4ff5f8ec4d0fb1534dd001f8e785 100644 (file)
@@ -126,9 +126,9 @@ is returned.
 The
 .Fn fparseln
 function uses internally
-.Xr fgetln 3 ,
+.Xr getline 3 ,
 so all error conditions that apply to
-.Xr fgetln 3 ,
+.Xr getline 3 ,
 apply to
 .Fn fparseln .
 In addition
@@ -141,7 +141,7 @@ and return
 .Dv NULL
 if it runs out of memory.
 .Sh SEE ALSO
-.Xr fgetln 3
+.Xr getline 3
 .Sh HISTORY
 The
 .Fn fparseln
index 8fbf75cc0c032b4cc655b26f677634d48af89a2a..959df11b3a5ac054ea14eebb39e6f556c75237d0 100644 (file)
@@ -77,7 +77,8 @@ fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
 {
        static const char dstr[3] = { '\\', '\\', '#' };
 
-       size_t  s, len;
+       ssize_t s;
+       size_t len, ptrlen;
        char   *buf;
        char   *ptr, *cp;
        int     cnt;
@@ -87,6 +88,8 @@ fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
 
        len = 0;
        buf = NULL;
+       ptrlen = 0;
+       ptr = NULL;
        cnt = 1;
 
        if (str == NULL)
@@ -97,7 +100,7 @@ fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
        com = str[2];
        /*
         * XXX: it would be cool to be able to specify the newline character,
-        * but unfortunately, fgetln does not let us
+        * getdelim(3) does let us, but supporting it would diverge from BSDs.
         */
        nl  = '\n';
 
@@ -109,7 +112,8 @@ fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
                if (lineno)
                        (*lineno)++;
 
-               if ((ptr = fgetln(fp, &s)) == NULL)
+               s = getline(&ptr, &ptrlen, fp);
+               if (s < 0)
                        break;
 
                if (s && com) {         /* Check and eliminate comments */
@@ -149,6 +153,7 @@ fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
                if ((cp = realloc(buf, len + s + 1)) == NULL) {
                        FUNLOCKFILE(fp);
                        free(buf);
+                       free(ptr);
                        return NULL;
                }
                buf = cp;
@@ -159,6 +164,7 @@ fparseln(FILE *fp, size_t *size, size_t *lineno, const char str[3], int flags)
        }
 
        FUNLOCKFILE(fp);
+       free(ptr);
 
        if ((flags & FPARSELN_UNESCALL) != 0 && esc && buf != NULL &&
            strchr(buf, esc) != NULL) {