From e9582417392ed244d57d8d625ab335a3589831fe Mon Sep 17 00:00:00 2001 From: des Date: Thu, 5 Nov 1998 19:48:17 +0000 Subject: First of a series of cleanups to libfetch. Changed files, in alphabetical order: Makefile: Add common.c to SRCS. Make debugging easier by making 'CFLAGS += -DNDEBUG' conditional on DEBUG Don't declare struct {ftp,http}err in {ftp,http}err.c; use struct fetcherr instead. README: Remove the todo list, which is out of date anyway. common.c: (new file) Gather utility functions in this file. Merge the error reporting functions intp _fetch_errstring(), _fetch_seterr() and _fetch_syserr(). Set fetchLastErrCode and fetchLastErrText appropriately when fetchConnect fails. common.h: (new file) Gather internal prototypes and structures in this files. fetch.3: Undocument fetchFreeURL(). Document a few more known bugs. Document fetchLastErrCode and fetchLastErrText. fetch.c: Add descriptive comments to all functions that lacked them. Move fetchConnect() to common.c. Obviate the need for fetchFreeURL(), and remove it. fetch.h: Modify struct url_t so the document part is at the end. ftp.c: Remove code that is duplicated elsewhere. http.c: Remove code that is duplicated elsewhere. Prompted by: jkh --- lib/libfetch/fetch.c | 85 +++++++++++++++++----------------------------------- 1 file changed, 28 insertions(+), 57 deletions(-) (limited to 'lib/libfetch/fetch.c') diff --git a/lib/libfetch/fetch.c b/lib/libfetch/fetch.c index 54421a2..226b628 100644 --- a/lib/libfetch/fetch.c +++ b/lib/libfetch/fetch.c @@ -25,20 +25,15 @@ * (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.3 1998/07/11 21:29:07 des Exp $ + * $Id: fetch.c,v 1.4 1998/08/17 09:30:19 des Exp $ */ #include -#include -#include -#include #include -#include #include #include #include -#include #include "fetch.h" @@ -51,6 +46,10 @@ int fetchLastErrCode; const char *fetchLastErrText; +/* + * Select the appropriate protocol for the URL scheme, and return a + * read-only stream connected to the document referenced by the URL. + */ FILE * fetchGet(url_t *URL, char *flags) { @@ -64,6 +63,10 @@ fetchGet(url_t *URL, char *flags) } +/* + * Select the appropriate protocol for the URL scheme, and return a + * write-only stream connected to the document referenced by the URL. + */ FILE * fetchPut(url_t *URL, char *flags) { @@ -76,7 +79,9 @@ fetchPut(url_t *URL, char *flags) else return NULL; } -/* get URL */ +/* + * Attempt to parse the given URL; if successful, call fetchGet(). + */ FILE * fetchGetURL(char *URL, char *flags) { @@ -88,12 +93,14 @@ fetchGetURL(char *URL, char *flags) f = fetchGet(u, flags); - fetchFreeURL(u); + free(u); return f; } -/* put URL */ +/* + * Attempt to parse the given URL; if successful, call fetchPut(). + */ FILE * fetchPutURL(char *URL, char *flags) { @@ -105,7 +112,7 @@ fetchPutURL(char *URL, char *flags) f = fetchPut(u, flags); - fetchFreeURL(u); + free(u); return f; } @@ -171,11 +178,17 @@ fetchParseURL(char *URL) nohost: /* document */ - if (*p) - u->doc = strdup(p); - u->doc = strdup(*p ? p : "/"); - if (!u->doc) - goto ouch; + if (*p) { + url_t *t; + t = realloc(u, sizeof(*u)+strlen(p)-1); + if (t == NULL) + goto ouch; + u = t; + strcpy(u->doc, p); + } else { + u->doc[0] = '/'; + u->doc[1] = 0; + } DEBUG(fprintf(stderr, "scheme: [\033[1m%s\033[m]\n" @@ -193,45 +206,3 @@ ouch: free(u); return NULL; } - -void -fetchFreeURL(url_t *u) -{ - if (u) { - if (u->doc) - free(u->doc); - free(u); - } -} - -int -fetchConnect(char *host, int port) -{ - struct sockaddr_in sin; - struct hostent *he; - int sd; - -#ifndef NDEBUG - fprintf(stderr, "\033[1m---> %s:%d\033[m\n", host, port); -#endif - - /* look up host name */ - if ((he = gethostbyname(host)) == NULL) - return -1; - - /* set up socket address structure */ - bzero(&sin, sizeof(sin)); - bcopy(he->h_addr, (char *)&sin.sin_addr, he->h_length); - sin.sin_family = he->h_addrtype; - sin.sin_port = htons(port); - - /* try to connect */ - if ((sd = socket(sin.sin_family, SOCK_STREAM, IPPROTO_TCP)) == -1) - return -1; - if (connect(sd, (struct sockaddr *)&sin, sizeof sin) == -1) { - close(sd); - return -1; - } - - return sd; -} -- cgit v1.1