]> git.ipfire.org Git - thirdparty/mlmmj.git/commitdiff
ctrlval: use getline/getdelim
authorBaptiste Daroussin <bapt@FreeBSD.org>
Mon, 24 Oct 2022 15:46:18 +0000 (17:46 +0200)
committerBaptiste Daroussin <bapt@FreeBSD.org>
Mon, 24 Oct 2022 15:46:18 +0000 (17:46 +0200)
src/ctrlvalue.c

index 4893a4ddb76f59c3ab698d7858a501502618cc1a..045deca7613af12265bf45c2154818b3fd1753ea 100644 (file)
@@ -23,7 +23,9 @@
 
 #include <sys/types.h>
 #include <sys/stat.h>
+
 #include <stdlib.h>
+#include <stdio.h>
 #include <fcntl.h>
 #include <unistd.h>
 #include <string.h>
 #include "ctrlvalue.h"
 #include "mygetline.h"
 #include "chomp.h"
-#include "memory.h"
 #include "utils.h"
 #include "log_error.h"
+#include "xmalloc.h"
 
 static char *ctrlval(const char *listdir, const char *subdir,
                const char *ctrlstr, int oneline)
 {
-       char *filename, *value = NULL;
-       int ctrlfd, i;
+       char *filename, *buf = NULL;
+       int ctrlfd;
+       size_t bufcap = 0;
+       ssize_t buflen;
+       FILE *fp;
 
        if(listdir == NULL)
                return NULL;
 
-       filename = concatstr(5, listdir, "/", subdir, "/", ctrlstr);
-       ctrlfd = open(filename, O_RDONLY);
+       xasprintf(&filename, "%s/%s/%s", listdir, subdir, ctrlstr);
+       ctrlfd = open(filename, O_RDONLY|O_CLOEXEC);
        free(filename);
 
        if(ctrlfd < 0)
                return NULL;
 
+       fp = fdopen(ctrlfd, "r");
+       if (fp == NULL) {
+               close(ctrlfd);
+               return (NULL);
+       }
+
        if (oneline) {
-               value = mygetline(ctrlfd);
-               chomp(value);
+               buflen = getline(&buf, &bufcap, fp);
+               if (buflen > 0)
+                       chomp(buf);
        } else {
-               value = mygetcontent(ctrlfd);
-               i = strlen(value) - 1;
-               if (i >= 0 && value[i] == '\n') {
-                       value[i] = '\0';
-                       i--;
-               }
-               if (i >= 0 && value[i] == '\r') {
-                       value[i] = '\0';
-                       i--;
-               }
+               buflen = getdelim(&buf, &bufcap, EOF, fp);
        }
-       close(ctrlfd);
+       fclose(fp);
 
-       return value;
+       return (buf);
 }
 
 char *ctrlvalue(const char *listdir, const char *ctrlstr)