diff options
author | Nick White <hg@njw.me.uk> | 2011-07-21 22:52:14 +0100 |
---|---|---|
committer | Nick White <hg@njw.me.uk> | 2011-07-21 22:52:14 +0100 |
commit | 2792ba2ddfbf7ece7b960faf1ec28c022e9c566e (patch) | |
tree | 2cfc1f83c7930dd747bdd36e9fe190de8f80f54a | |
parent | 2c87bf5e7c229e7c7c85bb6bc0cae03e989ad388 (diff) |
Separate util.c, use page codes, remove die()
-rw-r--r-- | TODO | 27 | ||||
-rw-r--r-- | config.mk | 10 | ||||
-rw-r--r-- | getgbook.c | 20 | ||||
-rw-r--r-- | util.c | 22 |
4 files changed, 51 insertions, 28 deletions
@@ -1,11 +1,32 @@ -use order to be able to use real page numbers (getpagecode) - to find this advance through click3 letter by letter until either } (none found) or strcmp "order" +make sure i'm checking all lib calls that could fail +make sure all arrays are used within bounds + + +getgbooktxt (different program as it gets from html pages, which getgbook doesn't any more) + +getabook + +getbnbook + +openlibrary.org? # once it is basically working # +try supporting 3xx in get, if it can be done in a few lines + by getting Location line, freeing buf, and returning a new + iteration. + +add https support to get + to be fast and efficient it's best to crank through all the json 1st, filling in an array of page structs as we go this requires slightly fuller json support could consider making a json reading module, ala confoo, to make ad-hoc memory structures from json -to be super fast we could have 2 threads, one filling the pages structs and one consuming them. this would complicate the code rather, though +write helper scripts like trymissing + +write some little tests + +have file extension be determined by file type, rather than assuming png + +think about whether default functionality should be dl all, rather than -a @@ -8,11 +8,11 @@ CFLAGS = -ansi -pedantic -Wall -Wextra -Werror -g -D_POSIX_C_SOURCE=200112L \ -DVERSION=\"$(VERSION)\" # musl static -#CC = musl-gcc -#LDFLAGS = -static #-s +CC = musl-gcc +LDFLAGS = -static #-s # glibc dynamic -CC = cc -LDFLAGS = +#CC = cc +#LDFLAGS = -LD = ld +LD = $(CC) @@ -2,7 +2,7 @@ #include <stdio.h> #include <stdlib.h> #include <string.h> -#include "util.c" +#include "util.h" #define usage "getgbook " VERSION " - a google books downloader\n" \ "usage: getgbook [-p|-a] bookid\n" \ @@ -73,16 +73,18 @@ int main(int argc, char *argv[]) Page *page; if(argc < 2 || argc > 3 || - (argv[1][0]=='-' && ((argv[1][1]!='p' && argv[1][1]!='a') || argc < 3))) - die(usage); + (argv[1][0]=='-' && ((argv[1][1]!='p' && argv[1][1]!='a') || argc < 3))) { + fputs(usage, stdout); + return 1; + } bookid = argv[argc-1]; if(argv[1][0] == '-') { strncpy(code, pagecodes[0], 3); - c = i =0; + c = i = 0; while(++i) { - snprintf(pg, 16, "%s%d", code, i); + snprintf(pg, 15, "%s%d", code, i); if(!(page = getpagedetail(bookid, pg))) { /* no more pages with that code */ strncpy(code, pagecodes[++c], 3); @@ -100,16 +102,14 @@ int main(int argc, char *argv[]) gettofile("books.google.com", page->url, n); printf("Downloaded page %d\n", page->num); } else if(page->num != -1) - printf("%d\n", page->num); + printf("%s %d\n", page->name, page->num); free(page); } } else { - /* todo: find the page based on its order number, rather than using PA%d */ while(fgets(buf, 1024, stdin)) { - sscanf(buf, "%d", &i); - snprintf(pg, 16, "%s%d", "PA", i); + sscanf(buf, "%15s", pg); if(!(page = getpagedetail(bookid, pg)) || !page->url[0]) { - fprintf(stderr, "%d failed\n", i); + fprintf(stderr, "%s failed\n", pg); free(page); continue; } @@ -1,14 +1,12 @@ /* See COPYING file for copyright, license and warranty details. */ +#include <stdlib.h> +#include <stdio.h> +#include <string.h> #include <unistd.h> #include <netdb.h> #include <netinet/in.h> #include <sys/socket.h> -void die(char *msg) { - fputs(msg, stderr); - exit(EXIT_FAILURE); -} - /* plundered from suckless' sic */ static int dial(char *host, char *port) { static struct addrinfo hints; @@ -18,8 +16,10 @@ static int dial(char *host, char *port) { memset(&hints, 0, sizeof hints); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_STREAM; - if(getaddrinfo(host, port, &hints, &res) != 0) - die("error: cannot resolve hostname\n"); + if(getaddrinfo(host, port, &hints, &res) != 0) { + fprintf(stderr, "error: cannot resolve hostname %s\n", host); + return -1; + } for(r = res; r; r = r->ai_next) { if((srv = socket(r->ai_family, r->ai_socktype, r->ai_protocol)) == -1) continue; @@ -28,8 +28,10 @@ static int dial(char *host, char *port) { close(srv); } freeaddrinfo(res); - if(!r) - die("error: cannot connect to host\n"); + if(!r) { + fprintf(stderr, "error: cannot connect to host %s\n", host); + return -1; + } return srv; } @@ -39,7 +41,7 @@ int get(char *host, char *path, char **buf) { char h[1024] = "\0"; FILE *srv; - fd = dial(host, "80"); + if((fd = dial(host, "80")) == -1) return 0; srv = fdopen(fd, "r+"); fprintf(srv, "GET %s HTTP/1.0\r\nUser-Agent: getgbook-"VERSION \ |