static void test_strshorten(void) { char s[] = "foobar"; assert_se(strlen(strshorten(s, 6)) == 6); assert_se(strlen(strshorten(s, 12)) == 6); assert_se(strlen(strshorten(s, 2)) == 2); assert_se(strlen(strshorten(s, 0)) == 0); }
char* hostname_cleanup(char *s) { char *p, *d; bool dot; assert(s); for (p = s, d = s, dot = true; *p; p++) { if (*p == '.') { if (dot) continue; *(d++) = '.'; dot = true; } else if (hostname_valid_char(*p)) { *(d++) = *p; dot = false; } } if (dot && d > s) d[-1] = 0; else *d = 0; strshorten(s, HOST_NAME_MAX); return s; }
int shorten_overlong(const char *s, char **ret) { char *h, *p; /* Shorten an overlong name to HOST_NAME_MAX or to the first dot, * whatever comes earlier. */ assert(s); h = strdup(s); if (!h) return -ENOMEM; if (hostname_is_valid(h, false)) { *ret = h; return 0; } p = strchr(h, '.'); if (p) *p = 0; strshorten(h, HOST_NAME_MAX); if (!hostname_is_valid(h, false)) { free(h); return -EDOM; } *ret = h; return 1; }