From 3e9c75318b0eb1114fb7361dbc467990674804b6 Mon Sep 17 00:00:00 2001 From: wrmr Date: Sun, 3 Nov 2024 02:48:05 -0500 Subject: start working on fetching --- net.c | 44 ++++++++++++++++++++++++++++++++++++++------ 1 file changed, 38 insertions(+), 6 deletions(-) (limited to 'net.c') diff --git a/net.c b/net.c index 73f50d4..e90d3c0 100644 --- a/net.c +++ b/net.c @@ -1,5 +1,8 @@ #include +#include + #include "net.h" +#include "err.h" int net_addr(const char *url, struct addr *adr, enum protocol prot_default) { char *prot_mark; @@ -24,11 +27,40 @@ int net_addr(const char *url, struct addr *adr, enum protocol prot_default) { } 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); + + if (adr->prot == PROT_FILE) { + 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 + 1); + } else { + return -1; + } + return 0; } + +int net_fetch(const struct addr *adr, struct buf *buf) { + switch (adr->prot) { + case PROT_FILE: + { + FILE *f = fopen(adr->path, "r/o"); + if (!f) { + perr("file not found"); + return -1; + } + buf_init(buf, 1024); + char b[256]; + while (fgets(b, sizeof b, f)) { + buf_cat(buf, b, strlen(b)); + } + buf_catc(buf, 0); + fclose(f); + } + return 0; + default: + return -1; + } +} -- cgit 1.4.1-2-gfad0