aboutsummaryrefslogtreecommitdiff
path: root/tgeb/update.c
diff options
context:
space:
mode:
Diffstat (limited to 'tgeb/update.c')
-rw-r--r--tgeb/update.c129
1 files changed, 129 insertions, 0 deletions
diff --git a/tgeb/update.c b/tgeb/update.c
new file mode 100644
index 0000000..307fb75
--- /dev/null
+++ b/tgeb/update.c
@@ -0,0 +1,129 @@
+/* $Id$ */
+/*
+ * Copyright (c) 2004 demon <demon@vhost.dyndns.org>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+#include <err.h>
+#include <unistd.h>
+#include <strings.h>
+#include <netinet/in.h>
+#include <arpa/inet.h>
+#include <sys/socket.h>
+#include <netdb.h>
+#include <sys/types.h>
+#include <time.h>
+#include <zlib.h>
+#include "unzip.h"
+#include "tgebup.h"
+
+#define DATFILE "/tmp/tarife.zip"
+
+const char *TGebURL = "www.billiger-telefonieren.de";
+const char *TGebGET = "GET /downloads/tgeb/tarife_%.4i%.2i%.2i.zip HTTP/1.0\n\n";
+
+static void unzip(void);
+
+void
+tgeb_update(struct tm * tm)
+{
+ struct hostent *he;
+ struct sockaddr_in sin;
+ int sockfd;
+ char *get;
+ char *buf;
+ FILE *fd;
+ char *file;
+ int filelen;
+ int offset = 0;
+
+ if (!(he = gethostbyname(TGebURL)))
+ err(1, "gethostbyname");
+
+ if ((sockfd = socket(AF_INET, SOCK_STREAM, NULL)) == -1)
+ err(1, "socket");
+ memset(&sin, '\0', sizeof(struct sockaddr_in));
+ sin.sin_family = AF_INET;
+ sin.sin_port = htons(80);
+ sin.sin_addr = *((struct in_addr *)he->h_addr);
+
+ if ((connect(sockfd, (struct sockaddr *)&sin,
+ sizeof(struct sockaddr))) == -1)
+ err(1, "connect");
+
+ if (!(get = malloc(sizeof(char) * strlen(TGebGET))))
+ err(1, "malloc");
+ sprintf(get, TGebGET, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday);
+ if ((send(sockfd, get, strlen(get), NULL)) == -1)
+ err(1, "send");
+ free(get);
+
+ if (!(buf = malloc(sizeof(char) * 1024)))
+ err(1, "malloc");
+
+ for(;;) {
+ int block;
+ if ((block = recv(sockfd, &buf[offset], 1024, NULL)) == -1)
+ err(1, "recv");
+ if (block == 0)
+ break;
+ offset += block;
+ if (!(buf = realloc(buf, offset + 1024)))
+ err(1, "malloc");
+ }
+ close(sockfd);
+ file = strstr(buf, "\r\n\r\n") + 4;
+ filelen = offset + buf - file;
+
+ if (!(fd = fopen(DATFILE, "wb")))
+ err(1, "fopen %s", DATFILE);
+ fwrite(file, 1, filelen, fd);
+ fclose(fd);
+ free(buf);
+ unzip();
+ return;
+}
+
+void
+unzip(void)
+{
+ FILE *fd;
+ unzFile *unzfd;
+ unz_global_info gi;
+ unz_file_info fi;
+ char FileName[256];
+ char buf[1024];
+ int i, len;
+
+ if (!(unzfd = unzOpen(DATFILE)))
+ errx(1, "unzOpen %s", DATFILE);
+ unzGetGlobalInfo(unzfd, &gi);
+ for (i = 0; i < gi.number_entry; i++) {
+ unzGetCurrentFileInfo(unzfd, &fi, FileName, sizeof(FileName),
+ NULL, 0, NULL, 0);
+ unzOpenCurrentFile(unzfd);
+ if (!(fd = fopen(FileName, "wb")))
+ err(1, "fopen %s", FileName);
+ while((len = unzReadCurrentFile(unzfd, &buf, 1024))) {
+ fwrite(buf, len, 1, fd);
+ }
+ fclose(fd);
+ unzGoToNextFile(unzfd);
+ }
+ unzClose(unzfd);
+ unlink(DATFILE);
+ return;
+}