/* $Id$ */ /* * Copyright (c) 2004 Dimitri Sokolyuk * * 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 #include #include #include #include #include #include #include #include #include #include #include #include #include "unzip.h" #include #define DATFILE "tarife.zip" #define BUF_SIZ 1024 const char *TGebURL = "www.billiger-telefonieren.de"; const char *TGebGET = "GET /downloads/tgeb/tarife_%.4i%.2i%.2i.zip HTTP/1.0\n\n"; extern char *__progname; static void unzip(void); static void twiddle(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; printf("%s: updating database ", __progname); fflush(stdout); if (!(he = gethostbyname(TGebURL))) err(1, "gethostbyname"); if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) == -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 = calloc(strlen(TGebGET), sizeof(char)))) err(1, "malloc"); snprintf(get, strlen(TGebGET), TGebGET, tm->tm_year + 1900, tm->tm_mon + 1, tm->tm_mday); if ((send(sockfd, get, strlen(get), 0)) == -1) err(1, "send"); free(get); if (!(buf = calloc(BUF_SIZ, sizeof(char)))) err(1, "malloc"); for (;;) { int block; if ((block = recv(sockfd, &buf[offset], BUF_SIZ, 0)) == -1) err(1, "recv"); if (block == 0) break; offset += block; if (!(buf = realloc(buf, offset + BUF_SIZ))) err(1, "malloc"); twiddle(); } printf(" \tdone\n"); 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[_POSIX_PATH_MAX]; char *buf; int i, len; if (!(unzfd = unzOpen(DATFILE))) errx(1, "unzOpen %s", DATFILE); unzGetGlobalInfo(unzfd, &gi); if (!(buf = calloc(BUF_SIZ, sizeof(char)))) err(1, "malloc"); for (i = 0; i < gi.number_entry; i++) { unzGetCurrentFileInfo(unzfd, &fi, FileName, sizeof(FileName), NULL, 0, NULL, 0); printf("%s: extracting %s ", __progname, FileName); fflush(stdout); unzOpenCurrentFile(unzfd); if (!(fd = fopen(FileName, "wb"))) err(1, "fopen %s", FileName); while ((len = unzReadCurrentFile(unzfd, buf, BUF_SIZ))) { fwrite(buf, len, 1, fd); twiddle(); } fclose(fd); printf(" \tdone\n"); unzGoToNextFile(unzfd); } free(buf); unzClose(unzfd); unlink(DATFILE); return; } void twiddle(void) { static int pos; putchar("|/-\\"[pos++ & 3]); putchar('\b'); }