summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorNick White <hg@njw.me.uk>2011-07-15 22:50:16 +0100
committerNick White <hg@njw.me.uk>2011-07-15 22:50:16 +0100
commit40199b38e08b550bdc27062c78e8cbe38a5e7c80 (patch)
treef975e213321691ea0a45f520a98b532391e3195c
parent14f2069f02e86c54209e03f284aebd81ce8b85e4 (diff)
Separate into util.c again, so can use common functions in different book getters
-rw-r--r--TODO6
-rw-r--r--getgbook.c74
-rw-r--r--util.c69
3 files changed, 80 insertions, 69 deletions
diff --git a/TODO b/TODO
new file mode 100644
index 0000000..1c19f74
--- /dev/null
+++ b/TODO
@@ -0,0 +1,6 @@
+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 6cf75df..7858008 100644
--- a/getgbook.c
+++ b/getgbook.c
@@ -1,79 +1,15 @@
-#define VERSION "prealpha"
-
-#define usage "getgbook bookid"
+/* See COPYING file for copyright, license and warranty details. */
-#define hostname "books.google.com"
+#define VERSION "prealpha"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
-#include <netdb.h>
-#include <netinet/in.h>
-#include <sys/socket.h>
-
-void die(char *msg) {
- fputs(msg, stderr);
- exit(EXIT_FAILURE);
-}
-
-/* plundered from suckless' sic */
-static int dial(char *host, char *port) {
- static struct addrinfo hints;
- int srv;
- struct addrinfo *res, *r;
-
- memset(&hints, 0, sizeof hints);
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM;
- if(getaddrinfo(host, port, &hints, &res) != 0)
- die("error: cannot resolve hostname\n");
- for(r = res; r; r = r->ai_next) {
- if((srv = socket(r->ai_family, r->ai_socktype, r->ai_protocol)) == -1)
- continue;
- if(connect(srv, r->ai_addr, r->ai_addrlen) == 0)
- break;
- close(srv);
- }
- freeaddrinfo(res);
- if(!r)
- die("error: cannot connect to host\n");
- return srv;
-}
+#include "util.c"
-char *get(FILE *srv, char *host, char *path) {
- size_t l, res;
- int fd, i;
- char *buf, *c, *p;
-
- fprintf(srv, "GET %s HTTP/1.0\r\nUser-Agent: getgbook-"VERSION" (not mozilla)\r\nHost: %s\r\n\r\n", path, host);
-
- fflush(srv);
-
- l=0;
- fd = fileno(srv);
-
- buf = malloc(sizeof(char *) * 4096);
- for(i=0; (res = read(fd, buf+l, 4096)) > 0; l+=res, i++)
- buf = realloc(buf, sizeof(char *) * (l+4096));
-
- /* check that it's a 200 */
- if(strncmp(buf+9, "200 ", 4)) {
- free(buf);
- return NULL;
- }
-
- /* exclude header */
- for(p = buf; *p && *(p+1) && *(p+2) && *(p+3); p++)
- if(!strncmp(p, "\r\n\r\n", 4)) break;
- p+=4;
-
- i = l - (p - buf);
- c = malloc(i);
- memcpy(c, p, i);
- free(buf);
+#define usage "getgbook bookid"
- return c;
-}
+#define hostname "books.google.com"
int main(int argc, char *argv[])
{
diff --git a/util.c b/util.c
new file mode 100644
index 0000000..427833b
--- /dev/null
+++ b/util.c
@@ -0,0 +1,69 @@
+/* See COPYING file for copyright, license and warranty details. */
+
+#include <netdb.h>
+#include <netinet/in.h>
+#include <sys/socket.h>
+
+void die(char *msg) {
+ fputs(msg, stderr);
+ exit(EXIT_FAILURE);
+}
+
+/* plundered from suckless' sic */
+static int dial(char *host, char *port) {
+ static struct addrinfo hints;
+ int srv;
+ struct addrinfo *res, *r;
+
+ memset(&hints, 0, sizeof hints);
+ hints.ai_family = AF_UNSPEC;
+ hints.ai_socktype = SOCK_STREAM;
+ if(getaddrinfo(host, port, &hints, &res) != 0)
+ die("error: cannot resolve hostname\n");
+ for(r = res; r; r = r->ai_next) {
+ if((srv = socket(r->ai_family, r->ai_socktype, r->ai_protocol)) == -1)
+ continue;
+ if(connect(srv, r->ai_addr, r->ai_addrlen) == 0)
+ break;
+ close(srv);
+ }
+ freeaddrinfo(res);
+ if(!r)
+ die("error: cannot connect to host\n");
+ return srv;
+}
+
+char *get(FILE *srv, char *host, char *path) {
+ size_t l, res;
+ int fd, i;
+ char *buf, *c, *p;
+
+ fprintf(srv, "GET %s HTTP/1.0\r\nUser-Agent: getgbook-"VERSION" (not mozilla)\r\nHost: %s\r\n\r\n", path, host);
+
+ fflush(srv);
+
+ l=0;
+ fd = fileno(srv);
+
+ buf = malloc(sizeof(char *) * 4096);
+ for(i=0; (res = read(fd, buf+l, 4096)) > 0; l+=res, i++)
+ buf = realloc(buf, sizeof(char *) * (l+4096));
+
+ /* check that it's a 200 */
+ if(strncmp(buf+9, "200 ", 4)) {
+ free(buf);
+ return NULL;
+ }
+
+ /* exclude header */
+ for(p = buf; *p && *(p+1) && *(p+2) && *(p+3); p++)
+ if(!strncmp(p, "\r\n\r\n", 4)) break;
+ p+=4;
+
+ i = l - (p - buf);
+ c = malloc(i);
+ memcpy(c, p, i);
+ free(buf);
+
+ return c;
+}