changeset 5257:b5780ea3dd70 draft

Move get_line to common to re-use outside of options
author Roy Marples <roy@marples.name>
date Sun, 24 May 2020 05:24:52 +0000
parents 32665ffe1782
children f29e384aa13e
files src/common.c src/common.h src/if-options.c
diffstat 3 files changed, 48 insertions(+), 45 deletions(-) [+]
line wrap: on
line diff
--- a/src/common.c	Fri May 22 10:12:55 2020 +0100
+++ b/src/common.c	Sun May 24 05:24:52 2020 +0000
@@ -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 @@
 	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)
 {
--- a/src/common.h	Fri May 22 10:12:55 2020 +0100
+++ b/src/common.h	Sun May 24 05:24:52 2020 +0000
@@ -150,5 +150,6 @@
 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
--- a/src/if-options.c	Fri May 22 10:12:55 2020 +0100
+++ b/src/if-options.c	Sun May 24 05:24:52 2020 +0000
@@ -2261,51 +2261,6 @@
 		    ~(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)
 {