Add verbose flag, and support functions.

Brucify the Makefile.
Differentiate atime and mtime in fetch*Stat().
Fix a few pointer bugs.
Tweak some error messages.
Don't #include sys/param.h and stdio.h in fetch.h.
Document that sys/param.h and stdio.h must be #included before fetch.h.
This commit is contained in:
Dag-Erling Smørgrav 1998-12-16 10:24:55 +00:00
parent e6799271cd
commit 0fba3a0005
Notes: svn2git 2020-12-20 02:59:44 +00:00
svn path=/head/; revision=41862
10 changed files with 109 additions and 59 deletions

View File

@ -1,27 +1,25 @@
# $Id: Makefile,v 1.8 1998/11/07 08:59:38 des Exp $
# $Id: Makefile,v 1.9 1998/12/15 12:24:26 des Exp $
LIB= fetch
CFLAGS+= -I. -Wall -pedantic
.if !defined(DEBUG)
CFLAGS+= -DNDEBUG
.endif
SRCS= fetch.c common.c ftp.c http.c file.c fetch_err.c
DPSRCS= ftperr.inc httperr.inc fetch_err.c fetch_err.h
SRCS= fetch.c common.c ftp.c http.c file.c fetch_err.c \
fetch_err.h ftperr.h httperr.h
MAN3= fetch.3
CLEANFILES= ${DPSRCS}
CLEANFILES= fetch_err.c fetch_err.h ftperr.h httperr.h
SHLIB_MAJOR= 1
SHLIB_MINOR= 0
beforedepend: ${DPSRCS}
beforeinstall: fetch.h fetch_err.h
beforeinstall:
${INSTALL} -C -o ${BINOWN} -g ${BINGRP} -m 444 ${.CURDIR}/fetch.h \
${DESTDIR}/usr/include
${INSTALL} -C -o ${BINOWN} -g ${BINGRP} -m 444 fetch_err.h \
${DESTDIR}/usr/include
ftperr.inc: ftp.errors
ftperr.h: ftp.errors
@echo "static struct fetcherr _ftp_errlist[] = {" > ${.TARGET}
@cat ${.ALLSRC} \
| grep -v ^# \
@ -32,8 +30,7 @@ ftperr.inc: ftp.errors
@echo " { -1, FETCH_UNKNOWN, \"Unknown FTP error\" }" >> ${.TARGET}
@echo "};" >> ${.TARGET}
httperr.inc: http.errors
httperr.h: http.errors
@echo "static struct fetcherr _http_errlist[] = {" > ${.TARGET}
@cat ${.ALLSRC} \
| grep -v ^# \
@ -44,11 +41,10 @@ httperr.inc: http.errors
@echo " { -1, FETCH_UNKNOWN, \"Unknown HTTP error\" }" >> ${.TARGET}
@echo "};" >> ${.TARGET}
hdrs: fetch_err.h
.ORDER: fetch_err.c fetch_err.h
fetch_err.c fetch_err.h: fetch_err.et
compile_et -lang c ${.ALLSRC}
.include <bsd.lib.mk>
.if !exists(${DEPENDFILE})
${OBJS} ${POBJS} ${SOBJS}: ${DPSRCS}
.endif

View File

