From 2792ba2ddfbf7ece7b960faf1ec28c022e9c566e Mon Sep 17 00:00:00 2001 From: Nick White Date: Thu, 21 Jul 2011 22:52:14 +0100 Subject: Separate util.c, use page codes, remove die() --- TODO | 27 ++++++++++++++++++++++++--- config.mk | 10 +++++----- getgbook.c | 20 ++++++++++---------- util.c | 22 ++++++++++++---------- 4 files changed, 51 insertions(+), 28 deletions(-) diff --git a/TODO b/TODO index 2ee5e68..ff35451 100644 --- a/TODO +++ b/TODO @@ -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 diff --git a/config.mk b/config.mk index 145c96d..321a1ec 100644 --- a/config.mk +++ b/config.mk @@ -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) diff --git a/getgbook.c b/getgbook.c index 1442e8e..9e83696 100644 --- a/getgbook.c +++ b/getgbook.c @@ -2,7 +2,7 @@ #include #include #include -#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; } diff --git a/util.c b/util.c index a34dad3..ab6fdf8 100644 --- a/util.c +++ b/util.c @@ -1,14 +1,12 @@ /* See COPYING file for copyright, license and warranty details. */ +#include +#include +#include #include #include #include #include -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 \ -- cgit v1.2.3