]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
get_line is now static inside of if-options.c and is a simple wrapper around getline(3).
authorRoy Marples <roy@marples.name>
Fri, 7 Feb 2014 21:48:24 +0000 (21:48 +0000)
committerRoy Marples <roy@marples.name>
Fri, 7 Feb 2014 21:48:24 +0000 (21:48 +0000)
common.c
common.h
dhcpcd.c
if-options.c

index 02ea4ae0a502a2245c783f7c3e6d7052a0f4f16c..e4e294cae64e78dcfa0e0b88df80e4c140ea44cc 100644 (file)
--- a/common.c
+++ b/common.c
 
 static char hostname_buffer[HOSTNAME_MAX_LEN + 1];
 int clock_monotonic;
-static char *lbuf;
-static size_t lbuf_len;
-
-/* Handy routine to read very long lines in text files.
- * This means we read the whole line and avoid any nasty buffer overflows.
- * We strip leading space and avoid comment lines, making the code that calls
- * us smaller.
- * As we don't use threads, this API is clean too. */
-char *
-get_line(FILE * __restrict fp)
-{
-       char *p;
-       ssize_t bytes;
-
-       do {
-               bytes = getline(&lbuf, &lbuf_len, fp);
-               if (bytes == -1)
-                       return NULL;
-               for (p = lbuf; *p == ' ' || *p == '\t'; p++)
-                       ;
-       } while (*p == '\0' || *p == '\n' || *p == '#' || *p == ';');
-       if (lbuf[--bytes] == '\n')
-               lbuf[bytes] = '\0';
-       return p;
-}
-
-void
-get_line_free(void)
-{
-
-       free(lbuf);
-       lbuf_len = 0;
-}
 
 const char *
 get_hostname(int short_hostname)
index cf2d37b2bd583b6e0896535033f5d54305bff117..c8a77b47d9769bf786dc1069251b978a239aa244 100644 (file)
--- a/common.h
+++ b/common.h
@@ -99,7 +99,6 @@
 # endif
 #endif
 
-char *get_line(FILE * __restrict);
 void get_line_free(void);
 const char *get_hostname(int);
 extern int clock_monotonic;
index 70848ee7dd1ca8e954099283b0ccfbed3255e2fc..41f13970aef01ba141561a0d23bc05e3bef7cb0f 100644 (file)
--- a/dhcpcd.c
+++ b/dhcpcd.c
@@ -1455,7 +1455,6 @@ exit1:
        restore_kernel_ra();
        ipv4_free(NULL);
        ipv6_free(NULL);
-       get_line_free();
        dev_stop(options & DHCPCD_DAEMONISED);
        if (linkfd != -1) {
                close(linkfd);
index 5e0e4be804e2911cf495f67b5831bc30f36b910f..95313cd23cac725442958233dc200906fbcb7078 100644 (file)
@@ -1759,18 +1759,40 @@ finish_config(struct if_options *ifo)
        }
 }
 
+/* Handy routine to read very long lines in text files.
+ * This means we read the whole line and avoid any nasty buffer overflows.
+ * We strip leading space and avoid comment lines, making the code that calls
+ * us smaller. */
+static char *
+get_line(char ** __restrict buf, size_t * __restrict buflen, FILE * __restrict fp)
+{
+       char *p;
+       ssize_t bytes;
+
+       do {
+               bytes = getline(buf, buflen, fp);
+               if (bytes == -1)
+                       return NULL;
+               for (p = *buf; *p == ' ' || *p == '\t'; p++)
+                       ;
+       } while (*p == '\0' || *p == '\n' || *p == '#' || *p == ';');
+       if ((*buf)[--bytes] == '\n')
+               (*buf)[bytes] = '\0';
+       return p;
+}
+
 struct if_options *
 read_config(const char *file,
     const char *ifname, const char *ssid, const char *profile)
 {
        struct if_options *ifo;
-       FILE *f;
-       char *line, *option, *p;
+       FILE *fp;
+       char *line, *buf, *option, *p;
+       size_t buflen;
        int skip = 0, have_profile = 0;
 #ifndef EMBEDDED_CONFIG
-       char *buf;
        const char * const *e;
-       size_t buflen, ol;
+       size_t ol;
 #endif
 #if !defined(INET) || !defined(INET6)
        size_t i;
@@ -1806,6 +1828,9 @@ read_config(const char *file,
        ifo->vendorclassid[0] = strlen(vendor);
        memcpy(ifo->vendorclassid + 1, vendor, ifo->vendorclassid[0]);
 
+       buf = NULL;
+       buflen = 0;
+
        /* Parse our embedded options file */
        if (ifname == NULL) {
                /* Space for initial estimates */
@@ -1829,11 +1854,11 @@ read_config(const char *file,
 
                /* Now load our embedded config */
 #ifdef EMBEDDED_CONFIG
-               f = fopen(EMBEDDED_CONFIG, "r");
-               if (f == NULL)
+               fp = fopen(EMBEDDED_CONFIG, "r");
+               if (fp == NULL)
                        syslog(LOG_ERR, "fopen `%s': %m", EMBEDDED_CONFIG);
 
-               while (f && (line = get_line(f))) {
+               while (fp && (line = get_line(&buf, &buflen, fp))) {
 #else
                buflen = 80;
                buf = malloc(buflen);
@@ -1871,10 +1896,8 @@ read_config(const char *file,
                }
 
 #ifdef EMBEDDED_CONFIG
-               if (f)
-                       fclose(f);
-#else
-               free(buf);
+               if (fp)
+                       fclose(fp);
 #endif
 #ifdef INET
                dhcp_opts = ifo->dhcp_override;
@@ -1909,14 +1932,15 @@ read_config(const char *file,
        }
 
        /* Parse our options file */
-       f = fopen(file ? file : CONFIG, "r");
-       if (f == NULL) {
+       fp = fopen(file ? file : CONFIG, "r");
+       if (fp == NULL) {
                if (file != NULL)
                        syslog(LOG_ERR, "fopen `%s': %m", file);
+               free(buf);
                return ifo;
        }
 
-       while ((line = get_line(f))) {
+       while ((line = get_line(&buf, &buflen, fp))) {
                option = strsep(&line, " \t");
                if (line)
                        line = strskipwhite(line);
@@ -1957,7 +1981,8 @@ read_config(const char *file,
                        continue;
                parse_config_line(ifname, ifo, option, line);
        }
-       fclose(f);
+       fclose(fp);
+       free(buf);
 
        if (profile && !have_profile) {
                free_options(ifo);