-
Notifications
You must be signed in to change notification settings - Fork 13
/
main.c
121 lines (107 loc) · 2.42 KB
/
main.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
#include <unistd.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <signal.h>
#include <pthread.h>
#include "Defs.h"
#include "Data.h"
#include "Misc.h"
#include "Aget.h"
#include "Signal.h"
#include "Resume.h"
#include "main.h"
#include <errno.h>
int main(int argc, char **argv)
{
extern char *optarg;
extern int optind;
int c, error = 0, ret;
struct hist_data h;
int retlog;
/* Allocate heap for download request
* struct request stores all the information that might be
* of interest
*/
req = (struct request *)calloc(1, sizeof(struct request));
/* Only some signals will be emitted */
sigemptyset(&signal_set);
sigaddset(&signal_set, SIGINT);
sigaddset(&signal_set, SIGALRM);
/* Block out all signals */
pthread_sigmask(SIG_BLOCK, &signal_set, NULL);
/* Create a thread for hadling signals */
if ((ret = pthread_create(&hthread, NULL, signal_waiter, NULL)) != 0) {
fprintf(stderr, "main: cannot create signal_waiter thread: %s, exiting...\n", strerror(errno));
exit(-1);
}
while (!error && (c = getopt(argc,argv,"p:l:n:hfv")) != -1) {
switch(c) {
case 'p':
req->port = atoi(optarg);
break;
case 'f':
fsuggested = 1;
break;
case 'l':
strncpy(req->lfile, optarg, MAXBUFSIZ);
break;
case 'n':
if ((nthreads = atoi(optarg)) > MAXTHREADS) {
Log("Error: Maximum # of threads allowed is %d\n", MAXTHREADS);
nthreads = 0;
}
break;
case 'h':
printf("%s\n", PROGVERSION);
usage();
exit(0);
break;
case 'v':
printf("%s\nby Murat BALABAN <[email protected]>\n", PROGVERSION);
exit(0);
break;
default:
error = 1;
usage();
exit(1);
break;
}
}
if (error) {
usage();
exit(1);
}
if (fsuggested == 1 && nthreads == 0) {
fprintf(stderr, "\nERROR: -f and -n should be used together!, exiting...\n\n");
usage();
exit(1);
}
if (argc == 2) /* If only url is supplied... */
fullurl = strdup(argv[1]);
else
if (optind < argc)
if (argc > 2)
fullurl = strdup(argv[optind]);
else {
usage();
exit(1);
}
else
if (optind == argc) {
usage();
exit(1);
}
parse_url(fullurl, req);
/* If a log file for a previous try has been found, read it and
* resume the download job (resume_get), otherwise, start with
* a clean job (get)
*
* Logfile is of the pattern: aget-$file_name.log
*/
if ((retlog = read_log(&h)) != -1)
resume_get(&h);
else
get(req);
return 0;
}