From 2c15efc249bafb84f401edd68eb862a098dd3699 Mon Sep 17 00:00:00 2001 From: Justine Tunney Date: Thu, 18 Feb 2021 19:20:41 -0800 Subject: [PATCH] Add curl example (#42) make -j8 o//examples/curl.com o//examples/curl.com http://justine.lol/ape.html --- examples/curl.c | 107 +++++++++++++++++++++++++++++ examples/examples.mk | 2 + libc/dns/dns.h | 33 +++++++-- libc/dns/eai2str.c | 63 +++++++++++------ libc/dns/getaddrinfo.c | 1 - libc/dns/resolvconf.h | 5 +- libc/sysv/consts.sh | 23 ------- libc/sysv/consts/EAI_ADDRFAMILY.S | 2 - libc/sysv/consts/EAI_AGAIN.S | 2 - libc/sysv/consts/EAI_ALLDONE.S | 2 - libc/sysv/consts/EAI_BADFLAGS.S | 2 - libc/sysv/consts/EAI_CANCELED.S | 2 - libc/sysv/consts/EAI_FAIL.S | 2 - libc/sysv/consts/EAI_FAMILY.S | 2 - libc/sysv/consts/EAI_IDN_ENCODE.S | 2 - libc/sysv/consts/EAI_INPROGRESS.S | 2 - libc/sysv/consts/EAI_INTR.S | 2 - libc/sysv/consts/EAI_MEMORY.S | 2 - libc/sysv/consts/EAI_NODATA.S | 2 - libc/sysv/consts/EAI_NONAME.S | 2 - libc/sysv/consts/EAI_NOTCANCELED.S | 2 - libc/sysv/consts/EAI_OVERFLOW.S | 2 - libc/sysv/consts/EAI_SERVICE.S | 2 - libc/sysv/consts/EAI_SOCKTYPE.S | 2 - libc/sysv/consts/EAI_SUCCESS.S | 2 - libc/sysv/consts/EAI_SYSTEM.S | 2 - libc/sysv/consts/eai.h | 49 ------------- net/http/urislice2cstr.c | 27 +++++--- tool/net/dig.c | 13 +++- 29 files changed, 208 insertions(+), 153 deletions(-) create mode 100644 examples/curl.c delete mode 100644 libc/sysv/consts/EAI_ADDRFAMILY.S delete mode 100644 libc/sysv/consts/EAI_AGAIN.S delete mode 100644 libc/sysv/consts/EAI_ALLDONE.S delete mode 100644 libc/sysv/consts/EAI_BADFLAGS.S delete mode 100644 libc/sysv/consts/EAI_CANCELED.S delete mode 100644 libc/sysv/consts/EAI_FAIL.S delete mode 100644 libc/sysv/consts/EAI_FAMILY.S delete mode 100644 libc/sysv/consts/EAI_IDN_ENCODE.S delete mode 100644 libc/sysv/consts/EAI_INPROGRESS.S delete mode 100644 libc/sysv/consts/EAI_INTR.S delete mode 100644 libc/sysv/consts/EAI_MEMORY.S delete mode 100644 libc/sysv/consts/EAI_NODATA.S delete mode 100644 libc/sysv/consts/EAI_NONAME.S delete mode 100644 libc/sysv/consts/EAI_NOTCANCELED.S delete mode 100644 libc/sysv/consts/EAI_OVERFLOW.S delete mode 100644 libc/sysv/consts/EAI_SERVICE.S delete mode 100644 libc/sysv/consts/EAI_SOCKTYPE.S delete mode 100644 libc/sysv/consts/EAI_SUCCESS.S delete mode 100644 libc/sysv/consts/EAI_SYSTEM.S delete mode 100644 libc/sysv/consts/eai.h diff --git a/examples/curl.c b/examples/curl.c new file mode 100644 index 000000000..0190e99e6 --- /dev/null +++ b/examples/curl.c @@ -0,0 +1,107 @@ +#if 0 +/*─────────────────────────────────────────────────────────────────╗ +│ To the extent possible under law, Justine Tunney has waived │ +│ all copyright and related or neighboring rights to this file, │ +│ as it is written in the following disclaimers: │ +│ • http://unlicense.org/ │ +│ • http://creativecommons.org/publicdomain/zero/1.0/ │ +╚─────────────────────────────────────────────────────────────────*/ +#endif +#include "libc/bits/safemacros.h" +#include "libc/dns/dns.h" +#include "libc/fmt/conv.h" +#include "libc/fmt/fmt.h" +#include "libc/log/check.h" +#include "libc/log/log.h" +#include "libc/macros.h" +#include "libc/runtime/gc.h" +#include "libc/runtime/runtime.h" +#include "libc/sock/sock.h" +#include "libc/stdio/stdio.h" +#include "libc/str/str.h" +#include "libc/sysv/consts/af.h" +#include "libc/sysv/consts/ai.h" +#include "libc/sysv/consts/ipproto.h" +#include "libc/sysv/consts/shut.h" +#include "libc/sysv/consts/sock.h" +#include "libc/x/x.h" +#include "net/http/uri.h" + +/** + * @fileoverview Downloads HTTP URL to stdout. + * + * make -j8 o//examples/curl.com + * o//examples/curl.com http://justine.lol/ape.html + */ + +int main(int argc, char *argv[]) { + int sock; + ssize_t rc; + unsigned long need; + struct UriSlice path; + size_t i, got, toto, msglen; + char buf[1500], host[256], port[7]; + const char *url, *msg, *pathstr, *crlfcrlf, *contentlength; + struct UriSlice us[16]; + struct Uri u = {.segs.p = us, .segs.n = ARRAYLEN(us)}; + struct addrinfo *addr, *addrs; + struct addrinfo hints = {.ai_family = AF_INET, + .ai_socktype = SOCK_STREAM, + .ai_protocol = IPPROTO_TCP, + .ai_flags = AI_NUMERICSERV}; + if (argc != 2) { + fprintf(stderr, "USAGE: %s URL\n", argv[0]); + exit(1); + } + url = argv[1]; + CHECK_NE(-1, uriparse(&u, url, strlen(url)), "BAD URL: %`'s", url); + CHECK_EQ(kUriSchemeHttp, urischeme(u.scheme, url)); + urislice2cstr(host, sizeof(host), u.host, url, "127.0.0.1"); + urislice2cstr(port, sizeof(port), u.port, url, "80"); + path = uripath(&u); + pathstr = path.n ? url + path.i : "/"; + msg = gc(xstrcat("GET ", pathstr, + " HTTP/1.1\r\n" + "Host: ", + host, + "\r\n" + "Connection: close\r\n" + "Content-Length: 0\r\n" + "Accept: text/plain; */*\r\n" + "Accept-Encoding: identity\r\n" + "User-Agent: github.com/jart/cosmopolitan\r\n" + "\r\n")); + msglen = strlen(msg); + CHECK_EQ(EAI_SUCCESS, getaddrinfo(host, port, &hints, &addrs)); + for (addr = addrs; addr; addr = addr->ai_next) { + CHECK_NE(-1, (sock = socket(addr->ai_family, addr->ai_socktype, + addr->ai_protocol))); + CHECK_NE(-1, connect(sock, addr->ai_addr, addr->ai_addrlen)); + CHECK_EQ(msglen, write(sock, msg, msglen)); + shutdown(sock, SHUT_WR); + buf[0] = '\0'; + CHECK_NE(-1, (rc = read(sock, buf, sizeof(buf)))); + got = rc; + CHECK(startswith(buf, "HTTP/1.1 200"), "%`'.*s", got, buf); + CHECK_NOTNULL((crlfcrlf = memmem(buf, got, "\r\n\r\n", 4))); + need = + strtol((char *)firstnonnull( + firstnonnull( + memmem(buf, crlfcrlf - buf, "\r\nContent-Length: ", 18), + memmem(buf, crlfcrlf - buf, "\r\ncontent-length: ", 18)), + "\r\nContent-Length: -1") + + 18, + NULL, 10); + got = MIN(got - (crlfcrlf + 4 - buf), need); + CHECK_EQ(got, write(1, crlfcrlf + 4, got)); + for (toto = got; toto < need; toto += got) { + CHECK_NE(-1, (rc = read(sock, buf, sizeof(buf)))); + if (!(got = rc)) exit(18); + got = MIN(got, need - toto); + CHECK_EQ(got, write(1, buf, got)); + } + LOGIFNEG1(close(sock)); + break; + } + return 0; +} diff --git a/examples/examples.mk b/examples/examples.mk index ef2a8ba51..d5f06fe15 100644 --- a/examples/examples.mk +++ b/examples/examples.mk @@ -40,6 +40,7 @@ EXAMPLES_DIRECTDEPS = \ LIBC_ALG \ LIBC_BITS \ LIBC_CALLS \ + LIBC_DNS \ LIBC_FMT \ LIBC_INTRIN \ LIBC_LOG \ @@ -64,6 +65,7 @@ EXAMPLES_DIRECTDEPS = \ LIBC_UNICODE \ LIBC_X \ LIBC_ZIPOS \ + NET_HTTP \ THIRD_PARTY_COMPILER_RT \ THIRD_PARTY_DLMALLOC \ THIRD_PARTY_GDTOA \ diff --git a/libc/dns/dns.h b/libc/dns/dns.h index ba26503ad..6bc2a285b 100644 --- a/libc/dns/dns.h +++ b/libc/dns/dns.h @@ -1,15 +1,34 @@ #ifndef COSMOPOLITAN_LIBC_DNS_DNS_H_ #define COSMOPOLITAN_LIBC_DNS_DNS_H_ -#if !(__ASSEMBLER__ + __LINKER__ + 0) -COSMOPOLITAN_C_START_ +#include "libc/dns/resolvconf.h" +#include "libc/sock/sock.h" -#define DNS_PORT 53 -#define DNS_NAME_MAX 253 +#define DNS_PORT 53 +#define DNS_NAME_MAX 253 #define DNS_LABEL_MAX 63 -struct sockaddr; -struct sockaddr_in; -struct ResolvConf; +#define EAI_SUCCESS 0 +#define EAI_BADFLAGS -1 +#define EAI_NONAME -2 +#define EAI_AGAIN -3 +#define EAI_FAIL -4 +#define EAI_NODATA -5 +#define EAI_FAMILY -6 +#define EAI_SOCKTYPE -7 +#define EAI_SERVICE -8 +#define EAI_ADDRFAMILY -9 +#define EAI_MEMORY -10 +#define EAI_OVERFLOW -12 +#define EAI_SYSTEM -11 +#define EAI_ALLDONE -103 +#define EAI_CANCELED -101 +#define EAI_IDN_ENCODE -105 +#define EAI_INPROGRESS -100 +#define EAI_INTR -104 +#define EAI_NOTCANCELED -102 + +#if !(__ASSEMBLER__ + __LINKER__ + 0) +COSMOPOLITAN_C_START_ struct addrinfo { int32_t ai_flags; /* AI_XXX */ diff --git a/libc/dns/eai2str.c b/libc/dns/eai2str.c index b4d55c285..b14c3bdc7 100644 --- a/libc/dns/eai2str.c +++ b/libc/dns/eai2str.c @@ -17,30 +17,51 @@ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ #include "libc/dns/dns.h" -#include "libc/sysv/consts/eai.h" /** * Turns getaddrinfo() return code into string. */ const char *eai2str(int code) { - if (code == EAI_ADDRFAMILY) return "ADDRFAMILY"; - if (code == EAI_AGAIN) return "AGAIN"; - if (code == EAI_ALLDONE) return "ALLDONE"; - if (code == EAI_BADFLAGS) return "BADFLAGS"; - if (code == EAI_CANCELED) return "CANCELED"; - if (code == EAI_FAIL) return "FAIL"; - if (code == EAI_FAMILY) return "FAMILY"; - if (code == EAI_IDN_ENCODE) return "ENCODE"; - if (code == EAI_INPROGRESS) return "INPROGRESS"; - if (code == EAI_INTR) return "INTR"; - if (code == EAI_MEMORY) return "MEMORY"; - if (code == EAI_NODATA) return "NODATA"; - if (code == EAI_NONAME) return "NONAME"; - if (code == EAI_NOTCANCELED) return "NOTCANCELED"; - if (code == EAI_OVERFLOW) return "OVERFLOW"; - if (code == EAI_SERVICE) return "SERVICE"; - if (code == EAI_SOCKTYPE) return "SOCKTYPE"; - if (code == EAI_SUCCESS) return "SUCCESS"; - if (code == EAI_SYSTEM) return "SYSTEM"; - return "???"; + switch (code) { + case EAI_ADDRFAMILY: + return "ADDRFAMILY"; + case EAI_AGAIN: + return "AGAIN"; + case EAI_ALLDONE: + return "ALLDONE"; + case EAI_BADFLAGS: + return "BADFLAGS"; + case EAI_CANCELED: + return "CANCELED"; + case EAI_FAIL: + return "FAIL"; + case EAI_FAMILY: + return "FAMILY"; + case EAI_IDN_ENCODE: + return "ENCODE"; + case EAI_INPROGRESS: + return "INPROGRESS"; + case EAI_INTR: + return "INTR"; + case EAI_MEMORY: + return "MEMORY"; + case EAI_NODATA: + return "NODATA"; + case EAI_NONAME: + return "NONAME"; + case EAI_NOTCANCELED: + return "NOTCANCELED"; + case EAI_OVERFLOW: + return "OVERFLOW"; + case EAI_SERVICE: + return "SERVICE"; + case EAI_SOCKTYPE: + return "SOCKTYPE"; + case EAI_SUCCESS: + return "SUCCESS"; + case EAI_SYSTEM: + return "SYSTEM"; + default: + return "???"; + } } diff --git a/libc/dns/getaddrinfo.c b/libc/dns/getaddrinfo.c index cd3e9d1e9..f7b765e7c 100644 --- a/libc/dns/getaddrinfo.c +++ b/libc/dns/getaddrinfo.c @@ -27,7 +27,6 @@ #include "libc/str/str.h" #include "libc/sysv/consts/af.h" #include "libc/sysv/consts/ai.h" -#include "libc/sysv/consts/eai.h" #include "libc/sysv/consts/inaddr.h" #include "libc/sysv/errfuns.h" diff --git a/libc/dns/resolvconf.h b/libc/dns/resolvconf.h index ee2fd3fef..222a7fbc1 100644 --- a/libc/dns/resolvconf.h +++ b/libc/dns/resolvconf.h @@ -1,11 +1,10 @@ #ifndef COSMOPOLITAN_LIBC_DNS_RESOLVCONF_H_ #define COSMOPOLITAN_LIBC_DNS_RESOLVCONF_H_ +#include "libc/sock/sock.h" +#include "libc/stdio/stdio.h" #if !(__ASSEMBLER__ + __LINKER__ + 0) COSMOPOLITAN_C_START_ -struct FILE; -struct sockaddr_in; - struct Nameservers { size_t i, n; struct sockaddr_in *p; diff --git a/libc/sysv/consts.sh b/libc/sysv/consts.sh index bdeea030b..a7a1d152b 100755 --- a/libc/sysv/consts.sh +++ b/libc/sysv/consts.sh @@ -1833,29 +1833,6 @@ syscon gai AI_NUMERICSERV 0x0400 0x1000 8 0x10 0x10 8 syscon gai AI_ALL 0x10 0x0100 0x0100 0 0 0x0100 syscon gai AI_V4MAPPED 8 0x0800 0x0800 0 0 0x0800 -# getaddrinfo() return codes -# -# group name GNU/Systemd XNU's Not UNIX FreeBSD OpenBSD NetBSD XENIX Commentary -syscon eai EAI_SUCCESS 0 0 0 0 0 0 -syscon eai EAI_BADFLAGS -1 3 3 -1 -1 0x2726 -syscon eai EAI_NONAME -2 8 8 -2 -2 0x2af9 -syscon eai EAI_AGAIN -3 2 2 -3 -3 0x2afa -syscon eai EAI_FAIL -4 4 4 -4 -4 0x2afb -syscon eai EAI_FAMILY -6 5 5 -6 -6 0x273f -syscon eai EAI_MEMORY -10 6 6 -10 -10 0x2747 -syscon eai EAI_SERVICE -8 9 9 -8 -8 0x277d -syscon eai EAI_SOCKTYPE -7 10 10 -7 -7 0x273c -syscon eai EAI_NODATA -5 7 0 -5 -5 0x2af9 -syscon eai EAI_OVERFLOW -12 14 14 -14 -14 -12 -syscon eai EAI_SYSTEM -11 11 11 -11 -11 -11 -syscon eai EAI_ADDRFAMILY -9 1 0 -9 -9 -9 -syscon eai EAI_ALLDONE -103 -103 -103 -103 -103 -103 # copying from linux -syscon eai EAI_CANCELED -101 -101 -101 -101 -101 -101 # copying from linux -syscon eai EAI_IDN_ENCODE -105 -105 -105 -105 -105 -105 # copying from linux -syscon eai EAI_INPROGRESS -100 -100 -100 -100 -100 -100 # copying from linux -syscon eai EAI_INTR -104 -104 -104 -104 -104 -104 # copying from linux -syscon eai EAI_NOTCANCELED -102 -102 -102 -102 -102 -102 # copying from linux - syscon misc BLK_BYTECOUNT 2 2 2 2 2 0 # unix consensus syscon misc BLK_EOF 0x40 0x40 0x40 0x40 0x40 0 # unix consensus syscon misc BLK_EOR 0x80 0x80 0x80 0x80 0x80 0 # unix consensus diff --git a/libc/sysv/consts/EAI_ADDRFAMILY.S b/libc/sysv/consts/EAI_ADDRFAMILY.S deleted file mode 100644 index f99569db6..000000000 --- a/libc/sysv/consts/EAI_ADDRFAMILY.S +++ /dev/null @@ -1,2 +0,0 @@ -#include "libc/sysv/consts/syscon.internal.h" -.syscon eai,EAI_ADDRFAMILY,-9,1,0,-9,-9,-9 diff --git a/libc/sysv/consts/EAI_AGAIN.S b/libc/sysv/consts/EAI_AGAIN.S deleted file mode 100644 index c28223271..000000000 --- a/libc/sysv/consts/EAI_AGAIN.S +++ /dev/null @@ -1,2 +0,0 @@ -#include "libc/sysv/consts/syscon.internal.h" -.syscon eai,EAI_AGAIN,-3,2,2,-3,-3,0x2afa diff --git a/libc/sysv/consts/EAI_ALLDONE.S b/libc/sysv/consts/EAI_ALLDONE.S deleted file mode 100644 index 0eaee3613..000000000 --- a/libc/sysv/consts/EAI_ALLDONE.S +++ /dev/null @@ -1,2 +0,0 @@ -#include "libc/sysv/consts/syscon.internal.h" -.syscon eai,EAI_ALLDONE,-103,-103,-103,-103,-103,-103 diff --git a/libc/sysv/consts/EAI_BADFLAGS.S b/libc/sysv/consts/EAI_BADFLAGS.S deleted file mode 100644 index d22c968e8..000000000 --- a/libc/sysv/consts/EAI_BADFLAGS.S +++ /dev/null @@ -1,2 +0,0 @@ -#include "libc/sysv/consts/syscon.internal.h" -.syscon eai,EAI_BADFLAGS,-1,3,3,-1,-1,0x2726 diff --git a/libc/sysv/consts/EAI_CANCELED.S b/libc/sysv/consts/EAI_CANCELED.S deleted file mode 100644 index 94c22acc3..000000000 --- a/libc/sysv/consts/EAI_CANCELED.S +++ /dev/null @@ -1,2 +0,0 @@ -#include "libc/sysv/consts/syscon.internal.h" -.syscon eai,EAI_CANCELED,-101,-101,-101,-101,-101,-101 diff --git a/libc/sysv/consts/EAI_FAIL.S b/libc/sysv/consts/EAI_FAIL.S deleted file mode 100644 index e21044c3a..000000000 --- a/libc/sysv/consts/EAI_FAIL.S +++ /dev/null @@ -1,2 +0,0 @@ -#include "libc/sysv/consts/syscon.internal.h" -.syscon eai,EAI_FAIL,-4,4,4,-4,-4,0x2afb diff --git a/libc/sysv/consts/EAI_FAMILY.S b/libc/sysv/consts/EAI_FAMILY.S deleted file mode 100644 index 2541e7252..000000000 --- a/libc/sysv/consts/EAI_FAMILY.S +++ /dev/null @@ -1,2 +0,0 @@ -#include "libc/sysv/consts/syscon.internal.h" -.syscon eai,EAI_FAMILY,-6,5,5,-6,-6,0x273f diff --git a/libc/sysv/consts/EAI_IDN_ENCODE.S b/libc/sysv/consts/EAI_IDN_ENCODE.S deleted file mode 100644 index 5a833c1d4..000000000 --- a/libc/sysv/consts/EAI_IDN_ENCODE.S +++ /dev/null @@ -1,2 +0,0 @@ -#include "libc/sysv/consts/syscon.internal.h" -.syscon eai,EAI_IDN_ENCODE,-105,-105,-105,-105,-105,-105 diff --git a/libc/sysv/consts/EAI_INPROGRESS.S b/libc/sysv/consts/EAI_INPROGRESS.S deleted file mode 100644 index 2d592cdff..000000000 --- a/libc/sysv/consts/EAI_INPROGRESS.S +++ /dev/null @@ -1,2 +0,0 @@ -#include "libc/sysv/consts/syscon.internal.h" -.syscon eai,EAI_INPROGRESS,-100,-100,-100,-100,-100,-100 diff --git a/libc/sysv/consts/EAI_INTR.S b/libc/sysv/consts/EAI_INTR.S deleted file mode 100644 index 8360f0be7..000000000 --- a/libc/sysv/consts/EAI_INTR.S +++ /dev/null @@ -1,2 +0,0 @@ -#include "libc/sysv/consts/syscon.internal.h" -.syscon eai,EAI_INTR,-104,-104,-104,-104,-104,-104 diff --git a/libc/sysv/consts/EAI_MEMORY.S b/libc/sysv/consts/EAI_MEMORY.S deleted file mode 100644 index c36447f7a..000000000 --- a/libc/sysv/consts/EAI_MEMORY.S +++ /dev/null @@ -1,2 +0,0 @@ -#include "libc/sysv/consts/syscon.internal.h" -.syscon eai,EAI_MEMORY,-10,6,6,-10,-10,0x2747 diff --git a/libc/sysv/consts/EAI_NODATA.S b/libc/sysv/consts/EAI_NODATA.S deleted file mode 100644 index 6bc507712..000000000 --- a/libc/sysv/consts/EAI_NODATA.S +++ /dev/null @@ -1,2 +0,0 @@ -#include "libc/sysv/consts/syscon.internal.h" -.syscon eai,EAI_NODATA,-5,7,0,-5,-5,0x2af9 diff --git a/libc/sysv/consts/EAI_NONAME.S b/libc/sysv/consts/EAI_NONAME.S deleted file mode 100644 index c5fcf8df9..000000000 --- a/libc/sysv/consts/EAI_NONAME.S +++ /dev/null @@ -1,2 +0,0 @@ -#include "libc/sysv/consts/syscon.internal.h" -.syscon eai,EAI_NONAME,-2,8,8,-2,-2,0x2af9 diff --git a/libc/sysv/consts/EAI_NOTCANCELED.S b/libc/sysv/consts/EAI_NOTCANCELED.S deleted file mode 100644 index ad50a520e..000000000 --- a/libc/sysv/consts/EAI_NOTCANCELED.S +++ /dev/null @@ -1,2 +0,0 @@ -#include "libc/sysv/consts/syscon.internal.h" -.syscon eai,EAI_NOTCANCELED,-102,-102,-102,-102,-102,-102 diff --git a/libc/sysv/consts/EAI_OVERFLOW.S b/libc/sysv/consts/EAI_OVERFLOW.S deleted file mode 100644 index c6da5adda..000000000 --- a/libc/sysv/consts/EAI_OVERFLOW.S +++ /dev/null @@ -1,2 +0,0 @@ -#include "libc/sysv/consts/syscon.internal.h" -.syscon eai,EAI_OVERFLOW,-12,14,14,-14,-14,-12 diff --git a/libc/sysv/consts/EAI_SERVICE.S b/libc/sysv/consts/EAI_SERVICE.S deleted file mode 100644 index 4a5db5c5f..000000000 --- a/libc/sysv/consts/EAI_SERVICE.S +++ /dev/null @@ -1,2 +0,0 @@ -#include "libc/sysv/consts/syscon.internal.h" -.syscon eai,EAI_SERVICE,-8,9,9,-8,-8,0x277d diff --git a/libc/sysv/consts/EAI_SOCKTYPE.S b/libc/sysv/consts/EAI_SOCKTYPE.S deleted file mode 100644 index 2e02936a0..000000000 --- a/libc/sysv/consts/EAI_SOCKTYPE.S +++ /dev/null @@ -1,2 +0,0 @@ -#include "libc/sysv/consts/syscon.internal.h" -.syscon eai,EAI_SOCKTYPE,-7,10,10,-7,-7,0x273c diff --git a/libc/sysv/consts/EAI_SUCCESS.S b/libc/sysv/consts/EAI_SUCCESS.S deleted file mode 100644 index b1e97bc00..000000000 --- a/libc/sysv/consts/EAI_SUCCESS.S +++ /dev/null @@ -1,2 +0,0 @@ -#include "libc/sysv/consts/syscon.internal.h" -.syscon eai,EAI_SUCCESS,0,0,0,0,0,0 diff --git a/libc/sysv/consts/EAI_SYSTEM.S b/libc/sysv/consts/EAI_SYSTEM.S deleted file mode 100644 index a78b73213..000000000 --- a/libc/sysv/consts/EAI_SYSTEM.S +++ /dev/null @@ -1,2 +0,0 @@ -#include "libc/sysv/consts/syscon.internal.h" -.syscon eai,EAI_SYSTEM,-11,11,11,-11,-11,-11 diff --git a/libc/sysv/consts/eai.h b/libc/sysv/consts/eai.h deleted file mode 100644 index f062477ad..000000000 --- a/libc/sysv/consts/eai.h +++ /dev/null @@ -1,49 +0,0 @@ -#ifndef COSMOPOLITAN_LIBC_SYSV_CONSTS_EAI_H_ -#define COSMOPOLITAN_LIBC_SYSV_CONSTS_EAI_H_ -#include "libc/runtime/symbolic.h" -#if !(__ASSEMBLER__ + __LINKER__ + 0) -COSMOPOLITAN_C_START_ - -extern const long EAI_ADDRFAMILY; -extern const long EAI_AGAIN; -extern const long EAI_ALLDONE; -extern const long EAI_BADFLAGS; -extern const long EAI_CANCELED; -extern const long EAI_FAIL; -extern const long EAI_FAMILY; -extern const long EAI_IDN_ENCODE; -extern const long EAI_INPROGRESS; -extern const long EAI_INTR; -extern const long EAI_MEMORY; -extern const long EAI_NODATA; -extern const long EAI_NONAME; -extern const long EAI_NOTCANCELED; -extern const long EAI_OVERFLOW; -extern const long EAI_SERVICE; -extern const long EAI_SOCKTYPE; -extern const long EAI_SYSTEM; - -COSMOPOLITAN_C_END_ -#endif /* !(__ASSEMBLER__ + __LINKER__ + 0) */ - -#define EAI_ADDRFAMILY SYMBOLIC(EAI_ADDRFAMILY) -#define EAI_AGAIN SYMBOLIC(EAI_AGAIN) -#define EAI_ALLDONE SYMBOLIC(EAI_ALLDONE) -#define EAI_BADFLAGS SYMBOLIC(EAI_BADFLAGS) -#define EAI_CANCELED SYMBOLIC(EAI_CANCELED) -#define EAI_FAIL SYMBOLIC(EAI_FAIL) -#define EAI_FAMILY SYMBOLIC(EAI_FAMILY) -#define EAI_IDN_ENCODE SYMBOLIC(EAI_IDN_ENCODE) -#define EAI_INPROGRESS SYMBOLIC(EAI_INPROGRESS) -#define EAI_INTR SYMBOLIC(EAI_INTR) -#define EAI_MEMORY SYMBOLIC(EAI_MEMORY) -#define EAI_NODATA SYMBOLIC(EAI_NODATA) -#define EAI_NONAME SYMBOLIC(EAI_NONAME) -#define EAI_NOTCANCELED SYMBOLIC(EAI_NOTCANCELED) -#define EAI_OVERFLOW SYMBOLIC(EAI_OVERFLOW) -#define EAI_SERVICE SYMBOLIC(EAI_SERVICE) -#define EAI_SOCKTYPE SYMBOLIC(EAI_SOCKTYPE) -#define EAI_SUCCESS LITERALLY(0) -#define EAI_SYSTEM SYMBOLIC(EAI_SYSTEM) - -#endif /* COSMOPOLITAN_LIBC_SYSV_CONSTS_EAI_H_ */ diff --git a/net/http/urislice2cstr.c b/net/http/urislice2cstr.c index aab03e792..a3d6e9830 100644 --- a/net/http/urislice2cstr.c +++ b/net/http/urislice2cstr.c @@ -16,19 +16,30 @@ │ TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR │ │ PERFORMANCE OF THIS SOFTWARE. │ ╚─────────────────────────────────────────────────────────────────────────────*/ +#include "libc/macros.h" #include "libc/str/str.h" #include "net/http/uri.h" +/* TODO(jart): Unescape */ + char *urislice2cstr(char *buf, size_t size, struct UriSlice slice, const char *uristr, const char *defaultval) { - /* TODO(jart): Unescape */ - if (slice.n && slice.n + 1 < size) { - memcpy(buf, uristr + slice.i, slice.n); - } else if (defaultval) { - memcpy(buf, defaultval, (slice.n = strlen(defaultval))); - } else { - slice.n = 0; + size_t n; + const char *p; + if (size) { + if (slice.n) { + p = uristr + slice.i; + n = slice.n; + } else if (defaultval) { + p = defaultval; + n = strlen(defaultval); + } else { + p = NULL; + n = 0; + } + n = MIN(n, size - 1); + memcpy(buf, p, n); + buf[n] = '\0'; } - buf[slice.n] = '\0'; return buf; } diff --git a/tool/net/dig.c b/tool/net/dig.c index 50ab9e994..2dc97808b 100644 --- a/tool/net/dig.c +++ b/tool/net/dig.c @@ -31,14 +31,21 @@ #include "tool/decode/lib/socknames.h" void lookup(const char *name) { + int rc; struct addrinfo hints = (struct addrinfo){.ai_family = AF_INET, .ai_socktype = SOCK_STREAM, .ai_protocol = IPPROTO_TCP, .ai_flags = AI_NUMERICSERV}; struct addrinfo *addrs = NULL; - if (getaddrinfo(name, "80", &hints, &addrs) == -1) { - perror("getaddrinfo"); - exit(1); + switch ((rc = getaddrinfo(name, "80", &hints, &addrs))) { + case EAI_SUCCESS: + break; + case EAI_SYSTEM: + perror("getaddrinfo"); + exit(1); + default: + fprintf(stderr, "getaddrinfo failed: %d (EAI_%s)\n", rc, eai2str(rc)); + exit(1); } if (addrs) { for (struct addrinfo *addr = addrs; addr; addr = addr->ai_next) {