gawk

[old] Sed-like interface to the Gopher protocol
Log | Files | Refs | LICENSE

commit 40fd5d995c059ac0b2c97b6cdf9ac509b3b6da08
parent adf079f494619d02092e5adcfe6d855c345f4af7
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Mon, 21 Dec 2020 19:41:18 -0800

Rename gopher network functions and fix minor things

Gopher network functions renamed, each with a prefix of `gph_`.

In strtorange there is no reason to set againt 0 since `min` does
that already.

In gph_request treat shutdown failure as an error worthy of reporting.

Diffstat:
Mmain.c | 25++++++++++++++-----------
1 file changed, 14 insertions(+), 11 deletions(-)

diff --git a/main.c b/main.c @@ -117,7 +117,7 @@ strtorange(unsigned int *r, unsigned int min, unsigned int max, const char *s) warnc(EINVAL, "not a number '%s'", s); return -1; } else if ((errno == ERANGE && n == ULONG_MAX) || - n < min || n > max || n > UINT_MAX || n < 0) { + n < min || n > max || n > UINT_MAX) { warnc(ERANGE, "'%s'", s); return -1; } @@ -267,19 +267,21 @@ too_big: } int -gphsend(int sock, const char *request) +gph_request(int sock, const char *request) { if (send(sock, request, strlen(request), 0) == -1) { warn("unable to send request"); return 1; } - if (shutdown(sock, SHUT_WR)) + if (shutdown(sock, SHUT_WR)) { warn("shutdown"); + return 1; + } return 0; } int -fetch(int sock, const char *path) +gph_write_resp(int sock, const char *path) { FILE *fp; char buf[BUFSIZE]; @@ -301,6 +303,7 @@ fetch(int sock, const char *path) if (fp == NULL) return 1; + /* TODO: exclude ".\r\n" terminator */ while ((bytes = recv(sock, buf, sizeof(buf), 0)) > 0) { if (fwrite(buf, 1, bytes, fp) != bytes) { wfclose(fp); @@ -314,7 +317,7 @@ fetch(int sock, const char *path) } int -selwrite(const char *host, const char *port, const char *selector, const char *file) +gph_write(const char *host, const char *port, const char *request, const char *path) { int sock; @@ -322,8 +325,8 @@ selwrite(const char *host, const char *port, const char *selector, const char *f if (sock == -1) return 1; - if (gphsend(sock, selector) != 0 || - fetch(sock, file) != 0) { + if (gph_request(sock, request) != 0 || + gph_write_resp(sock, path) != 0) { close(sock); return 1; } @@ -486,7 +489,7 @@ getfromline(char *line, const char *output) warn("unable to get basename '%s'", fields[GI_PATH]); return 1; } - if (selwrite(fields[GI_HOST], fields[GI_PORT], fields[GI_PATH], output) != 0) + if (gph_write(fields[GI_HOST], fields[GI_PORT], fields[GI_PATH], output) != 0) return 1; warnx("'%s/%s' written to '%s'", fields[GI_HOST], fields[GI_PATH], output); return 0; @@ -590,7 +593,7 @@ gawk(const char *host, const char *path, const char *port) mycache = 0; else { mycache = 1; - if (selwrite(host, port, path, cache) == 1) + if (gph_write(host, port, path, cache) == 1) return 0; /* let the user handle it */ } @@ -617,11 +620,11 @@ gawk(const char *host, const char *path, const char *port) if (mycache && unlink(cache) == -1) warn("unable to unlink '%s'", cache); + if (ferror(stdin)) { - warn("'stdin'"); + warn("'%s'", "/dev/stdin"); return -1; } - if (feof(stdin)) done = depth; return done - 1;