-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathget-comics.c
226 lines (195 loc) · 4.62 KB
/
get-comics.c
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
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
/*
* get-comics.c - download comics from the net
* Copyright (C) 2002-2011 Sean MacLennan <[email protected]>
* Revision: 1.3
*
* This program is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License as published by the
* Free Software Foundation; either version 2, or (at your option) any
* later version.
*
* This program is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* for more details.
*
* You should have received a copy of the GNU General Public License
* along with this project; see the file COPYING. If not, write to
* the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
* Boston, MA 02111-1307, USA.
*/
#include "get-comics.h"
#include <getopt.h>
#include <signal.h>
#include <dirent.h>
char *comics_dir;
int skipped;
/* If the user specified this on the command line we do not want the
* config file to override */
int threads_set;
#ifndef WIN32
/* Polarssl can send a sigpipe when a connection is reset by the
* peer. It is safe to just ignore it.
*/
static void sigpipe(int signum) {}
#endif
static void free_comics(void)
{
while (comics) {
struct connection *next = comics->next;
free(comics->url);
free(comics->host);
free(comics->regexp);
free(comics->regfname);
free(comics->outname);
free(comics->base_href);
free(comics->referer);
free(comics);
comics = next;
}
free(comics_dir);
}
/* We have done a chdir to the comics dir */
static void clean_dir(void)
{
DIR *dir = opendir(".");
if (!dir)
return;
struct dirent *ent;
while ((ent = readdir(dir))) {
if (*ent->d_name == '.') continue;
char *p = strrchr(ent->d_name, '.');
if (p && is_imgtype(p))
unlink(ent->d_name);
else
printf("Warning: %s\n", ent->d_name);
}
closedir(dir);
clean_index_dir();
}
static void cd_comics_dir(int clean)
{
if (!comics_dir) {
#ifdef WIN32
char *homedrive = getenv("HOMEDRIVE");
char *homepath = getenv("HOMEPATH");
char home[64];
snprintf(home, sizeof(home), "%s%s", homedrive, homepath);
#else
char *home = getenv("HOME");
#endif
if (home) {
comics_dir = must_alloc(strlen(home) + 10);
sprintf(comics_dir, "%s/comics", home);
} else
comics_dir = must_strdup("comics");
}
if (chdir(comics_dir)) {
my_perror(comics_dir);
exit(1);
}
if (clean)
clean_dir();
}
static void usage(int rc)
{
fputs("usage: get-comics [-hckvCV] [-d comics_dir]", stdout);
puts(" [-i index_dir] [-l links_file]");
puts(" [-t threads] [-T timeout] [config-file ...]");
puts("Where: -h this help");
puts("\t-c clean (remove) images from comics dir before downloading");
puts("\t-k keep index files");
puts("\t-v verbose");
puts("\t-V verify config but don't download comics");
exit(rc);
}
int main(int argc, char *argv[])
{
int i, verify = 0, clean = 0;
while ((i = getopt(argc, argv, "cd:hi:kl:t:vT:V")) != -1)
switch ((char)i) {
case 'c':
clean = 1;
break;
case 'd':
comics_dir = must_strdup(optarg);
break;
case 'h':
usage(0);
case 'i':
add_index_dir(optarg);
break;
case 'k':
unlink_index = 0;
break;
case 'l':
links_only = fopen(optarg, "w");
if (!links_only) {
my_perror(optarg);
exit(1);
}
break;
case 't':
thread_limit = strtol(optarg, NULL, 0);
threads_set = 1;
break;
case 'v':
verbose++;
break;
case 'T':
read_timeout = strtol(optarg, NULL, 0);
break;
case 'V':
verify = 1;
break;
default:
usage(1);
}
if (optind < argc)
while (optind < argc) {
if (read_config(argv[optind])) {
printf("Fatal error in config file\n");
exit(1);
}
++optind;
}
else if (read_config(NULL)) {
printf("Fatal error in config file\n");
exit(1);
}
if (verify) {
printf("Comics: %u Skipped today: %u\n", n_comics + skipped, skipped);
if (verbose)
dump_outstanding(0);
return 0;
}
if (thread_limit == 0) {
printf("You must allow at least one thread\n");
exit(1);
}
if (thread_limit > n_comics)
thread_limit = n_comics;
cd_comics_dir(clean);
#ifdef _WIN32
win32_init();
#else
signal(SIGTERM, dump_outstanding);
signal(SIGHUP, dump_outstanding);
signal(SIGPIPE, sigpipe);
#endif
want_extensions = 1;
main_loop();
out_results(comics, skipped);
#ifdef WIN32
printf("Hit return to exit");
getchar();
#endif
if (links_only)
fclose(links_only);
free_cache(); /* for valgrind */
free_comics(); /* for valgrind */
if (debug_fp)
fclose(debug_fp);
/* Return 0 only if we got all comics */
return gotit != n_comics;
}