]> git.ipfire.org Git - thirdparty/mtr.git/commitdiff
cleanup: merge two trim functions to one
authorSami Kerola <kerolasa@iki.fi>
Sat, 3 Sep 2016 07:02:09 +0000 (08:02 +0100)
committerSami Kerola <kerolasa@iki.fi>
Sun, 4 Sep 2016 15:39:36 +0000 (16:39 +0100)
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.

asn.c
mtr.c
mtr.h
report.c
utils.c
utils.h

diff --git a/asn.c b/asn.c
index 6bc2f17fe9c70345fed22174880112df74585f2d..c6454cf445a39778c72c3efe309e95a1ec44f075 100644 (file)
--- 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 bd6a18c92ccf81ad3832f9b773c3d70eeb6c391e..534e8ac351a09f1199dbff04ddb2daee66d10065 100644 (file)
--- 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 b9d8009ae69344f74832ce6d5f671a45887ff08b..2bd9ab7ec1707d30086bd8e6d759a0f0640c2792 100644 (file)
--- 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;
index c6f98ac7e9ff56be351ccde863bb4b7ca9f89924..9f9082f979df5447c0ded21d30ad62babad8e0d9 100644 (file)
--- 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 55fec05fae2cb8333504747644d95f90aa54570d..6681038592e516467f7b6d895cada2dcfc969163 100644 (file)
--- a/utils.c
+++ b/utils.c
 
 #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 cb06899cd9b8a9751bcd34d9ebb5df66c3d6e607..62ae239545820b6d986d37232ca593cdbc63c5cb 100644 (file)
--- 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);