commit 34b926de8c9d1dac1890690f52765f55a741e92b
parent 0da64e5161ebe55fccfd1716a81e21e7ed4ef8ac
Author: glenda <glenda@9front.local>
Date: Sat, 14 Aug 2021 01:10:55 +0000
add -h host flag
Diffstat:
1 file changed, 20 insertions(+), 7 deletions(-)
diff --git a/gemfetch.c b/gemfetch.c
@@ -12,11 +12,14 @@ gethost(char *uri)
long length;
sp = strstr(uri, "://");
- if (sp == 0) sp = uri;
+ if (sp == nil) {
+ werrstr("missing scheme:// in '%s'", uri);
+ return nil;
+ }
else sp += 3;
ep = strchr(sp, '/');
- if (ep == 0) ep = sp + strlen(sp);
+ if (ep == nil) ep = sp + strlen(sp);
length = ep - sp;
host = mallocz(sizeof(char) * (length+1), 1);
memcpy(host, sp, length);
@@ -26,7 +29,7 @@ gethost(char *uri)
void
usage(void)
{
- fprint(2, "usage: %s uri\n", argv0);
+ fprint(2, "usage: %s [-h host] uri\n", argv0);
exits("usage");
}
@@ -38,12 +41,22 @@ main(int argc, char **argv)
int fd, tlsfd;
TLSconn conn;
- argv0 = argv[0];
- argv++, argc--;
+ host = nil;
+
+ ARGBEGIN {
+ case 'h':
+ host = EARGF(usage());
+ break;
+ default:
+ usage();
+ } ARGEND
+
if (argc != 1) usage();
uri = argv[0];
- host = gethost(uri);
+ if (host == nil) host = gethost(uri);
+ if (host == nil) sysfatal("%r");
+
fprint(2, "fetching from %s\n", host);
fd = dial(netmkaddr(host, "tcp", "1965"), nil, nil, nil);
if (fd <=0) sysfatal("dial: %r");
@@ -64,7 +77,7 @@ main(int argc, char **argv)
if (buf[t] == '\n') break;
if (n < 0) sysfatal("read: %r");
t += n;
- if (t >= BSIZE - 2) sysfatal("responce header too long");
+ if (t >= 1024 + 2) sysfatal("responce header too long");
}
write(2, buf, t-1);
write(2, "\n", 1);