commit d020696a19792b64fbb997ec8b55df1f9c92320d
parent b562cc7e674b9116daa0d25819bc3154aa3f267d
Author: Jacob R. Edwards <jacobouno@protonmail.com>
Date: Thu, 24 Dec 2020 18:15:35 -0800
Allow multiple terms in selector string
Instead of only replacing the first question mark with a tab replace
the first question mark with a tab and the rest with spaces.
tr() was modified to double as strcpy.
Diffstat:
M | main.c | | | 35 | ++++++++++++++++++++--------------- |
1 file changed, 20 insertions(+), 15 deletions(-)
diff --git a/main.c b/main.c
@@ -152,11 +152,13 @@ strtorange(unsigned int *r, unsigned int min, unsigned int max, const char *s)
}
void
-tr(char *s, int orig, int repl)
+tr(char *r, const char *s, int orig, int repl)
{
- for (; *s != '\0'; ++s)
+ for (; *s != '\0'; ++s, ++r)
if (*s == orig)
- *s = repl;
+ *r = repl;
+ else
+ *r = *s;
}
int
@@ -240,19 +242,23 @@ too_big:
int
gph_send(int sock, const char *message)
{
- int status;
- char *query;
-
- if ((query = strchr(message, '?')))
- *query = '\t';
- status = send(sock, message, strlen(message), 0);
- if (query)
- *query = '?';
- if (status == -1) {
+ char tmp[MY_PATH_MAX];
+ int n;
+
+ n = strcspn(message, "?");
+ if (message[n] == '\0')
+ n = send(sock, message, n, 0);
+ else {
+ strlcpy(tmp, message, n + 1);
+ tr(tmp + n, message + n, '?', ' ');
+ tmp[n] = '\t';
+ n = send(sock, tmp, strlen(tmp), 0);
+ }
+
+ if (n == -1) {
warn("unable to send message");
return 1;
}
-
if (shutdown(sock, SHUT_WR)) {
warn("shutdown");
return 1;
@@ -645,8 +651,7 @@ tmp_mkpath(char *cache, const char **addr)
{
char tmp[MY_PATH_MAX];
- strlcpy(tmp, addr[AR_PATH], sizeof(tmp));
- tr(tmp, '/', '-');
+ tr(tmp, addr[AR_PATH], '/', '-');
snprintf(cache, MY_PATH_MAX, "%s/%s_%s_%s", tmpdir, addr[AR_HOST], addr[AR_PORT], tmp);
}