summaryrefslogtreecommitdiff
path: root/getabook.c
diff options
context:
space:
mode:
Diffstat (limited to 'getabook.c')
-rw-r--r--getabook.c18
1 files changed, 14 insertions, 4 deletions
diff --git a/getabook.c b/getabook.c
index 098a5c3..9f4015b 100644
--- a/getabook.c
+++ b/getabook.c
@@ -2,6 +2,9 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
+#include <sys/stat.h>
+#include <sys/types.h>
+#include <dirent.h>
#include "util.h"
#define usage "getabook " VERSION " - an amazon look inside the book downloader\n" \
@@ -20,7 +23,8 @@ typedef struct {
Page **pages;
int numpages;
-char *bookid;
+char bookid[STRMAX];
+char *bookdir;
int fillurls(char *buf) {
char m[STRMAX];
@@ -116,7 +120,7 @@ int getpageurls(int pagenum) {
int getpage(Page *page)
{
char path[STRMAX];
- snprintf(path, STRMAX, "%04d.png", page->num);
+ snprintf(path, STRMAX, "%s/%04d.png", bookdir, page->num);
if(page->url[0] == '\0') {
fprintf(stderr, "%d not found\n", page->num);
@@ -147,7 +151,8 @@ int main(int argc, char *argv[])
return 1;
}
- bookid = argv[argc-1];
+ strncpy(bookid, argv[argc-1], STRMAX);
+ bookdir = argv[argc-1];
pages = malloc(sizeof(*pages) * MAXPAGES);
if(getpagelist(bookid, pages)) {
@@ -155,9 +160,14 @@ int main(int argc, char *argv[])
return 1;
}
+ if(!(opendir(bookdir) || !mkdir(bookdir, S_IRWXU))) {
+ fprintf(stderr, "Could not create directory %s\n", bookdir);
+ return 1;
+ }
+
if(argc == 2) {
for(i=0; i<numpages; i++) {
- snprintf(pgpath, STRMAX, "%04d.png", pages[i]->num);
+ snprintf(pgpath, STRMAX, "%s/%04d.png", bookdir, pages[i]->num);
if((f = fopen(pgpath, "r")) != NULL) {
fclose(f);
continue;