summaryrefslogtreecommitdiff
path: root/getgbook.c
diff options
context:
space:
mode:
authorNick White <hg@njw.me.uk>2011-07-16 00:05:43 +0100
committerNick White <hg@njw.me.uk>2011-07-16 00:05:43 +0100
commited86c00598a42b262866af969780d7e84b6fba0a (patch)
treefb44cba5bd485ef1a9a228063240e6b70fb79b48 /getgbook.c
parent9bdbf7b6158887256df70d5f006c46607f192c83 (diff)
Cleanup, and lay out argument code
Diffstat (limited to 'getgbook.c')
-rw-r--r--getgbook.c33
1 files changed, 23 insertions, 10 deletions
diff --git a/getgbook.c b/getgbook.c
index 6f6870f..bfe56ef 100644
--- a/getgbook.c
+++ b/getgbook.c
@@ -7,7 +7,11 @@
#include <string.h>
#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,"<dc:identifier>");
+ if((c = strstr(buf,"<dc:identifier>")) == 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);