#include #include #include #define prefixcmp(haystack, needle) strncmp(haystack, needle, strlen(needle)) #define xcalloc(n, size) calloc(n, size) #define xstrdup(str) strdup(str) static char *anonymize_url(const char *url) { char *anon_url; const char *at_sign = strchr(url, '@'); size_t len, prefix_len = 0; if (!at_sign) return xstrdup(url); if (!prefixcmp(url, "ssh://")) prefix_len = strlen("ssh://"); else if (!prefixcmp(url, "http://")) prefix_len = strlen("http://"); else if (!prefixcmp(url, "https://")) prefix_len = strlen("https://"); else if (!strchr(at_sign + 1, ':')) return xstrdup(url); len = prefix_len + strlen(at_sign + 1); anon_url = xcalloc(1, 1 + prefix_len + strlen(at_sign + 1)); if (prefix_len) memcpy(anon_url, url, prefix_len); memcpy(anon_url + prefix_len, at_sign + 1, strlen(at_sign + 1)); return anon_url; } int main(int argc, char **argv) { int errors = 0; struct { char *raw; char *correct; } urls[] = { { "rsync://host.xz/path/to/repo.git/", NULL, }, { "http://host.xz:port/path/to/repo.git/", NULL, }, { "https://host.xz:port/path/to/repo.git/", NULL,}, { "git://host.xz:port/path/to/repo.git/", NULL, }, { "git://host.xz:port/~user/path/to/repo.git/", NULL, }, { "http://user@host.xz:port/path/to/repo.git/", "http://host.xz:port/path/to/repo.git/", }, { "https://user@host.xz:port/path/to/repo.git/", "https://host.xz:port/path/to/repo.git/", }, { "ssh://user@host.xz:port/path/to/repo.git/", "ssh://host.xz:port/path/to/repo.git/", }, { "ssh://user@host.xz/path/to/repo.git/", "ssh://host.xz/path/to/repo.git/", }, { "ssh://user@host.xz/~user/path/to/repo.git/", "ssh://host.xz/~user/path/to/repo.git/", }, { "user@host.xz:/path/to/repo.git/", "host.xz:/path/to/repo.git/", }, { "user@host.xz:~user/path/to/repo.git/", "host.xz:~user/path/to/repo.git/", }, { NULL, NULL }, }; int i; for (i = 0; urls[i].raw; i++) { char *anon_url = anonymize_url(urls[i].raw); if (!strcmp(anon_url, urls[i].correct ? urls[i].correct : urls[i].raw)) continue; errors++; printf("raw : %s\nanon : %s\n", urls[i].raw, anon_url); printf("correct: %s\n", urls[i].correct); } printf("There were %d errors\n", errors); return 0; }