From: Sami Kerola Date: Sat, 3 Sep 2016 07:02:09 +0000 (+0100) Subject: cleanup: merge two trim functions to one X-Git-Tag: v0.88~26^2~11 X-Git-Url: http://git.ipfire.org/cgi-bin/gitweb.cgi?a=commitdiff_plain;h=0f126b5fb6903af3a79bea898a7cd22d39598b57;p=thirdparty%2Fmtr.git cleanup: merge two trim functions to one As a minor improvement make the trimmed string to stay at same start address, so that trims can be safely done to pointers that are allocated and need to be free'd later on. There is no such case in this code, but it is good idea to write functions the way they will not cause memory issues if/when such use case happens. --- diff --git a/asn.c b/asn.c index 6bc2f17..c6454cf 100644 --- a/asn.c +++ b/asn.c @@ -146,16 +146,6 @@ static char *ipinfo_lookup(const char *domain) { return txt; } -static char* trimsep(char *s) { - int l; - char *p = s; - while (*p == ' ' || *p == ITEMSEP) - *p++ = '\0'; - for (l = strlen(p)-1; p[l] == ' ' || p[l] == ITEMSEP; l--) - p[l] = '\0'; - return p; -} - // originX.asn.cymru.com txtrec: ASN | Route | Country | Registry | Allocated static char* split_txtrec(struct mtr_ctl *ctl, char *txt_rec) { if (!txt_rec) @@ -176,11 +166,11 @@ static char* split_txtrec(struct mtr_ctl *ctl, char *txt_rec) { while ((next = strchr(prev, ITEMSEP)) && (i < ITEMSMAX)) { *next = '\0'; next++; - (*items)[i] = trimsep(prev); + (*items)[i] = trim(prev, ITEMSEP); prev = next; i++; } - (*items)[i] = trimsep(prev); + (*items)[i] = trim(prev, ITEMSEP); if (i < ITEMSMAX) i++; diff --git a/mtr.c b/mtr.c index bd6a18c..534e8ac 100644 --- a/mtr.c +++ b/mtr.c @@ -176,7 +176,7 @@ read_from_file(const char *filename) { } while (fgets(line, sizeof(line), in)) { - char* name = trim(line); + char* name = trim(line, '\0'); append_to_names(name); } diff --git a/mtr.h b/mtr.h index b9d8009..2bd9ab7 100644 --- a/mtr.h +++ b/mtr.h @@ -45,9 +45,6 @@ typedef struct in_addr ip_t; typedef int socklen_t; #endif -extern char * -trim(char * s); - struct mtr_ctl { int MaxPing; float WaitTime; diff --git a/report.c b/report.c index c6f98ac..9f9082f 100644 --- a/report.c +++ b/report.c @@ -437,7 +437,7 @@ extern void csv_close(struct mtr_ctl *ctl, time_t now) #ifdef HAVE_IPINFO if(!ctl->ipinfo_no) { char* fmtinfo = fmt_ipinfo(ctl, addr); - fmtinfo = trim(fmtinfo); + fmtinfo = trim(fmtinfo, '\0'); printf("MTR.%s,%lld,%s,%s,%d,%s,%s", PACKAGE_VERSION, (long long)now, "OK", ctl->Hostname, at+1, name, fmtinfo); } else diff --git a/utils.c b/utils.c index 55fec05..6681038 100644 --- a/utils.c +++ b/utils.c @@ -40,17 +40,31 @@ #include "utils.h" -extern char *trim(char *s) +extern char *trim(char *str, const char c) { - char *p = s; - int l = strlen(p); - - while (isspace(p[l - 1]) && l) - p[--l] = 0; - while (*p && isspace(*p) && l) - ++p, --l; + char *p = str; + size_t len; + + /* left trim */ + while (*p && (isspace(*p) || (c && *p == c))) + p++; + if (str < p) { + len = strlen(str); + memmove(str, p, len + 1); + } - return p; + /* right trim */ + len = strlen(str); + while (len) { + len--; + if (isspace(str[len]) || (c && str[len] == c)) { + continue; + } + len++; + break; + } + str[len] = '\0'; + return str; } /* Parse string, and return positive signed int. */ diff --git a/utils.h b/utils.h index cb06899..62ae239 100644 --- a/utils.h +++ b/utils.h @@ -22,7 +22,7 @@ enum { STRTO_U32INT }; -extern char *trim(char *s); +extern char *trim(char *s, const char c); extern int strtonum_or_err(const char *str, const char *errmesg, const int type); extern float strtofloat_or_err(const char *str, const char *errmesg);