From bb1c3b3ff7308a1eedba8dc4780f76819332dbe8 Mon Sep 17 00:00:00 2001 From: naddy Date: Tue, 16 Feb 2021 16:27:34 +0000 Subject: [PATCH] make use of getline(3) in ftp(1) Replace fparseln(3) with getline(3). This removes the only use of libutil.a(fparseln.o) from the ramdisk. Replace a complicated fgetln(3) idiom with the much simpler getline(3). ok jca@ --- distrib/special/ftp/Makefile | 5 +---- usr.bin/ftp/Makefile | 6 +++--- usr.bin/ftp/cookie.c | 22 ++++++---------------- usr.bin/ftp/fetch.c | 32 +++++++++++++------------------- 4 files changed, 23 insertions(+), 42 deletions(-) diff --git a/distrib/special/ftp/Makefile b/distrib/special/ftp/Makefile index c394dfc815d..a716808026c 100644 --- a/distrib/special/ftp/Makefile +++ b/distrib/special/ftp/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.14 2019/05/16 12:44:17 florian Exp $ +# $OpenBSD: Makefile,v 1.15 2021/02/16 16:27:34 naddy Exp $ CFLAGS+=-DSMALL -DNOSSL @@ -6,7 +6,4 @@ PROG= ftp SRCS= fetch.c ftp.c main.c small.c util.c .PATH: ${.CURDIR}/../../../usr.bin/ftp -LDADD+= -lutil -DPADD+= ${LIBUTIL} - .include diff --git a/usr.bin/ftp/Makefile b/usr.bin/ftp/Makefile index 3eb5a9c261d..38480bfaefb 100644 --- a/usr.bin/ftp/Makefile +++ b/usr.bin/ftp/Makefile @@ -1,4 +1,4 @@ -# $OpenBSD: Makefile,v 1.34 2021/01/27 22:27:41 sthen Exp $ +# $OpenBSD: Makefile,v 1.35 2021/02/16 16:27:34 naddy Exp $ # Define SMALL to disable command line editing and some other features, # NOSSL to disable https support. @@ -8,8 +8,8 @@ PROG= ftp SRCS= cmds.c cmdtab.c complete.c cookie.c domacro.c fetch.c ftp.c \ list.c main.c ruserpass.c small.c stringlist.c util.c -LDADD+= -ledit -lcurses -lutil -ltls -lssl -lcrypto -DPADD+= ${LIBEDIT} ${LIBCURSES} ${LIBUTIL} ${LIBTLS} ${LIBSSL} ${LIBCRYPTO} +LDADD+= -ledit -lcurses -ltls -lssl -lcrypto +DPADD+= ${LIBEDIT} ${LIBCURSES} ${LIBTLS} ${LIBSSL} ${LIBCRYPTO} #COPTS+= -Wall -Wconversion -Wstrict-prototypes -Wmissing-prototypes diff --git a/usr.bin/ftp/cookie.c b/usr.bin/ftp/cookie.c index 6b526a22d14..b5f5bebdac6 100644 --- a/usr.bin/ftp/cookie.c +++ b/usr.bin/ftp/cookie.c @@ -1,4 +1,4 @@ -/* $OpenBSD: cookie.c,v 1.9 2019/05/16 12:44:17 florian Exp $ */ +/* $OpenBSD: cookie.c,v 1.10 2021/02/16 16:27:34 naddy Exp $ */ /* * Copyright (c) 2007 Pierre-Yves Ritschard @@ -58,10 +58,10 @@ void cookie_load(void) { field_t field; - size_t len; time_t date; char *line; - char *lbuf; + char *lbuf = NULL; + size_t lbufsize = 0; char *param; const char *estr; FILE *fp; @@ -75,19 +75,9 @@ cookie_load(void) if (fp == NULL) err(1, "cannot open cookie file %s", cookiefile); date = time(NULL); - lbuf = NULL; - while ((line = fgetln(fp, &len)) != NULL) { - if (line[len - 1] == '\n') { - line[len - 1] = '\0'; - --len; - } else { - if ((lbuf = malloc(len + 1)) == NULL) - err(1, NULL); - memcpy(lbuf, line, len); - lbuf[len] = '\0'; - line = lbuf; - } - line[strcspn(line, "\r")] = '\0'; + while (getline(&lbuf, &lbufsize, fp) != -1) { + line = lbuf; + line[strcspn(line, "\r\n")] = '\0'; line += strspn(line, " \t"); if ((*line == '#') || (*line == '\0')) { diff --git a/usr.bin/ftp/fetch.c b/usr.bin/ftp/fetch.c index c9655122716..c5b1d18cb5b 100644 --- a/usr.bin/ftp/fetch.c +++ b/usr.bin/ftp/fetch.c @@ -1,4 +1,4 @@ -/* $OpenBSD: fetch.c,v 1.200 2021/02/02 12:58:42 robert Exp $ */ +/* $OpenBSD: fetch.c,v 1.201 2021/02/16 16:27:34 naddy Exp $ */ /* $NetBSD: fetch.c,v 1.14 1997/08/18 10:20:20 lukem Exp $ */ /*- @@ -56,7 +56,6 @@ #include #include #include -#include #include #include @@ -76,7 +75,6 @@ static void aborthttp(int); static char hextochar(const char *); static char *urldecode(const char *); static char *recode_credentials(const char *_userinfo); -static char *ftp_readline(FILE *, size_t *); static void ftp_close(FILE **, struct tls **, int *); static const char *sockerror(struct tls *); #ifdef SMALL @@ -330,6 +328,7 @@ url_get(const char *origline, const char *proxyenv, const char *outfile, int las off_t hashbytes; const char *errstr; ssize_t len, wlen; + size_t bufsize; char *proxyhost = NULL; #ifndef NOSSL char *sslpath = NULL, *sslhost = NULL; @@ -805,12 +804,13 @@ noslash: free(buf); #endif /* !NOSSL */ buf = NULL; + bufsize = 0; if (fflush(fin) == EOF) { warnx("Writing HTTP request: %s", sockerror(tls)); goto cleanup_url_get; } - if ((buf = ftp_readline(fin, &len)) == NULL) { + if ((len = getline(&buf, &bufsize, fin)) == -1) { warnx("Receiving HTTP reply: %s", sockerror(tls)); goto cleanup_url_get; } @@ -885,11 +885,10 @@ noslash: /* * Read the rest of the header. */ - free(buf); filesize = -1; for (;;) { - if ((buf = ftp_readline(fin, &len)) == NULL) { + if ((len = getline(&buf, &bufsize, fin)) == -1) { warnx("Receiving HTTP reply: %s", sockerror(tls)); goto cleanup_url_get; } @@ -1001,8 +1000,8 @@ noslash: server_timestamps = 0; #endif /* !SMALL */ } - free(buf); } + free(buf); /* Content-Length should be ignored for Transfer-Encoding: chunked */ if (chunked) @@ -1046,7 +1045,6 @@ noslash: #endif } - free(buf); if ((buf = malloc(buflen)) == NULL) errx(1, "Can't allocate memory for transfer buffer"); @@ -1168,15 +1166,14 @@ static int save_chunked(FILE *fin, struct tls *tls, int out, char *buf, size_t buflen) { - char *header, *end, *cp; + char *header = NULL, *end, *cp; unsigned long chunksize; - size_t hlen, rlen, wlen; + size_t hsize = 0, rlen, wlen; ssize_t written; char cr, lf; for (;;) { - header = ftp_readline(fin, &hlen); - if (header == NULL) + if (getline(&header, &hsize, fin) == -1) break; /* strip CRLF and any optional chunk extension */ header[strcspn(header, ";\r\n")] = '\0'; @@ -1188,10 +1185,10 @@ save_chunked(FILE *fin, struct tls *tls, int out, char *buf, size_t buflen) free(header); return -1; } - free(header); if (chunksize == 0) { /* We're done. Ignore optional trailer. */ + free(header); return 0; } @@ -1205,6 +1202,7 @@ save_chunked(FILE *fin, struct tls *tls, int out, char *buf, size_t buflen) wlen -= written, cp += written) { if ((written = write(out, cp, wlen)) == -1) { warn("Writing output file"); + free(header); return -1; } } @@ -1217,9 +1215,11 @@ save_chunked(FILE *fin, struct tls *tls, int out, char *buf, size_t buflen) if (cr != '\r' || lf != '\n') { warnx("Invalid chunked encoding"); + free(header); return -1; } } + free(header); if (ferror(fin)) warnx("Error while reading from socket: %s", sockerror(tls)); @@ -1654,12 +1654,6 @@ isurl(const char *p) return (0); } -static char * -ftp_readline(FILE *fp, size_t *lenp) -{ - return fparseln(fp, lenp, NULL, "\0\0\0", 0); -} - #ifndef SMALL static int ftp_printf(FILE *fp, const char *fmt, ...) -- 2.20.1