Fixed URL parsing bug

Fixed -O - output-to-stdout bug
This commit is contained in:
Randolph Chung 2000-12-07 03:53:47 +00:00
parent 3d957c87b7
commit 02553a2a18
2 changed files with 28 additions and 16 deletions

View File

@ -71,7 +71,11 @@ int wget_main(int argc, char **argv)
++do_continue; ++do_continue;
break; break;
case 'O': case 'O':
fname_out = (strcmp(optarg, "-") == 0 ? NULL : optarg); /* can't set fname_out to NULL if outputting to stdout, because
* this gets interpreted as the auto-gen output filename
* case below - tausq@debian.org
*/
fname_out = (strcmp(optarg, "-") == 0 ? (char *)1 : optarg);
break; break;
default: default:
usage(wget_usage); usage(wget_usage);
@ -110,12 +114,12 @@ int wget_main(int argc, char **argv)
/* /*
* Open the output stream. * Open the output stream.
*/ */
if (fname_out != NULL) { if (fname_out != (char *)1) {
if ( (output=fopen(fname_out, (do_continue ? "a" : "w"))) if ( (output=fopen(fname_out, (do_continue ? "a" : "w")))
== NULL) == NULL)
fatalPerror("fopen(%s)", fname_out); fatalPerror("fopen(%s)", fname_out);
} else { } else {
output=stdout; output = stdout;
} }
/* /*
@ -202,6 +206,7 @@ int wget_main(int argc, char **argv)
void parse_url(char *url, char **uri_host, int *uri_port, char **uri_path) void parse_url(char *url, char **uri_host, int *uri_port, char **uri_path)
{ {
char *s, *h; char *s, *h;
static char *defaultpath = "/";
*uri_port = 80; *uri_port = 80;
@ -209,9 +214,7 @@ void parse_url(char *url, char **uri_host, int *uri_port, char **uri_path)
fatalError("not an http url: %s\n", url); fatalError("not an http url: %s\n", url);
/* pull the host portion to the front of the buffer */ /* pull the host portion to the front of the buffer */
for (s = url, h = url+7 ; *h != '/' ; ++h) { for (s = url, h = url+7 ; *h != '/' && *h != 0; ++h) {
if (*h == '\0')
fatalError("cannot parse url: %s\n", url);
if (*h == ':') { if (*h == ':') {
*uri_port = atoi(h+1); *uri_port = atoi(h+1);
*h = '\0'; *h = '\0';
@ -219,6 +222,9 @@ void parse_url(char *url, char **uri_host, int *uri_port, char **uri_path)
*s++ = *h; *s++ = *h;
} }
*s = '\0'; *s = '\0';
if (*h == 0) h = defaultpath;
*uri_host = url; *uri_host = url;
*uri_path = h; *uri_path = h;
} }
@ -469,7 +475,7 @@ progressmeter(int flag)
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: wget.c,v 1.7 2000/11/14 23:29:24 andersen Exp $ * $Id: wget.c,v 1.8 2000/12/07 03:53:47 tausq Exp $
*/ */

22
wget.c
View File

@ -71,7 +71,11 @@ int wget_main(int argc, char **argv)
++do_continue; ++do_continue;
break; break;
case 'O': case 'O':
fname_out = (strcmp(optarg, "-") == 0 ? NULL : optarg); /* can't set fname_out to NULL if outputting to stdout, because
* this gets interpreted as the auto-gen output filename
* case below - tausq@debian.org
*/
fname_out = (strcmp(optarg, "-") == 0 ? (char *)1 : optarg);
break; break;
default: default:
usage(wget_usage); usage(wget_usage);
@ -110,12 +114,12 @@ int wget_main(int argc, char **argv)
/* /*
* Open the output stream. * Open the output stream.
*/ */
if (fname_out != NULL) { if (fname_out != (char *)1) {
if ( (output=fopen(fname_out, (do_continue ? "a" : "w"))) if ( (output=fopen(fname_out, (do_continue ? "a" : "w")))
== NULL) == NULL)
fatalPerror("fopen(%s)", fname_out); fatalPerror("fopen(%s)", fname_out);
} else { } else {
output=stdout; output = stdout;
} }
/* /*
@ -202,6 +206,7 @@ int wget_main(int argc, char **argv)
void parse_url(char *url, char **uri_host, int *uri_port, char **uri_path) void parse_url(char *url, char **uri_host, int *uri_port, char **uri_path)
{ {
char *s, *h; char *s, *h;
static char *defaultpath = "/";
*uri_port = 80; *uri_port = 80;
@ -209,9 +214,7 @@ void parse_url(char *url, char **uri_host, int *uri_port, char **uri_path)
fatalError("not an http url: %s\n", url); fatalError("not an http url: %s\n", url);
/* pull the host portion to the front of the buffer */ /* pull the host portion to the front of the buffer */
for (s = url, h = url+7 ; *h != '/' ; ++h) { for (s = url, h = url+7 ; *h != '/' && *h != 0; ++h) {
if (*h == '\0')
fatalError("cannot parse url: %s\n", url);
if (*h == ':') { if (*h == ':') {
*uri_port = atoi(h+1); *uri_port = atoi(h+1);
*h = '\0'; *h = '\0';
@ -219,6 +222,9 @@ void parse_url(char *url, char **uri_host, int *uri_port, char **uri_path)
*s++ = *h; *s++ = *h;
} }
*s = '\0'; *s = '\0';
if (*h == 0) h = defaultpath;
*uri_host = url; *uri_host = url;
*uri_path = h; *uri_path = h;
} }
@ -469,7 +475,7 @@ progressmeter(int flag)
* OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
* SUCH DAMAGE. * SUCH DAMAGE.
* *
* $Id: wget.c,v 1.7 2000/11/14 23:29:24 andersen Exp $ * $Id: wget.c,v 1.8 2000/12/07 03:53:47 tausq Exp $
*/ */