summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick White <git@njw.me.uk>2011-11-30 18:55:47 +0000
committerNick White <git@njw.me.uk>2011-11-30 18:55:47 +0000
commit750f41033d1e8eb0a0c6ba4401e685b378ac3ae6 (patch)
treed4b8204055524e91cd852d9673d77eccdc406975
parent83adb758f73ebc6a6c09b7dee82e139317b7ad0a (diff)
Use appropriate file extensions when saving
-rw-r--r--TODO2
-rw-r--r--getabook.c1
-rw-r--r--getbnbook.c1
-rw-r--r--getgbook.c1
-rw-r--r--util.c24
-rw-r--r--util.h1
6 files changed, 28 insertions, 2 deletions
diff --git a/TODO b/TODO
index b4bc96c..28295ec 100644
--- a/TODO
+++ b/TODO
@@ -2,8 +2,6 @@ before 1.0: fix http bugs, package for osx & windows
# other todos
-use the correct file extension depending on the image type (for google and amazon the first page is a jpg, all the others are png)
-
bug in get() & post(): if the \r\n\r\n after http headers is cut off between recv buffers
package for osx - https://github.com/kennethreitz/osx-gcc-installer
diff --git a/getabook.c b/getabook.c
index 6ad0ae2..64cb50f 100644
--- a/getabook.c
+++ b/getabook.c
@@ -136,6 +136,7 @@ int getpage(Page *page)
fprintf(stderr, "%d failed\n", page->num);
return 1;
}
+ renameifjpg(path);
printf("%d downloaded\n", page->num);
fflush(stdout);
diff --git a/getbnbook.c b/getbnbook.c
index 76e3e0e..5254f14 100644
--- a/getbnbook.c
+++ b/getbnbook.c
@@ -83,6 +83,7 @@ int getpage(int pagenum)
fprintf(stderr, "%d failed\n", pagenum);
return 1;
}
+ renameifjpg(path);
printf("%d downloaded\n", pagenum);
fflush(stdout);
diff --git a/getgbook.c b/getgbook.c
index f446c9b..f8b77dc 100644
--- a/getgbook.c
+++ b/getgbook.c
@@ -136,6 +136,7 @@ int getpage(Page *page)
fprintf(stderr, "%s failed\n", page->name);
return 1;
}
+ renameifjpg(path);
printf("%d downloaded\n", page->num);
fflush(stdout);
diff --git a/util.c b/util.c
index b1f4c69..b7502f2 100644
--- a/util.c
+++ b/util.c
@@ -161,3 +161,27 @@ int post(char *host, char *path, char *data, char **buf) {
return l;
}
+
+int renameifjpg(char *path) {
+ FILE *f;
+ char *newpath, *c;
+
+ if((f = fopen(path, "rb")) == NULL)
+ return 1;
+
+ if(fgetc(f) == 255) {
+ if((newpath = malloc(strlen(path) + 1)) == NULL)
+ return 1;
+ strncpy(newpath, path, strlen(path));
+ c = strrchr(newpath, '.');
+ strncpy(c+1, "jpg\0", 4);
+
+ if(rename(path, newpath))
+ return 1;
+ free(newpath);
+ }
+
+ fclose(f);
+
+ return 0;
+}
diff --git a/util.h b/util.h
index 235303b..1711765 100644
--- a/util.h
+++ b/util.h
@@ -5,3 +5,4 @@ int dial(char *host, char *port);
int get(char *host, char *path, char *sendcookie, char *savecookie, char **buf);
int gettofile(char *host, char *url, char *sendcookie, char *savecookie, char *savepath);
int post(char *host, char *path, char *data, char **buf);
+int renameifjpg(char *path);