From: Roy Marples Date: Fri, 7 Feb 2014 21:48:24 +0000 (+0000) Subject: get_line is now static inside of if-options.c and is a simple wrapper around getline(3). X-Git-Tag: v6.3.0~24 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=c1b54b57cba693f51667438fe90f7178937f0801;p=thirdparty%2Fdhcpcd.git get_line is now static inside of if-options.c and is a simple wrapper around getline(3). --- diff --git a/common.c b/common.c index 02ea4ae0..e4e294ca 100644 --- a/common.c +++ b/common.c @@ -62,39 +62,6 @@ 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) diff --git a/common.h b/common.h index cf2d37b2..c8a77b47 100644 --- 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; diff --git a/dhcpcd.c b/dhcpcd.c index 70848ee7..41f13970 100644 --- 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); diff --git a/if-options.c b/if-options.c index 5e0e4be8..95313cd2 100644 --- a/if-options.c +++ b/if-options.c @@ -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);