aboutsummaryrefslogtreecommitdiff
path: root/lib/update.c
blob: cca80db27a62a143b139d7be009894228c6ae899 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
/* $Id$ */
/*
 * Copyright (c) 2004 Dimitri Sokolyuk <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 <string.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 <limits.h>
#include "unzip.h"
#include <tgebup.h>

#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');
}