# HG changeset patch # User Roy Marples # Date 1590297892 0 # Node ID b5780ea3dd70a47a9fd997522d786ec4c83c900a # Parent 32665ffe178291199c9e029ea5c262922ffc61f5 Move get_line to common to re-use outside of options diff -r 32665ffe1782 -r b5780ea3dd70 src/common.c --- 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 #include #include +#include #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) { diff -r 32665ffe1782 -r b5780ea3dd70 src/common.h --- 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 diff -r 32665ffe1782 -r b5780ea3dd70 src/if-options.c --- 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) {