commit 69069fe9e1d69ef1a8bb10728fb7ac05da7670af
parent dce88200ff80053a5b68740a5d43eab48a33cbe2
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date: Sun, 20 Dec 2020 14:32:16 -0800
Add getcache() helper function and cleanup
Add tr() function to translate one character to another within a
char * for use in making the cache path.
Remove redundent break statements.
Diffstat:
M | main.c | | | 28 | +++++++++++++--------------- |
1 file changed, 13 insertions(+), 15 deletions(-)
diff --git a/main.c b/main.c
@@ -164,18 +164,20 @@ newpath(char *path, const char *newpath)
}
void
+tr(char *s, int orig, int repl)
+{
+ for (; *s != '\0'; ++s)
+ if (*s == orig)
+ *s = repl;
+}
+
+void
getcache(char *cache, const char *tmpdir, const char *host, const char *port, const char *path)
{
- int i;
char tmp[PATH_MAX];
- for (i = 0; path[i] != '\0'; ++i) {
- if (path[i] == '/')
- tmp[i] = '-';
- else
- tmp[i] = path[i];
- }
- tmp[i] = '\0';
+ strlcpy(tmp, path, PATH_MAX);
+ tr(tmp, '/', '-');
snprintf(cache, PATH_MAX, "%s/%s-%s-%s", tmpdir, host, port, tmp);
}
@@ -305,7 +307,7 @@ gawkat(const char *path)
return 1;
case 0:
if (pipedup(fds[0], STDIN_FILENO, fds) != 0)
- _exit(EXIT_FAILURE);
+ _exit(1);
execvp(*gphfmt, gphfmt);
warn("execvp '%s'", *gphfmt);
_exit(1);
@@ -386,23 +388,19 @@ execute(int command, int argc, const char **argv, int depth, const char *cache,
switch (command) {
case 'q':
if (argc != 0)
- goto too_many_args; /* break would do, but this is more clear */
+ goto too_many_args;
return depth;
- break;
case 'p':
gawkat(cache);
return 0;
- break;
case 'b':
if (argc > 1)
goto too_many_args;
return cback(argc, argv);
- break;
case 'g':
if (argc > 4)
goto too_many_args;
return cgoto(argc, argv, depth, tmpdir, host, path, port);
- break;
default:
warnx("invalid command '%c'", command);
return 0;
@@ -416,12 +414,12 @@ int
gawk(int depth, const char *tmpdir, const char *host, const char *path, const char *port)
{
char *argv[ARGV_MAX];
+ char *start;
char cache[PATH_MAX];
char inbuf[ARG_MAX];
char prompt[64];
int argc;
int done;
- char *start;
int unlinkit;
++depth;