From ed86c00598a42b262866af969780d7e84b6fba0a Mon Sep 17 00:00:00 2001 From: Nick White Date: Sat, 16 Jul 2011 00:05:43 +0100 Subject: Cleanup, and lay out argument code --- TODO | 6 ------ getgbook.c | 33 +++++++++++++++++++++++---------- 2 files changed, 23 insertions(+), 16 deletions(-) diff --git a/TODO b/TODO index 1c19f74..e69de29 100644 --- a/TODO +++ b/TODO @@ -1,6 +0,0 @@ -usage: getgbook [-p|-a] isbn - -p print all available pages - -a get all available pages - otherwise, get pages from stdin - - diff --git a/getgbook.c b/getgbook.c index 6f6870f..bfe56ef 100644 --- a/getgbook.c +++ b/getgbook.c @@ -7,7 +7,11 @@ #include #include "util.c" -#define usage "getgbook isbn" +#define usage "getgbook - a google books downloader\n" \ + "getgbook [-p|-a] isbn\n" \ + " -p print all available pages\n" \ + " -a download all available pages\n" \ + " otherwise, all pages given in stdin will be downloaded" #define hostname "books.google.com" @@ -32,9 +36,10 @@ char *getgbookid(char *isbn) bookid = malloc(sizeof(char *) * BOOKID_LEN); if((buf = get(srv, "books.google.com", url)) == NULL) - fprintf(stderr,"Error downloading page\n"); + return NULL; else { - c = strstr(buf,""); + if((c = strstr(buf,"")) == NULL) + return NULL; strncpy(bookid, c+15, BOOKID_LEN); bookid[BOOKID_LEN] = '\0'; free(buf); @@ -45,19 +50,27 @@ char *getgbookid(char *isbn) int main(int argc, char *argv[]) { - int i; char *bookid, isbn[16]; - FILE *srv; - if(argc != 2) + if(argc < 2 || argc > 3 || !strncmp(argv[1], "-h", 2)) die("usage: " usage "\n"); - strncpy(isbn,argv[1],16); + if(!strncmp(argv[1], "-p", 2)) { + if(argc != 3) die("usage: " usage "\n"); + printf("I'd love to print a list of available pages\n"); + argv++; + } else if(!strncmp(argv[1], "-a", 2)) { + if(argc != 3) die("usage: " usage "\n"); + printf("I'd love to download all available pages\n"); + argv++; + } else { + printf("I'd love to download all pages from stdin\n"); + } - i = dial(hostname, "80"); - srv = fdopen(i, "r+"); + strncpy(isbn,argv[1],16); - bookid = getgbookid(isbn); + if((bookid = getgbookid(isbn)) == NULL) + die("Could not find book\n"); printf("bookid is %s\n", bookid); free(bookid); -- cgit v1.2.3