]> git.ipfire.org Git - thirdparty/dhcpcd.git/commitdiff
Move get_line to common to re-use outside of options
authorRoy Marples <roy@marples.name>
Sun, 24 May 2020 05:24:52 +0000 (05:24 +0000)
committerRoy Marples <roy@marples.name>
Sun, 24 May 2020 05:24:52 +0000 (05:24 +0000)
src/common.c
src/common.h
src/if-options.c

index e72a882ef014bf91704cd69d52fe06941138512a..41528f33ceb16f3c3956471842c5c01398091946 100644 (file)
@@ -34,6 +34,7 @@
 #include <fcntl.h>
 #include <stdio.h>
 #include <stdlib.h>
+#include <string.h>
 
 #include "common.h"
 #include "dhcpcd.h"
@@ -158,6 +159,52 @@ filemtime(const char *file, time_t *time)
        return 0;
 }
 
+/* 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. */
+char *
+get_line(char ** __restrict buf, ssize_t * __restrict buflen)
+{
+       char *p, *c;
+       bool quoted;
+
+       do {
+               p = *buf;
+               c = memchr(*buf, '\n', (size_t)*buflen);
+               if (c == NULL) {
+                       c = memchr(*buf, '\0', (size_t)*buflen);
+                       if (c == NULL)
+                               return NULL;
+                       *buflen = c - *buf;
+                       *buf = NULL;
+               } else {
+                       *c++ = '\0';
+                       *buflen -= c - *buf;
+                       *buf = c;
+               }
+               for (; *p == ' ' || *p == '\t'; p++)
+                       ;
+       } while (*p == '\0' || *p == '\n' || *p == '#' || *p == ';');
+
+       /* Strip embedded comments unless in a quoted string or escaped */
+       quoted = false;
+       for (c = p; *c != '\0'; c++) {
+               if (*c == '\\') {
+                       c++; /* escaped */
+                       continue;
+               }
+               if (*c == '"')
+                       quoted = !quoted;
+               else if (*c == '#' && !quoted) {
+                       *c = '\0';
+                       break;
+               }
+       }
+       return p;
+}
+
+
 int
 is_root_local(void)
 {
index b848434aff4f54bdff2480b984fff5c9038cd728..6a68fa1549c317216de956f7c8ff74d6576f128d 100644 (file)
@@ -150,5 +150,6 @@ size_t hwaddr_aton(uint8_t *, const char *);
 ssize_t readfile(const char *, void *, size_t);
 ssize_t writefile(const char *, mode_t, const void *, size_t);
 int filemtime(const char *, time_t *);
+char *get_line(char ** __restrict, ssize_t * __restrict);
 int is_root_local(void);
 #endif
index ca0216ae89f7663e5a85ee17c8769164fc8aaa09..48ec60555212eeed5197ece7a6ab5a9fdf9bac1a 100644 (file)
@@ -2261,51 +2261,6 @@ finish_config(struct if_options *ifo)
                    ~(DHCPCD_IPV6RA_AUTOCONF | DHCPCD_IPV6RA_REQRDNSS);
 }
 
-/* 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, ssize_t * __restrict buflen)
-{
-       char *p, *c;
-       bool quoted;
-
-       do {
-               p = *buf;
-               c = memchr(*buf, '\n', (size_t)*buflen);
-               if (c == NULL) {
-                       c = memchr(*buf, '\0', (size_t)*buflen);
-                       if (c == NULL)
-                               return NULL;
-                       *buflen = c - *buf;
-                       *buf = NULL;
-               } else {
-                       *c++ = '\0';
-                       *buflen -= c - *buf;
-                       *buf = c;
-               }
-               for (; *p == ' ' || *p == '\t'; p++)
-                       ;
-       } while (*p == '\0' || *p == '\n' || *p == '#' || *p == ';');
-
-       /* Strip embedded comments unless in a quoted string or escaped */
-       quoted = false;
-       for (c = p; *c != '\0'; c++) {
-               if (*c == '\\') {
-                       c++; /* escaped */
-                       continue;
-               }
-               if (*c == '"')
-                       quoted = !quoted;
-               else if (*c == '#' && !quoted) {
-                       *c = '\0';
-                       break;
-               }
-       }
-       return p;
-}
-
 struct if_options *
 default_config(struct dhcpcd_ctx *ctx)
 {