gawk

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

commit 8dc7569cf03b1821d76fc5aee916152536fc995b
parent 13c0d6111bd58f09591d173af6a065d0efb7c3d3
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date:   Mon, 21 Dec 2020 14:28:02 -0800

Revise cfetch()

Refactor is the word I beleive; nothing changed as far as results
but goto was done away with in a case where break works just as
well.

Diffstat:
Mmain.c | 45++++++++++++++++++++++-----------------------
1 file changed, 22 insertions(+), 23 deletions(-)

diff --git a/main.c b/main.c @@ -33,7 +33,6 @@ #define LEN(X) (sizeof(X) / sizeof(*X)) -#define FETCH_MESSAGE #define BUFSIZE 4096 #define ARGV_MAX 16 #define ARG_SEP ":\t " @@ -437,7 +436,7 @@ cfetch(int argc, const char **argv, const char *cache, const char *host, const c char *line; const char *output; int error; - int i, index; + int i, n; size_t size; if (argc == 0) { @@ -446,9 +445,10 @@ cfetch(int argc, const char **argv, const char *cache, const char *host, const c } wunused(2, argc, argv); - index = strtorange(0, INT_MAX, argv[0]); - if (index < 0) + n = strtorange(0, INT_MAX, argv[0]); + if (n < 0) return 0; + if (argv[1]) output = argv[1]; else output = NULL; @@ -460,28 +460,27 @@ cfetch(int argc, const char **argv, const char *cache, const char *host, const c error = 1; line = NULL; size = 0; - for (i = 0; getline(&line, &size, fp) != -1; ++i) { - if (i != index) - continue; - if (argsplit(fields, LEN(fields), line, "\t\r\n") != LEN(fields) - 1) { - warnx("Not a valid gopher item."); - goto cleanup; - } - if (output == NULL) - if ((output = basename(fields[GI_PATH])) == NULL) { - warn("unable to get basename '%s'", fields[GI_PATH]); - goto cleanup; + for (i = 0; i <= n && getline(&line, &size, fp) != -1; ++i) { + if (i == n) { + if (argsplit(fields, LEN(fields), line, "\t\r\n") != LEN(fields) - 1) { + warnx("Not a valid gopher item."); + break; } - if (selwrite(fields[GI_HOST], fields[GI_PORT], fields[GI_PATH], output) == 0) - error = 0; - goto cleanup; + if (output == NULL) + if ((output = basename(fields[GI_PATH])) == NULL) { + warn("unable to get basename '%s'", fields[GI_PATH]); + break; + } + if (selwrite(fields[GI_HOST], fields[GI_PORT], fields[GI_PATH], output) == 0) + error = 0; + } } - warnx("%d: Out of range.", index); -cleanup: -#ifdef FETCH_MESSAGE - if (!error) + + if (error && n > i) + warnx("%d: Out of range.", n); + else if (!error) warnx("'%s/%s' written to '%s'", fields[GI_HOST], fields[GI_PATH], output); -#endif + free(line); if (wfclose(fp) || error) return 1;