From: Roy Marples Date: Sun, 24 May 2020 05:24:52 +0000 (+0000) Subject: Move get_line to common to re-use outside of options X-Git-Tag: v9.1.0~30 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=c572835e98da5a186f7453f3daa2aec765ef2d9b;p=thirdparty%2Fdhcpcd.git Move get_line to common to re-use outside of options --- diff --git a/src/common.c b/src/common.c index e72a882e..41528f33 100644 --- a/src/common.c +++ b/src/common.c @@ -34,6 +34,7 @@ #include #include #include +#include #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) { diff --git a/src/common.h b/src/common.h index b848434a..6a68fa15 100644 --- a/src/common.h +++ b/src/common.h @@ -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 diff --git a/src/if-options.c b/src/if-options.c index ca0216ae..48ec6055 100644 --- a/src/if-options.c +++ b/src/if-options.c @@ -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) {