diff -ruw wget-1.7/src/connect.c wget-1.7-bandwidth/src/connect.c --- wget-1.7/src/connect.c Sun May 27 21:34:55 2001 +++ wget-1.7-bandwidth/src/connect.c Mon Sep 24 14:19:18 2001 @@ -282,6 +282,22 @@ iread (int fd, char *buf, int len) { int res; + static time_t lastaccess = 0; /* last second measured */ + static int received = 0; /* bytes received last second */ + static int iteration = 0; + static int bytes_per_iteration; + static int iterations_per_second; + + if (!lastaccess) + lastaccess = time(0); + if (!opt.bandwidth_wait) + opt.bandwidth_wait = 50; + + iterations_per_second = 1000 / opt.bandwidth_wait; + bytes_per_iteration = ( opt.bandwidth_limit / (iterations_per_second+1) ); + + if (opt.bandwidth_limit > 0 && len < bytes_per_iteration) + opt.bandwidth_wait -= 10; do { @@ -302,11 +318,39 @@ return -1; } } + if (opt.bandwidth_limit) + { + struct timeval tv; + + if ( received >= bytes_per_iteration ) + { + bzero(&tv, sizeof(struct timeval)); + tv.tv_sec = opt.bandwidth_wait / 1000; /* full seconds */ + tv.tv_usec = (opt.bandwidth_wait % 1000) * 1000; /* microseconds */ + + select(0, NULL, NULL, NULL, &tv); + + received = 0; + iteration++; + } + if (len > bytes_per_iteration-received) + len = bytes_per_iteration-received; + } #endif res = READ (fd, buf, len); } while (res == -1 && errno == EINTR); + if (opt.bandwidth_limit) + { + received += res; /* increment received counter */ + if (lastaccess != time(0) && iteration >= iterations_per_second ) + { + iteration = 0; + lastaccess = time(0); + received = 0; + } + } return res; } diff -ruw wget-1.7/src/init.c wget-1.7-bandwidth/src/init.c --- wget-1.7/src/init.c Sun May 27 21:35:04 2001 +++ wget-1.7-bandwidth/src/init.c Mon Sep 24 14:19:21 2001 @@ -250,6 +250,11 @@ opt.dot_bytes = 1024; opt.dot_spacing = 10; opt.dots_in_line = 50; + + /* : bandwidth limitation */ + opt.bandwidth_limit = 0; /* unlimited */ + opt.bandwidth_wait = 200; /* wait 200ms by default */ + } /* Return the user's home directory (strdup-ed), or NULL if none is diff -ruw wget-1.7/src/main.c wget-1.7-bandwidth/src/main.c --- wget-1.7/src/main.c Sun May 27 21:35:05 2001 +++ wget-1.7-bandwidth/src/main.c Mon Sep 24 14:23:46 2001 @@ -171,6 +171,7 @@ --waitretry=SECONDS wait 1...SECONDS between retries of a retrieval.\n\ -Y, --proxy=on/off turn proxy on or off.\n\ -Q, --quota=NUMBER set retrieval quota to NUMBER.\n\ + --bandwidth=NUMBER set the bandwidth limit in BYTES per sec\n\ \n"), stdout); fputs (_("\ Directories:\n\ @@ -285,6 +286,7 @@ { "accept", required_argument, NULL, 'A' }, { "append-output", required_argument, NULL, 'a' }, { "backups", required_argument, NULL, 151 }, /* undocumented */ + { "bandwidth", required_argument, NULL, 24 }, { "base", required_argument, NULL, 'B' }, { "bind-address", required_argument, NULL, 155 }, { "cache", required_argument, NULL, 'C' }, @@ -540,6 +542,9 @@ setval ("sslcertkey", optarg); break; #endif /* HAVE_SSL */ + case 24: /* bandwidth limitation */ + opt.bandwidth_limit = atoi(optarg); + break; case 'A': setval ("accept", optarg); break; diff -ruw wget-1.7/src/options.h wget-1.7-bandwidth/src/options.h --- wget-1.7/src/options.h Sun May 27 21:35:08 2001 +++ wget-1.7-bandwidth/src/options.h Mon Sep 24 14:24:38 2001 @@ -164,6 +164,10 @@ int cookies; char *cookies_input; char *cookies_output; + int bandwidth_limit; /* : bandwidth limitation in kb */ + int bandwidth_wait; /* : milliseconds to wait when + bandwidth limit es reached */ + }; #ifndef OPTIONS_DEFINED_HERE