Fix printing of FTP server response

Hrvoje Niksic <[email protected]> Thu, 05 May 2005 12:09:43 +0200
Newsgroups gmane.comp.web.wget.patches
Message-ID <[email protected]>
Wget 1.10-alpha3 has the following problem:

--12:05:38--  ftp://ftp.gnu.org/
           => `.listing'
Resolving ftp.gnu.org... 199.232.41.7
Connecting to ftp.gnu.org|199.232.41.7|:21... connected.
Logging in as anonymous ...
220 GNU FTP server ready.\015\012--> USER anonymous

230-Due to U.S. Export Regulations, all cryptographic software on this\015\012230-site is subject to the following legal notice:\015\012230-\015\012230-    This site includes publiclyavailable encryption source code\015\012230-    which, together with object code resulting from the compiling of\015\012230-    publicly available source code, may be exported from the United\015\012230-    States under License Exception "TSU" pursuant to 15 C.F.R. Section\015\012230-    740.13(e).\015\012230-\015\012230-This legal notice applies to cryptographic software only. Please see\015\012230-the Bureau of Industry and Security (www.bxa.doc.gov) for more\015\012230-information about current U.S. regulations.\015\012230 Login successful.\015\012--> SYST
[...]

The problem is that the pre-escnonprint() relied on newlines being a
part of the output; on the other hand, escnonprint() explicitly
escapes newlines.  Since that function already has code that strips
trailing [CR]LF, it is trivial to move it in front of the printing.


2005-05-05  Hrvoje Niksic  <[email protected]>

	* ftp-basic.c (ftp_response): Fix printing FTP server response.

Index: src/ftp-basic.c
===================================================================
RCS file: /pack/anoncvs/wget/src/ftp-basic.c,v
retrieving revision 1.43
diff -u -r1.43 ftp-basic.c
--- src/ftp-basic.c	2005/05/03 15:24:29	1.43
+++ src/ftp-basic.c	2005/05/05 10:05:43
@@ -58,26 +58,34 @@
    it.  <CR> and <LF> characters are stripped from the line, and the
    line is 0-terminated.  All the response lines but the last one are
    skipped.  The last line is determined as described in RFC959.  */
+
 uerr_t
 ftp_response (int fd, char **ret_line)
 {
   while (1)
     {
+      char *p;
       char *line = fd_read_line (fd);
       if (!line)
 	return FTPRERR;
+
+      /* Strip trailing CRLF before printing the line, so that
+	 escnonprint doesn't include bogus \012 and \015. */
+      p = strchr (line, '\0');
+      if (p > line && p[-1] == '\n')
+	*--p = '\0';
+      if (p > line && p[-1] == '\r')
+	*--p = '\0';
+
       if (opt.server_response)
-        logputs (LOG_NOTQUIET, escnonprint (line));
+	logprintf (LOG_NOTQUIET, "%s\n", escnonprint (line));
       else
-        DEBUGP (("%s", escnonprint (line)));
+        DEBUGP (("%s\n", escnonprint (line)));
+
+      /* The last line of output is the one that begins with "ddd ". */
       if (ISDIGIT (line[0]) && ISDIGIT (line[1]) && ISDIGIT (line[2])
 	  && line[3] == ' ')
 	{
-	  char *p = line + strlen (line);
-	  if (p > line && p[-1] == '\n')
-	    *--p = '\0';
-	  if (p > line && p[-1] == '\r')
-	    *--p = '\0';
 	  strncpy (ftp_last_respline, line, sizeof (ftp_last_respline));
 	  ftp_last_respline[sizeof (ftp_last_respline) - 1] = '\0';
 	  *ret_line = line;