From e5b284ed9e1759292ac0a2c34261266ffee38c32 Mon Sep 17 00:00:00 2001 From: Rob Landley Date: Mon, 19 May 2025 14:14:07 -0500 Subject: [PATCH] Fix ftpget bug reported by Hongsheng Peng: every ftp command produces a response line on the control channel, so ftp_line() having a -1 option to not read a response line is wrong, and gets the parsing out of sync. (But must=0 is ok, that means read and return it, but don't require a specific value.) This resulted in "ftpget HOST filename" successfully fetching the file, but reporting error anyway. --- toys/net/ftpget.c | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/toys/net/ftpget.c b/toys/net/ftpget.c index 8c72d97c..6ec55692 100644 --- a/toys/net/ftpget.c +++ b/toys/net/ftpget.c @@ -70,6 +70,8 @@ static int xread2line(int fd, char *buf, int len) return total+1; } +// Send a command, and read and return the response code. +// If must is nonzero, non-matching response code is a fatal error. static int ftp_line(char *cmd, char *arg, int must) { int rc = 0; @@ -79,11 +81,9 @@ static int ftp_line(char *cmd, char *arg, int must) if (FLAG(v)) fprintf(stderr, s, cmd, arg); dprintf(TT.fd, s, cmd, arg); } - if (must>=0) { - xread2line(TT.fd, toybuf, sizeof(toybuf)); - if (!sscanf(toybuf, "%d", &rc) || (must && rc != must)) - error_exit_raw(toybuf); - } + xread2line(TT.fd, toybuf, sizeof(toybuf)); + if (!sscanf(toybuf, "%d", &rc) || (must && rc != must)) + error_exit_raw(toybuf); return rc; } @@ -175,9 +175,9 @@ void ftpget_main(void) ftp_line("REST", buf, 350); } else lenl = 0; - ftp_line(cmd, remote, -1); + ftp_line(cmd, remote, 150); lenl += xsendfile(port, ii); - ftp_line(0, 0, FLAG(g) ? 226 : 150); + if (FLAG(g)) ftp_line(0, 0, 226); } else if (FLAG(s)) { cmd = "STOR"; if (cnt && lenr) { -- 2.39.5