From 4341d92d4149f68447d112bde3ed6be79e1c6803 Mon Sep 17 00:00:00 2001 From: Sami Kerola Date: Fri, 9 Nov 2012 23:29:49 +0000 Subject: [PATCH] getdata: skip strings before nth_field analysis This will allow nth_field function to begin from first field, and stop immediately when it ends, which makes function much more simple and quite a bit quicker. Signed-off-by: Sami Kerola --- src/dhcpd-pools.h | 4 ++-- src/getdata.c | 38 ++++++++++---------------------------- 2 files changed, 12 insertions(+), 30 deletions(-) diff --git a/src/dhcpd-pools.h b/src/dhcpd-pools.h index 4237062..5795d7d 100644 --- a/src/dhcpd-pools.h +++ b/src/dhcpd-pools.h @@ -157,8 +157,8 @@ int prepare_memory(void); int parse_leases(void); void parse_config(int, const char *__restrict, struct shared_network_t *__restrict) __attribute__ ((nonnull(2, 3))); -int nth_field(int n, char *__restrict dest, const char *__restrict src) - __attribute__ ((nonnull(2, 3))) +void nth_field(char *__restrict dest, const char *__restrict src) + __attribute__ ((nonnull(1, 2))) #if __GNUC__ > 4 || (__GNUC__ == 4 && __GNUC_MINOR__ >= 3) __attribute__ ((__hot__)) #endif diff --git a/src/getdata.c b/src/getdata.c index beeff89..57bb2b5 100644 --- a/src/getdata.c +++ b/src/getdata.c @@ -112,8 +112,8 @@ int parse_leases(void) } /* It's a lease, save IP */ if (xstrstr(line, "lease", 5)) { - strncpy(ipstring, line, MAXLEN); - nth_field(2, ipstring, ipstring); + memcpy(ipstring, line + 6, 16); + nth_field(ipstring, ipstring); inet_aton(ipstring, &inp); sw_active_lease = 0; continue; @@ -147,7 +147,7 @@ int parse_leases(void) if ((macaddr != NULL) && (sw_active_lease == 1) && (xstrstr(line, " hardware ethernet", 19))) { - nth_field(3, macstring, line); + nth_field(macstring, line + 20); if (macstring) { macstring[17] = '\0'; macaddr_p->ethernet = xstrdup(macstring); @@ -168,36 +168,18 @@ int parse_leases(void) return 0; } -/* Like strcpy but for field which is separated by white spaces. Number of - * first field is 1 and not 0 like C programs should have. Question of - * semantics, send mail to author if this annoys. All performance boosts for - * this function are well come. */ -int nth_field(int n, char *restrict dest, const char *restrict src) +/* Like strcpy but for field which is separated by white spaces. */ +void nth_field(char *restrict dest, const char *restrict src) { - int i, j = 0, wordn = 0, len; - + size_t i, len; len = strlen(src); - for (i = 0; i < len; i++) { - if (isspace(src[i])) { - if (!(wordn < n)) { - dest[j] = '\0'; - break; - } - j = 0; - } else { - if (j == 0) { - wordn++; - if (n + 1 < wordn) - break; - } - if (wordn == n) { - dest[j] = src[i]; - } - j++; + dest[i] = src[i]; + if (src[i] == ' ' || dest[i] == '\0') { + dest[i] = '\0'; + break; } } - return 0; } /* dhcpd.conf interesting words */