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?
Index: distrib/special/ftp/Makefile
===================================================================
RCS file: /cvs/src/distrib/special/ftp/Makefile,v
retrieving revision 1.14
diff -u -p -r1.14 Makefile
--- distrib/special/ftp/Makefile 16 May 2019 12:44:17 -0000 1.14
+++ distrib/special/ftp/Makefile 29 Jan 2021 18:05:46 -0000
@@ -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 <bsd.prog.mk>
Index: usr.bin/ftp/Makefile
===================================================================
RCS file: /cvs/src/usr.bin/ftp/Makefile,v
retrieving revision 1.34
diff -u -p -r1.34 Makefile
--- usr.bin/ftp/Makefile 27 Jan 2021 22:27:41 -0000 1.34
+++ usr.bin/ftp/Makefile 29 Jan 2021 17:57:31 -0000
@@ -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
Index: usr.bin/ftp/cookie.c
===================================================================
RCS file: /cvs/src/usr.bin/ftp/cookie.c,v
retrieving revision 1.9
diff -u -p -r1.9 cookie.c
--- usr.bin/ftp/cookie.c 16 May 2019 12:44:17 -0000 1.9
+++ usr.bin/ftp/cookie.c 29 Jan 2021 16:07:56 -0000
@@ -58,10 +58,9 @@ void
cookie_load(void)
{
field_t field;
- size_t len;
time_t date;
- char *line;
- char *lbuf;
+ char *line = NULL;
+ size_t linesize = 0;
char *param;
const char *estr;
FILE *fp;
@@ -75,19 +74,8 @@ 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(&line, &linesize, fp) != -1) {
+ line[strcspn(line, "\r\n")] = '\0';
line += strspn(line, " \t");
if ((*line == '#') || (*line == '\0')) {
@@ -172,7 +160,7 @@ cookie_load(void)
} else
TAILQ_INSERT_TAIL(&jar, ck, entry);
}
- free(lbuf);
+ free(line);
fclose(fp);
}
Index: usr.bin/ftp/fetch.c
===================================================================
RCS file: /cvs/src/usr.bin/ftp/fetch.c,v
retrieving revision 1.199
diff -u -p -r1.199 fetch.c
--- usr.bin/ftp/fetch.c 1 Jan 2021 17:39:54 -0000 1.199
+++ usr.bin/ftp/fetch.c 29 Jan 2021 17:57:58 -0000
@@ -56,7 +56,6 @@
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
-#include <util.h>
#include <resolv.h>
#ifndef NOSSL
@@ -75,7 +74,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
@@ -329,6 +327,7 @@ url_get(const char *origline, const char
off_t hashbytes;
const char *errstr;
ssize_t len, wlen;
+ size_t bufsize;
char *proxyhost = NULL;
#ifndef NOSSL
char *sslpath = NULL, *sslhost = NULL;
@@ -790,12 +789,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;
}
@@ -867,11 +867,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;
}
@@ -974,8 +973,8 @@ noslash:
if (strcasecmp(cp, "chunked") == 0)
chunked = 1;
}
- free(buf);
}
+ free(buf);
/* Content-Length should be ignored for Transfer-Encoding: chunked */
if (chunked)
@@ -1019,7 +1018,6 @@ noslash:
#endif
}
- free(buf);
if ((buf = malloc(buflen)) == NULL)
errx(1, "Can't allocate memory for transfer buffer");
@@ -1130,15 +1128,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';
@@ -1150,10 +1147,10 @@ save_chunked(FILE *fin, struct tls *tls,
free(header);
return -1;
}
- free(header);
if (chunksize == 0) {
/* We're done. Ignore optional trailer. */
+ free(header);
return 0;
}
@@ -1167,6 +1164,7 @@ save_chunked(FILE *fin, struct tls *tls,
wlen -= written, cp += written) {
if ((written = write(out, cp, wlen)) == -1) {
warn("Writing output file");
+ free(header);
return -1;
}
}
@@ -1179,9 +1177,11 @@ save_chunked(FILE *fin, struct tls *tls,
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));
@@ -1614,12 +1614,6 @@ isurl(const char *p)
strstr(p, ":/"))
return (1);
return (0);
-}
-
-static char *
-ftp_readline(FILE *fp, size_t *lenp)
-{
- return fparseln(fp, lenp, NULL, "\0\0\0", 0);
}
#ifndef SMALL
--
Christian "naddy" Weisgerber [email protected]