summaryrefslogtreecommitdiff
path: root/getgbook.c
diff options
context:
space:
mode:
authorNick White <git@njw.me.uk>2011-09-12 22:57:57 +0100
committerNick White <git@njw.me.uk>2011-09-12 22:57:57 +0100
commitb8e6013ca1f59dd5f50c7e0ae07161f5c5fee25c (patch)
treef62fb70713e623753c7e6fe06bdddb10d4619f80 /getgbook.c
parent45a96ef428c9808abcc6d4043f149eb6ac825ece (diff)
Only use memory for a page when needed
Diffstat (limited to 'getgbook.c')
-rw-r--r--getgbook.c7
1 files changed, 3 insertions, 4 deletions
diff --git a/getgbook.c b/getgbook.c
index 862d90c..f43abec 100644
--- a/getgbook.c
+++ b/getgbook.c
@@ -45,10 +45,11 @@ int getpagelist()
s+=strlen("_OC_Run({\"page\":[");
for(i=0, p=pages[0];*s; s++) {
- p->url[0] = '\0';
if(*s == ']')
break;
if(!strncmp(s, "\"pid\"", 5)) {
+ p=pages[i++]=malloc(sizeof(**pages));;
+ p->url[0] = '\0';
snprintf(m, STRMAX, "\"%%%d[^\"]\"", STRMAX-1);
sscanf(s+6, m, p->name);
for(;*s; s++) {
@@ -57,7 +58,6 @@ int getpagelist()
if(!strncmp(s, "\"order\"", 7))
sscanf(s+8, "%d,", &(p->num));
}
- p=pages[++i];
}
}
@@ -175,7 +175,6 @@ int main(int argc, char *argv[])
bookid = argv[argc-1];
pages = malloc(sizeof(*pages) * MAXPAGES);
- for(i=0; i<MAXPAGES; i++) pages[i] = malloc(sizeof(**pages));
if(!(totalpages = getpagelist(bookid, pages))) {
fprintf(stderr, "Could not find any pages for %s\n", bookid);
return 1;
@@ -220,7 +219,7 @@ int main(int argc, char *argv[])
}
}
- for(i=0; i<MAXPAGES; i++) free(pages[i]);
+ for(i=0; i<totalpages; i++) free(pages[i]);
free(pages);
return EXIT_SUCCESS;