From 37f6ae6efa76ffd76030b285155c142929e27e71 Mon Sep 17 00:00:00 2001 From: des Date: Wed, 16 Dec 1998 10:24:55 +0000 Subject: 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. --- lib/libfetch/Makefile | 24 +++++++-------- lib/libfetch/common.c | 75 +++++++++++++++++++++++++++++++++++------------ lib/libfetch/common.h | 5 ++-- lib/libfetch/fetch.3 | 4 ++- lib/libfetch/fetch.c | 7 +++-- lib/libfetch/fetch.h | 11 +++---- lib/libfetch/fetch_err.et | 3 +- lib/libfetch/file.c | 7 +++-- lib/libfetch/ftp.c | 15 +++++----- lib/libfetch/http.c | 17 +++++++---- 10 files changed, 109 insertions(+), 59 deletions(-) diff --git a/lib/libfetch/Makefile b/lib/libfetch/Makefile index 772a602..7bd77dc 100644 --- a/lib/libfetch/Makefile +++ b/lib/libfetch/Makefile @@ -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 - -.if !exists(${DEPENDFILE}) -${OBJS} ${POBJS} ${SOBJS}: ${DPSRCS} -.endif diff --git a/lib/libfetch/common.c b/lib/libfetch/common.c index c42f2db..88d23d4 100644 --- a/lib/libfetch/common.c +++ b/lib/libfetch/common.c @@ -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 +#include #include #include #include #include #include +#include +#include #include #include @@ -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); diff --git a/lib/libfetch/common.h b/lib/libfetch/common.h index dc2a42e..9ea23ea 100644 --- a/lib/libfetch/common.h +++ b/lib/libfetch/common.h @@ -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) diff --git a/lib/libfetch/fetch.3 b/lib/libfetch/fetch.3 index f384fd6..c5d50bf 100644 --- a/lib/libfetch/fetch.3 +++ b/lib/libfetch/fetch.3 @@ -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 +.Fd #include .Fd #include .Ft FILE * .Fn fetchGetURL "char *URL" "char *flags" diff --git a/lib/libfetch/fetch.c b/lib/libfetch/fetch.c index 20cf675..1dba89e 100644 --- a/lib/libfetch/fetch.c +++ b/lib/libfetch/fetch.c @@ -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 @@ -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) diff --git a/lib/libfetch/fetch.h b/lib/libfetch/fetch.h index 286b253..6ec7d67 100644 --- a/lib/libfetch/fetch.h +++ b/lib/libfetch/fetch.h @@ -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 -#include - #include #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 diff --git a/lib/libfetch/fetch_err.et b/lib/libfetch/fetch_err.et index 233d7e7..2e247a6 100644 --- a/lib/libfetch/fetch_err.et +++ b/lib/libfetch/fetch_err.et @@ -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 diff --git a/lib/libfetch/file.c b/lib/libfetch/file.c index 66a681f..fb10f8a 100644 --- a/lib/libfetch/file.c +++ b/lib/libfetch/file.c @@ -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 +#include #include #include #include @@ -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; } diff --git a/lib/libfetch/ftp.c b/lib/libfetch/ftp.c index 81da7f8..2e2e8a2 100644 --- a/lib/libfetch/ftp.c +++ b/lib/libfetch/ftp.c @@ -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 +#include #include #include #include @@ -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 @@ ouch: * 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) diff --git a/lib/libfetch/http.c b/lib/libfetch/http.c index 973584c..6f0388d 100644 --- a/lib/libfetch/http.c +++ b/lib/libfetch/http.c @@ -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 @@ ouch: 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; } -- cgit v1.1