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)
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++;
}
while (fgets(line, sizeof(line), in)) {
- char* name = trim(line);
+ char* name = trim(line, '\0');
append_to_names(name);
}
typedef int socklen_t;
#endif
-extern char *
-trim(char * s);
-
struct mtr_ctl {
int MaxPing;
float WaitTime;
#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
#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. */
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);