gawk

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

commit 97328d55af30493615f5eff43d910462cc46ac8f
parent ad0e4c4b9baa1e4d41afcad5a38c735220cc943b
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Wed, 23 Dec 2020 22:49:51 -0800

Revise networking functions

The major change is error handling in what is now gph_recvto():
- Print a generic error message on timeout or poll error
- Simply return wfclose()

Diffstat:
Mmain.c | 31++++++++++++-------------------
1 file changed, 12 insertions(+), 19 deletions(-)

diff --git a/main.c b/main.c @@ -228,10 +228,10 @@ too_big: } int -gph_request(int sock, const char *request) +gph_send(int sock, const char *message) { - if (send(sock, request, strlen(request), 0) == -1) { - warn("unable to send request"); + if (send(sock, message, strlen(message), 0) == -1) { + warn("unable to send message"); return 1; } if (shutdown(sock, SHUT_WR)) { @@ -242,7 +242,7 @@ gph_request(int sock, const char *request) } int -gph_write_resp(int sock, const char *path) +gph_recvto(int sock, const char *path) { FILE *fp; char buf[CHUNK_SIZE]; @@ -251,12 +251,8 @@ gph_write_resp(int sock, const char *path) pfd.fd = sock; pfd.events = POLLRDNORM; - switch (poll(&pfd, 1, timeout)) { - case -1: - warn("poll"); - return 1; - case 0: - warnx("timeout."); + if (poll(&pfd, 1, timeout) < 1) { + warn("Unable to recieve response"); return 1; } @@ -264,7 +260,6 @@ gph_write_resp(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) != (size_t)bytes) { wfclose(fp); @@ -272,13 +267,11 @@ gph_write_resp(int sock, const char *path) } } - if (wfclose(fp)) - return 1; - return 0; + return wfclose(fp); } int -gph_write(const char **addr, const char *path) +gopher(const char **addr, const char *path) { int sock; @@ -286,8 +279,8 @@ gph_write(const char **addr, const char *path) if (sock == -1) return 1; - if (gph_request(sock, addr[AR_PATH]) != 0 || - gph_write_resp(sock, path) != 0) { + if (gph_send(sock, addr[AR_PATH]) != 0 || + gph_recvto(sock, path) != 0) { close(sock); return 1; } @@ -470,7 +463,7 @@ cfetch(int argc, const char **argv, int i, const char **request) return ERROR; } - if (gph_write(reqtoaddr(request), output) != 0) + if (gopher(reqtoaddr(request), output) != 0) return ERROR; warnx("'%s/%s' written to '%s'", request[GI_HOST], request[GI_PATH], output); return 0; @@ -630,7 +623,7 @@ gawk(const char **addr) mycache = 0; else { mycache = 1; - if (gph_write(addr, cache) == 1) + if (gopher(addr, cache) == 1) return 0; /* let the user handle it */ done = run_filters(cache, NULL, 0, NULL, cprintn); }