diff options
| author | user <user@clank> | 2026-07-18 00:38:38 +0200 |
|---|---|---|
| committer | user <user@clank> | 2026-07-18 00:38:38 +0200 |
| commit | ba2d7ae1d7b319645b10aaa31a7f664f9f80c25c (patch) | |
| tree | e01e6c7c710a661defe2515ff9657f08cddcb283 /usr/wget.c | |
| parent | tools: gbtype prints hub errors to stderr and exits 1 (diff) | |
| download | gbos-ba2d7ae1d7b319645b10aaa31a7f664f9f80c25c.tar.gz gbos-ba2d7ae1d7b319645b10aaa31a7f664f9f80c25c.tar.xz gbos-ba2d7ae1d7b319645b10aaa31a7f664f9f80c25c.zip | |
refactor: c/ -> usr/, compiled program blobs out of the source tree
Userland sources live in usr/ (fits the Unix theme better than 'c').
SDCC output .bin blobs land in build/usr/ with the other build artifacts
instead of littering the source dir; programs.asm INCBINs them from there.
Byte-identical ROM.
Diffstat (limited to 'usr/wget.c')
| -rw-r--r-- | usr/wget.c | 43 |
1 files changed, 43 insertions, 0 deletions
diff --git a/usr/wget.c b/usr/wget.c new file mode 100644 index 0000000..aafbae0 --- /dev/null +++ b/usr/wget.c @@ -0,0 +1,43 @@ +#include "resolve.h" +/* wget HOST|IP - fetch http://HOST/ over the kernel TCP socket layer and print + it. Resolves a hostname via DNS first (resolve.h), then connect()/send()/ + recv() over TCP - all the SLIP/IP/UDP/TCP lives in the kernel. */ + +static unsigned char buf[220]; /* first + large so namebuf/dst land arg-safe */ +static unsigned char namebuf[64]; +static unsigned char dst[4]; + +void main(void) { + char *arg, *r; + unsigned char s, n, i; + arg = getargs(); + if (!arg || !*arg) { puts("usage: wget HOST|IP"); nl(); sexit(1); } + for (i = 0; arg[i] && i < 63; i++) namebuf[i] = (unsigned char)arg[i]; + namebuf[i] = 0; + + if (!resolve((char *)namebuf, dst)) { + puts("wget: cannot resolve "); puts((char *)namebuf); nl(); sexit(1); + } + puts("connecting "); for (i = 0; i < 4; i++) { putu(dst[i]); if (i < 3) putc('.'); } nl(); + + s = net_socket(SOCK_TCP); + if (s == 0xFF) { puts("wget: no socket"); nl(); sexit(1); } + net_bind(s, 40001); + if (net_connect(s, dst, 80) == 0xFF) { puts("wget: connect failed"); nl(); net_close(s); sexit(1); } + + /* GET / HTTP/1.0 with the requested host as the Host header */ + r = "GET / HTTP/1.0\r\nHost: "; + n = 0; + while (r[n]) { buf[n] = (unsigned char)r[n]; n++; } + for (i = 0; namebuf[i]; i++) buf[n++] = namebuf[i]; + buf[n++] = '\r'; buf[n++] = '\n'; buf[n++] = '\r'; buf[n++] = '\n'; + net_send(s, buf, n); + + for (;;) { + n = net_recv(s, buf, (unsigned char)sizeof(buf)); + if (n == 0xFF) { nl(); puts("[timeout]"); nl(); break; } + if (n == 0) { nl(); puts("[eof]"); nl(); break; } + for (i = 0; i < n; i++) putc((char)buf[i]); + } + net_close(s); +} |
