warn(cause);
warn("%s", cause);
url_get(origline, proxyenv, outfile)	const char *origline;	const char *proxyenv;	const char *outfile;{	struct addrinfo hints, *res0, *res;	int error;	int i, out, isftpurl, isfileurl;	volatile int s;	size_t len;	char c, *cp, *ep, *portnum, *path, buf[4096];	char pbuf[NI_MAXSERV];	const char *savefile;	char *line, *proxy, *host, *port;	char *hosttail;	volatile sig_t oldintr;	off_t hashbytes;	char *cause = "unknown";	s = -1;	proxy = NULL;	isftpurl = 0;	isfileurl = 0;#ifdef __GNUC__				/* XXX: to shut up gcc warnings */	(void)&out;	(void)&proxy;	(void)&savefile;#endif	line = strdup(origline);	if (line == NULL)		errx(1, "Can't allocate memory to parse URL");	if (strncasecmp(line, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)		host = line + sizeof(HTTP_URL) - 1;	else if (strncasecmp(line, FTP_URL, sizeof(FTP_URL) - 1) == 0) {		host = line + sizeof(FTP_URL) - 1;		isftpurl = 1;	} else if (strncasecmp(line, FILE_URL, sizeof(FILE_URL) - 1) == 0) {		host = line + sizeof(FILE_URL) - 1;		isfileurl = 1;	} else		errx(1, "url_get: Invalid URL '%s'", line);	if (isfileurl) {		path = host;	} else {		path = strchr(host, '/');		/* find path */		if (EMPTYSTRING(path)) {			if (isftpurl)				goto noftpautologin;			warnx("Invalid URL (no `/' after host): %s", origline);			goto cleanup_url_get;		}		*path++ = '\0';		if (EMPTYSTRING(path)) {			if (isftpurl)				goto noftpautologin;			warnx("Invalid URL (no file after host): %s", origline);			goto cleanup_url_get;		}	}	if (outfile)		savefile = outfile;	else		savefile = basename(path);	if (EMPTYSTRING(savefile)) {		if (isftpurl)			goto noftpautologin;		warnx("Invalid URL (no file after directory): %s", origline);		goto cleanup_url_get;	}	if (proxyenv != NULL) {				/* use proxy */		proxy = strdup(proxyenv);		if (proxy == NULL)			errx(1, "Can't allocate memory for proxy URL.");		if (strncasecmp(proxy, HTTP_URL, sizeof(HTTP_URL) - 1) == 0)			host = proxy + sizeof(HTTP_URL) - 1;		else if (strncasecmp(proxy, FTP_URL, sizeof(FTP_URL) - 1) == 0)			host = proxy + sizeof(FTP_URL) - 1;		else {			warnx("Malformed proxy URL: %s", proxyenv);			goto cleanup_url_get;		}		if (EMPTYSTRING(host)) {			warnx("Malformed proxy URL: %s", proxyenv);			goto cleanup_url_get;		}		*--path = '/';			/* add / back to real path */		path = strchr(host, '/');	/* remove trailing / on host */		if (! EMPTYSTRING(path))			*path++ = '\0';		path = line;	}	if (isfileurl) {		struct stat st;		s = open(path, O_RDONLY);		if (s == -1) {			warn("Can't open file %s", path);			goto cleanup_url_get;		}		if (fstat(s, &st) == -1)			filesize = -1;		else			filesize = st.st_size;		/* Open the output file.  */		if (strcmp(savefile, "-") != 0) {			out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666);			if (out < 0) {				warn("Can't open %s", savefile);				goto cleanup_url_get;			}		} else			out = fileno(stdout);		/* Trap signals */		oldintr = NULL;		if (setjmp(httpabort)) {			if (oldintr)				(void)signal(SIGINT, oldintr);			goto cleanup_url_get;		}		oldintr = signal(SIGINT, abortfile);			bytes = 0;		hashbytes = mark;		progressmeter(-1);			/* Finally, suck down the file. */		i = 0;		while ((len = read(s, buf, sizeof(buf))) > 0) {			bytes += len;			for (cp = buf; len > 0; len -= i, cp += i) {				if ((i = write(out, cp, len)) == -1) {					warn("Writing %s", savefile);					goto cleanup_url_get;				}				else if (i == 0)					break;			}			if (hash && !progress) {				while (bytes >= hashbytes) {					(void)putc('#', ttyout);					hashbytes += mark;				}				(void)fflush(ttyout);			}		}		if (hash && !progress && bytes > 0) {			if (bytes < mark)				(void)putc('#', ttyout);			(void)putc('\n', ttyout);			(void)fflush(ttyout);		}		if (len != 0) {			warn("Reading from file");			goto cleanup_url_get;		}		progressmeter(1);		if (verbose)			fputs("Successfully retrieved file.\n", ttyout);		(void)signal(SIGINT, oldintr);			close(s);		if (out != fileno(stdout))			close(out);		if (proxy)			free(proxy);		free(line);		return (0);	}	if (*host == '[' && (hosttail = strrchr(host, ']')) != NULL &&	    (hosttail[1] == '\0' || hosttail[1] == ':')) {		host++;		*hosttail++ = '\0';	} else		hosttail = host;	portnum = strrchr(hosttail, ':');		/* find portnum */	if (portnum != NULL)		*portnum++ = '\0';	if (debug)		fprintf(ttyout, "host %s, port %s, path %s, save as %s.\n",		    host, portnum, path, savefile);	memset(&hints, 0, sizeof(hints));	hints.ai_family = PF_UNSPEC;	hints.ai_socktype = SOCK_STREAM;	port = portnum ? portnum : httpport;	error = getaddrinfo(host, port, &hints, &res0);	if (error == EAI_SERVICE && port == httpport) {		/*		 * If the services file is corrupt/missing, fall back		 * on our hard-coded defines.		 */		char pbuf[NI_MAXSERV];		snprintf(pbuf, sizeof(pbuf), "%d", HTTP_PORT);		error = getaddrinfo(host, pbuf, &hints, &res0);	}	if (error) {		warnx("%s: %s", gai_strerror(error), host);		goto cleanup_url_get;	}	s = -1;	for (res = res0; res; res = res->ai_next) {		getnameinfo(res->ai_addr, res->ai_addrlen, buf, sizeof(buf),			NULL, 0, NI_NUMERICHOST);		fprintf(ttyout, "Trying %s...\n", buf);		s = socket(res->ai_family, res->ai_socktype, res->ai_protocol);		if (s == -1) {			cause = "socket";			continue;		}again:		if (connect(s, res->ai_addr, res->ai_addrlen) < 0) {			if (errno == EINTR)				goto again;			close(s);			s = -1;			cause = "connect";			continue;		}		/* get port in numeric */		if (getnameinfo(res->ai_addr, res->ai_addrlen, NULL, 0,		    pbuf, sizeof(pbuf), NI_NUMERICSERV) == 0)			port = pbuf;		else			port = NULL;		break;	}	freeaddrinfo(res0);	if (s < 0) {		warn(cause);		goto cleanup_url_get;	}	/*	 * Construct and send the request.  We're expecting a return	 * status of "200". Proxy requests don't want leading /.	 */	if (proxy) {		/*		 * Host: directive must use the destination host address for		 * the original URI (path).  We do not attach it at this moment.		 */		if (verbose)			fprintf(ttyout, "Requesting %s (via %s)\n",			    origline, proxyenv);		snprintf(buf, sizeof(buf), "GET %s HTTP/1.0\r\n\r\n", path);	} else {		if (verbose)			fprintf(ttyout, "Requesting %s\n", origline);		if (strchr(host, ':')) {			char *h, *p;			/* strip off scoped address portion, since it's local to node */			h = strdup(host);			if (h == NULL)				errx(1, "Can't allocate memory.");			if ((p = strchr(h, '%')) != NULL)				*p = '\0';			snprintf(buf, sizeof(buf),			    "GET /%s HTTP/1.0\r\nHost: [%s]%s%s\r\n\r\n",			    path, h, port ? ":" : "", port ? port : "");			free(h);		} else {			snprintf(buf, sizeof(buf),			    "GET /%s HTTP/1.0\r\nHost: %s%s%s\r\n\r\n",			    path, host, port ? ":" : "", port ? port : "");		}	}	len = strlen(buf);	if (write(s, buf, len) < len) {		warn("Writing HTTP request");		goto cleanup_url_get;	}	memset(buf, 0, sizeof(buf));	for (cp = buf; cp < buf + sizeof(buf); ) {		if (read(s, cp, 1) != 1)			goto improper;		if (*cp == '\r')			continue;		if (*cp == '\n')			break;		cp++;	}	buf[sizeof(buf) - 1] = '\0';		/* sanity */	cp = strchr(buf, ' ');	if (cp == NULL)		goto improper;	else		cp++;	if (strncmp(cp, "200", 3)) {		warnx("Error retrieving file: %s", cp);		goto cleanup_url_get;	}	/*	 * Read the rest of the header.	 */	memset(buf, 0, sizeof(buf));	c = '\0';	for (cp = buf; cp < buf + sizeof(buf); ) {		if (read(s, cp, 1) != 1)			goto improper;		if (*cp == '\r')			continue;		if (*cp == '\n' && c == '\n')			break;		c = *cp;		cp++;	}	buf[sizeof(buf) - 1] = '\0';		/* sanity */	/* Look for the "Content-length: " header.  */#define CONTENTLEN "Content-Length: "	for (cp = buf; *cp != '\0'; cp++) {		if (tolower(*cp) == 'c' &&		    strncasecmp(cp, CONTENTLEN, sizeof(CONTENTLEN) - 1) == 0)			break;	}	if (*cp != '\0') {		cp += sizeof(CONTENTLEN) - 1;		ep = strchr(cp, '\n');		if (ep == NULL)			goto improper;		else			*ep = '\0';		filesize = strtol(cp, &ep, 10);		if (filesize < 1 || *ep != '\0')			goto improper;	} else		filesize = -1;	/* Open the output file.  */	if (strcmp(savefile, "-") != 0) {		out = open(savefile, O_CREAT | O_WRONLY | O_TRUNC, 0666);		if (out < 0) {			warn("Can't open %s", savefile);			goto cleanup_url_get;		}	} else		out = fileno(stdout);	/* Trap signals */	oldintr = NULL;	if (setjmp(httpabort)) {		if (oldintr)			(void)signal(SIGINT, oldintr);		goto cleanup_url_get;	}	oldintr = signal(SIGINT, aborthttp);	bytes = 0;	hashbytes = mark;	progressmeter(-1);	/* Finally, suck down the file. */	i = 0;	while ((len = read(s, buf, sizeof(buf))) > 0) {		bytes += len;		for (cp = buf; len > 0; len -= i, cp += i) {			if ((i = write(out, cp, len)) == -1) {				warn("Writing %s", savefile);				goto cleanup_url_get;			}			else if (i == 0)				break;		}		if (hash && !progress) {			while (bytes >= hashbytes) {				(void)putc('#', ttyout);				hashbytes += mark;			}			(void)fflush(ttyout);		}	}	if (hash && !progress && bytes > 0) {		if (bytes < mark)			(void)putc('#', ttyout);		(void)putc('\n', ttyout);		(void)fflush(ttyout);	}	if (len != 0) {		warn("Reading from socket");		goto cleanup_url_get;	}	progressmeter(1);	if (filesize != -1 && len == 0 && bytes != filesize) {		if (verbose)			fputs("Read short file.\n", ttyout);		goto cleanup_url_get;	}	if (verbose)		fputs("Successfully retrieved file.\n", ttyout);	(void)signal(SIGINT, oldintr);	close(s);	if (out != fileno(stdout))		close(out);	if (proxy)		free(proxy);	free(line);	return (0);noftpautologin:	warnx(	    "Auto-login using ftp URLs isn't supported when using $ftp_proxy");	goto cleanup_url_get;improper:	warnx("Improper response from %s", host);cleanup_url_get:	if (s != -1)		close(s);	if (proxy)		free(proxy);	free(line);	return (-1);}