#include #include "net.h" int net_addr(const char *url, struct addr *adr, enum protocol prot_default) { char *prot_mark; if ((prot_mark = strstr(url, "://"))) { static struct { const char *str; enum protocol prot; } prot_str_tbl[] = { { "gopher", PROT_GOPHER }, { "gemini", PROT_GEMINI }, { "file", PROT_FILE }, }; size_t n = prot_mark - url; adr->prot = PROT_UNKNOWN; for (size_t i = 0; i < sizeof prot_str_tbl / sizeof *prot_str_tbl; i++) { if (!strncmp(prot_str_tbl[i].str, url, n)) { adr->prot = prot_str_tbl[i].prot; break; } } url = prot_mark + 3; } else { adr->prot = prot_default; } adr->type = TYPE_PLAIN; adr->host_len = 0; size_t n = strlen(url); if (n > PATH_MAX) return -1; adr->path_len = n; memcpy(adr->path, url, n); return 0; }