#include <stdint.h>
#include <string.h>
#include <unistd.h>
+#include <fcntl.h>
#include <errno.h>
#include <sys/socket.h>
#include <netinet/in.h>
int xread(void)
{
- int len = read(sock, buf, 512);
+ int len;
+
+ /* The first call we return the stray bytes from the line parser */
+ if (bufend != buf && bufend > readp) {
+ len = bufend - readp;
+ memcpy(buf, readp, bufend - readp);
+ bufend = buf;
+ readp = buf;
+ return len;
+ }
+ len = read(sock, buf, 512);
if (len < 0) {
perror("read");
exit(1);
uint16_t port = 80;
char *pp;
char *fp;
- int of = 1;
+ int of;
int code;
int len;
uint8_t looped = 0;
- if (argc != 2) {
- writes(2, "htget url\n");
+ if (argc != 3) {
+ writes(2, "htget url file\n");
exit(1);
}
if (strncmp(argv[1], "http://", 7)) {
exit(2);
}
argv[1] += 7;
+
+ fp = strchr(argv[1], '/');
+ if (fp)
+ *fp++ = 0;
+
pp = strrchr(argv[1], ':');
if (pp) {
*pp++ = 0;
}
}
- fp = strchr(argv[1], '/');
- if (fp)
- *fp = 0;
sin.sin_family = AF_INET;
sin.sin_port = htons(port);
if (code == 100)
xreadline();
} while (code == 100 && !looped++);
+
+ of = open(argv[2], O_WRONLY|O_CREAT, 0666);
+ if (of == -1) {
+ perror(argv[2]);
+ exit(1);
+ }
/* FIXME: if we saw a Transfer-Encoding: chunked" we need to do this
bit differently */
if (code == 200) {
perror("write");
exit(1);
}
+ write(1,".",1);
}
}
+ write(1,"\n",1);
+ close(of);
+ close(sock);
return 0;
}
-
-
-