@ -25,16 +25,18 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: common.c,v 1.1 1998/11/05 19:48:16 des Exp $
* $Id: common.c,v 1.2 1998/11/06 22:14:08 des Exp $
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <com_err.h>
#include <errno.h>
#include <netdb.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
@ -49,7 +51,7 @@
*/
static struct fetcherr _netdb_errlist[] = {
{ HOST_NOT_FOUND, FETCH_RESOLV, "Host not found" },
{ TRY_AGAIN, FETCH_RESOLV, "Transient resolver failure" },
{ TRY_AGAIN, FETCH_TEMP, "Transient resolver failure" },
{ NO_RECOVERY, FETCH_RESOLV, "Non-recoverable resolver failure" },
{ NO_DATA, FETCH_RESOLV, "No address record" },
{ -1, FETCH_UNKNOWN, "Unknown resolver error" }
@ -94,7 +96,8 @@ _fetch_seterr(struct fetcherr *p, int e)
_fetch_init_com_err();
n = _fetch_finderr(p, e);
com_err("libfetch", p[n].cat, "(%d %s)", e, p[n].string);
fetchLastErrCode = p[n].cat;
com_err("libfetch", fetchLastErrCode, "(%03d %s)", e, p[n].string);
}
/*
@ -103,38 +106,39 @@ _fetch_seterr(struct fetcherr *p, int e)
void
_fetch_syserr(void)
{
int cat;
int e;
e = errno;
if (!com_err_initialized)
_fetch_init_com_err();
switch (errno) {
case 0:
cat = FETCH_OK;
fetchLastErrCode = FETCH_OK;
break;
case EPERM:
case EACCES:
case EROFS:
case EAUTH:
case ENEEDAUTH:
cat = FETCH_AUTH;
fetchLastErrCode = FETCH_AUTH;
break;
case ENOENT:
case EISDIR: /* XXX */
cat = FETCH_UNAVAIL;
fetchLastErrCode = FETCH_UNAVAIL;
break;
case ENOMEM:
cat = FETCH_MEMORY;
fetchLastErrCode = FETCH_MEMORY;
break;
case EBUSY:
case EAGAIN:
cat = FETCH_TEMP;
fetchLastErrCode = FETCH_TEMP;
break;
case EEXIST:
cat = FETCH_EXISTS;
fetchLastErrCode = FETCH_EXISTS;
break;
case ENOSPC:
cat = FETCH_FULL;
fetchLastErrCode = FETCH_FULL;
break;
case EADDRINUSE:
case EADDRNOTAVAIL:
@ -142,23 +146,50 @@ _fetch_syserr(void)
case ENETUNREACH:
case ENETRESET:
case EHOSTUNREACH:
cat = FETCH_NETWORK;
fetchLastErrCode = FETCH_NETWORK;
break;
case ECONNABORTED:
case ECONNRESET:
cat = FETCH_ABORT;
fetchLastErrCode = FETCH_ABORT;
break;
case ETIMEDOUT:
cat = FETCH_TIMEOUT;
fetchLastErrCode = FETCH_TIMEOUT;
break;
case ECONNREFUSED:
case EHOSTDOWN:
cat = FETCH_DOWN;
fetchLastErrCode = FETCH_DOWN;
break;
default:
cat = FETCH_UNKNOWN;
fetchLastErrCode = FETCH_UNKNOWN;
}
com_err("libfetch", fetchLastErrCode, "(%03d %s)", e, strerror(e));
}
/*
* Emit status message
*/
int
_fetch_info(char *fmt, ...)
{
va_list ap;
char *s;
if (!com_err_initialized)
_fetch_init_com_err();
va_start(ap, fmt);
vasprintf(&s, fmt, ap);
va_end(ap);
if (s == NULL) {
com_err("libfetch", FETCH_MEMORY, "");
return -1;
} else {
com_err("libfetch", FETCH_VERBOSE, "%s", s);
free(s);
return 0;
}
com_err("libfetch", cat, "(%02d %s)", errno, strerror(errno));
}
@ -168,7 +199,7 @@ _fetch_syserr(void)
* Establish a TCP connection to the specified port on the specified host.
*/
int
fetchConnect(char *host, int port)
fetchConnect(char *host, int port, int verbose)
{
struct sockaddr_in sin;
struct hostent *he;
@ -177,6 +208,9 @@ fetchConnect(char *host, int port)
#ifndef NDEBUG
fprintf(stderr, "\033[1m---> %s:%d\033[m\n", host, port);
#endif
if (verbose)
_fetch_info("looking up %s", host);
/* look up host name */
if ((he = gethostbyname(host)) == NULL) {
@ -184,6 +218,9 @@ fetchConnect(char *host, int port)
return -1;
}
if (verbose)
_fetch_info("connecting to %s:%d", host, port);
/* set up socket address structure */
bzero(&sin, sizeof(sin));
bcopy(he->h_addr, (char *)&sin.sin_addr, he->h_length);

View File

@ -25,7 +25,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: common.h,v 1.1 1998/11/05 19:48:16 des Exp $
* $Id: common.h,v 1.2 1998/11/06 22:14:08 des Exp $
*/
#ifndef _COMMON_H_INCLUDED
@ -40,7 +40,8 @@ struct fetcherr {
void _fetch_seterr(struct fetcherr *, int);
void _fetch_syserr(void);
int fetchConnect(char *, int);
int _fetch_info(char *fmt, ...);
int fetchConnect(char *, int, int);
#define _ftp_seterr(n) _fetch_seterr(_ftp_errlist, n)
#define _http_seterr(n) _fetch_seterr(_http_errlist, n)

View File

@ -22,7 +22,7 @@
.\" OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
.\" SUCH DAMAGE.
.\"
.\" $Id: fetch.3,v 1.3 1998/11/05 19:48:16 des Exp $
.\" $Id: fetch.3,v 1.4 1998/11/06 22:14:08 des Exp $
.\"
.Dd July 1, 1998
.Dt FETCH 3
@ -46,6 +46,8 @@
.Nm fetchStatFTP
.Nd file transfer library
.Sh SYNOPSIS
.Fd #include <sys/param.h>
.Fd #include <stdio.h>
.Fd #include <fetch.h>
.Ft FILE *
.Fn fetchGetURL "char *URL" "char *flags"

View File

@ -25,7 +25,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: fetch.c,v 1.5 1998/11/05 19:48:17 des Exp $
* $Id: fetch.c,v 1.6 1998/11/06 22:14:08 des Exp $
*/
#include <sys/param.h>
@ -40,6 +40,9 @@
#include "common.h"
int fetchLastErrCode;
/*** Local data **************************************************************/
/*
@ -205,7 +208,7 @@ fetchParseURL(char *URL)
else URL += 2;
p = strpbrk(URL, "/@");
if (*p == '@') {
if (p && *p == '@') {
/* username */
for (q = URL, i = 0; (*q != ':') && (*q != '@'); q++)
if (i < URL_USERLEN)

View File

@ -25,15 +25,12 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: fetch.h,v 1.5 1998/11/05 19:48:17 des Exp $
* $Id: fetch.h,v 1.6 1998/11/06 22:14:08 des Exp $
*/
#ifndef _FETCH_H_INCLUDED
#define _FETCH_H_INCLUDED
#include <sys/param.h>
#include <stdio.h>
#include <fetch_err.h>
#define _LIBFETCH_VER "libfetch/1.0"
@ -53,7 +50,8 @@ struct url {
struct url_stat {
off_t size;
time_t time;
time_t atime;
time_t mtime;
};
/* FILE-specific functions */
@ -81,4 +79,7 @@ FILE *fetchGet(struct url *, char *);
FILE *fetchPut(struct url *, char *);
int fetchStat(struct url *, struct url_stat *, char *);
/* Last error code */
extern int fetchLastErrCode;
#endif

View File

@ -25,7 +25,7 @@
# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
# THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
#
# $Id$
# $Id: fetch_err.et,v 1.1 1998/11/06 22:14:08 des Exp $
#
et ftch
ec FETCH_ABORT, "Operation aborted"
@ -46,4 +46,5 @@ et ftch
ec FETCH_UNAVAIL, "File is not available"
ec FETCH_UNKNOWN, "Unknown error"
ec FETCH_URL, "Invalid URL"
ec FETCH_VERBOSE, ""
end

View File

@ -25,10 +25,10 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: file.c,v 1.1.1.1 1998/07/09 16:52:41 des Exp $
* $Id: file.c,v 1.2 1998/11/06 22:14:08 des Exp $
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/stat.h>
#include <stdio.h>
#include <string.h>
@ -73,6 +73,7 @@ fetchStatFile(struct url *u, struct url_stat *us, char *flags)
return -1;
}
us->size = sb.st_size;
us->time = sb.st_mtime;
us->atime = sb.st_atime;
us->mtime = sb.st_mtime;
return 0;
}

View File

@ -25,7 +25,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: ftp.c,v 1.6 1998/11/05 19:48:17 des Exp $
* $Id: ftp.c,v 1.7 1998/11/06 22:14:08 des Exp $
*/
/*
@ -55,7 +55,7 @@
*
*/
#include <sys/types.h>
#include <sys/param.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <sys/errno.h>
@ -71,7 +71,7 @@
#include "fetch.h"
#include "common.h"
#include "ftperr.inc"
#include "ftperr.h"
#define FTP_DEFAULT_TO_ANONYMOUS
#define FTP_ANONYMOUS_USER "ftp"
@ -275,7 +275,7 @@ _ftp_transfer(FILE *cf, char *oper, char *file, char *mode, int pasv)
* Log on to FTP server
*/
static FILE *
_ftp_connect(char *host, int port, char *user, char *pwd)
_ftp_connect(char *host, int port, char *user, char *pwd, int verbose)
{
int sd, e, pp = FTP_DEFAULT_PORT;
char *p, *q;
@ -289,12 +289,12 @@ _ftp_connect(char *host, int port, char *user, char *pwd)
}
if (q)
*q = 0;
sd = fetchConnect(p, pp);
sd = fetchConnect(p, pp, verbose);
if (q)
*q = ':';
} else {
/* no proxy, go straight to target */
sd = fetchConnect(host, port);
sd = fetchConnect(host, port, verbose);
}
/* check connection */
@ -398,7 +398,8 @@ fetchXxxFTP(struct url *url, char *oper, char *mode, char *flags)
/* connect to server */
if (!cf) {
cf = _ftp_connect(url->host, url->port, url->user, url->pwd);
cf = _ftp_connect(url->host, url->port, url->user, url->pwd,
(strchr(flags, 'v') != NULL));
if (!cf)
return NULL;
if (cached_socket)

View File

@ -25,7 +25,7 @@
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
* THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*
* $Id: http.c,v 1.6 1998/11/05 19:48:17 des Exp $
* $Id: http.c,v 1.7 1998/11/06 22:14:08 des Exp $
*/
/*
@ -78,7 +78,7 @@
#include "fetch.h"
#include "common.h"
#include "httperr.inc"
#include "httperr.h"
#ifndef NDEBUG
#define DEBUG(x) do x; while (0)
@ -305,12 +305,14 @@ _http_auth(char *usr, char *pwd)
FILE *
fetchGetHTTP(struct url *URL, char *flags)
{
int sd = -1, err, i, enc = ENC_NONE;
int sd = -1, err, i, enc = ENC_NONE, verbose;
struct cookie *c;
char *ln, *p, *q;
FILE *f, *cf;
size_t len;
verbose = (strchr(flags, 'v') != NULL);
/* allocate cookie */
if ((c = calloc(1, sizeof(struct cookie))) == NULL)
return NULL;
@ -340,12 +342,12 @@ fetchGetHTTP(struct url *URL, char *flags)
host[len] = 0;
/* connect */
sd = fetchConnect(host, port);
sd = fetchConnect(host, port, verbose);
}
/* if no proxy is configured or could be contacted, try direct */
if (sd == -1) {
if ((sd = fetchConnect(URL->host, URL->port)) == -1)
if ((sd = fetchConnect(URL->host, URL->port, verbose)) == -1)
goto ouch;
}
@ -355,6 +357,9 @@ fetchGetHTTP(struct url *URL, char *flags)
c->real_f = f;
/* send request (proxies require absolute form, so use that) */
if (verbose)
_fetch_info("requesting http://%s:%d%s",
URL->host, URL->port, URL->doc);
_http_cmd(f, "GET http://%s:%d%s HTTP/1.1" ENDL,
URL->host, URL->port, URL->doc);
@ -443,10 +448,12 @@ fetchGetHTTP(struct url *URL, char *flags)
if (sd >= 0)
close(sd);
free(c);
_http_seterr(999); /* XXX do this properly RSN */
return NULL;
fouch:
fclose(f);
free(c);
_http_seterr(999); /* XXX do this properly RSN */
return NULL;
}