struct host_info {
// May be used if we ever will want to free() all xstrdup()s...
/* char *allocated; */
- char *host;
- int port;
- char *path;
- int is_ftp;
- char *user;
+ const char *path;
+ const char *user;
+ char *host;
+ int port;
+ smallint is_ftp;
};
unsigned lastupdate_sec;
unsigned start_sec;
#endif
- bool chunked; /* chunked transfer encoding */
+ smallint chunked; /* chunked transfer encoding */
};
#define G (*(struct globals*)&bb_common_bufsiz1)
struct BUG_G_too_big {
return width;
}
-static void updateprogressmeter(int ignore)
-{
- int save_errno = errno;
-
- progressmeter(0);
- errno = save_errno;
-}
-
-static void alarmtimer(int iwait)
-{
- struct itimerval itv;
-
- itv.it_value.tv_sec = iwait;
- itv.it_value.tv_usec = 0;
- itv.it_interval = itv.it_value;
- setitimer(ITIMER_REAL, &itv, NULL);
-}
-
static void progressmeter(int flag)
{
+ /* We can be called from signal handler */
+ int save_errno = errno;
off_t abbrevsize;
unsigned since_last_update, elapsed;
unsigned ratio;
fprintf(stderr, "%6d%c ", (int)abbrevsize, " kMGTPEZY"[i]);
// Nuts! Ain't it easier to update progress meter ONLY when we transferred++?
-// FIXME: get rid of alarmtimer + updateprogressmeter mess
elapsed = monotonic_sec();
since_last_update = elapsed - lastupdate_sec;
}
}
- if (flag == -1) { /* first call to progressmeter */
- struct sigaction sa;
- sa.sa_handler = updateprogressmeter;
- sigemptyset(&sa.sa_mask);
- sa.sa_flags = SA_RESTART;
- sigaction(SIGALRM, &sa, NULL);
- alarmtimer(1);
- } else if (flag == 1) { /* last call to progressmeter */
- alarmtimer(0);
+ if (flag == 0) {
+ /* last call to progressmeter */
+ alarm(0);
transferred = 0;
- putc('\n', stderr);
+ fputc('\n', stderr);
+ } else {
+ if (flag == -1) {
+ /* first call to progressmeter */
+ struct sigaction sa;
+ sa.sa_handler = progressmeter;
+ sigemptyset(&sa.sa_mask);
+ sa.sa_flags = SA_RESTART;
+ sigaction(SIGALRM, &sa, NULL);
+ }
+ alarm(1);
}
+
+ errno = save_errno;
}
/* Original copyright notice which applies to the CONFIG_FEATURE_WGET_STATUSBAR stuff,
* much of which was blatantly stolen from openssh. */
p = strchr(h->host, '?'); if (!sp || (p && sp > p)) sp = p;
p = strchr(h->host, '#'); if (!sp || (p && sp > p)) sp = p;
if (!sp) {
- /* must be writable because of bb_get_last_path_component() */
- static char nullstr[] ALIGN1 = "";
- h->path = nullstr;
+ h->path = "";
} else if (*sp == '/') {
*sp = '\0';
h->path = sp + 1;
// http://busybox.net?login=john@doe is a valid URL
// memmove converts to:
// http:/busybox.nett?login=john@doe...
- memmove(h->host-1, h->host, sp - h->host);
+ memmove(h->host - 1, h->host, sp - h->host);
h->host--;
sp[-1] = '\0';
h->path = sp;
llist_t *headers_llist = NULL;
#endif
FILE *sfp = NULL; /* socket to web/ftp server */
- FILE *dfp = NULL; /* socket to ftp server (data) */
- char *fname_out = NULL; /* where to direct output (-O) */
+ FILE *dfp; /* socket to ftp server (data) */
+ char *fname_out; /* where to direct output (-O) */
bool got_clen = 0; /* got content-length: from server */
int output_fd = -1;
bool use_proxy = 1; /* Use proxies if env vars are set */
}
}
- /* Guess an output filename */
- if (!fname_out) {
- // Dirty hack. Needed because bb_get_last_path_component
- // will destroy trailing / by storing '\0' in last byte!
- if (!last_char_is(target.path, '/')) {
- fname_out = bb_get_last_path_component(target.path);
-#if ENABLE_FEATURE_WGET_STATUSBAR
- curfile = fname_out;
-#endif
- }
- if (!fname_out || !fname_out[0]) {
- /* bb_get_last_path_component writes
- * to last '/' only. We don't have one here... */
+ /* Guess an output filename, if there was no -O FILE */
+ if (!(opt & WGET_OPT_OUTNAME)) {
+ fname_out = bb_get_last_path_component_nostrip(target.path);
+ /* handle "wget http://kernel.org//" */
+ if (fname_out[0] == '/' || !fname_out[0])
fname_out = (char*)"index.html";
-#if ENABLE_FEATURE_WGET_STATUSBAR
- curfile = fname_out;
-#endif
- }
- if (dir_prefix != NULL)
+ /* -P DIR is considered only if there was no -O FILE */
+ if (dir_prefix)
fname_out = concat_path_file(dir_prefix, fname_out);
-#if ENABLE_FEATURE_WGET_STATUSBAR
} else {
- curfile = bb_get_last_path_component(fname_out);
-#endif
+ if (LONE_DASH(fname_out)) {
+ /* -O - */
+ output_fd = 1;
+ opt &= ~WGET_OPT_CONTINUE;
+ }
}
+#if ENABLE_FEATURE_WGET_STATUSBAR
+ curfile = bb_get_last_path_component_nostrip(fname_out);
+#endif
+
/* Impossible?
if ((opt & WGET_OPT_CONTINUE) && !fname_out)
bb_error_msg_and_die("cannot specify continue (-c) without a filename (-O)"); */
/* Determine where to start transfer */
- if (LONE_DASH(fname_out)) {
- output_fd = 1;
- opt &= ~WGET_OPT_CONTINUE;
- }
if (opt & WGET_OPT_CONTINUE) {
output_fd = open(fname_out, O_WRONLY);
if (output_fd >= 0) {
* HTTP session
*/
do {
- got_clen = chunked = 0;
+ got_clen = 0;
+ chunked = 0;
if (!--try)
bb_error_msg_and_die("too many redirections");
*/
/* Do it before progressmeter (want to have nice error message) */
- if (output_fd < 0)
- output_fd = xopen(fname_out,
- O_WRONLY|O_CREAT|O_EXCL|O_TRUNC);
+ if (output_fd < 0) {
+ int o_flags = O_WRONLY | O_CREAT | O_TRUNC | O_EXCL;
+ /* compat with wget: -O FILE can overwrite */
+ if (opt & WGET_OPT_OUTNAME)
+ o_flags = O_WRONLY | O_CREAT | O_TRUNC;
+ output_fd = xopen(fname_out, o_flags);
+ }
if (!(opt & WGET_OPT_QUIET))
progressmeter(-1);
}
if (!(opt & WGET_OPT_QUIET))
- progressmeter(1);
+ progressmeter(0);
if ((use_proxy == 0) && target.is_ftp) {
fclose(dfp);