bug-inetutils
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[bug-inetutils] Re: ftp: command processing


From: Debarshi 'Rishi' Ray
Subject: [bug-inetutils] Re: ftp: command processing
Date: Wed, 15 Aug 2007 15:20:39 +0530

Here (http://rishi.fedorapeople.org/gnu/ftp-cmd.diff and inline) is an
improved version of the ftp-cmd.diff patch.

Improvements include:

1. Use of MAXLINE instead of sizeof (char) while comparing string
lengths and in fgets, and using sizeof (char) in realloc to avoid
assuming single-byte characters.

2. Removed redundant variable 'int num' used in previous version of the patch.

3. Use 'intr ()' instead of 'return 0' when EOF is encountered on
empty line. Previous version of patch behaved incorrectly.

ChangeLog is included and I kept the old way of attaching parentheses
around 'prompt' in 'another' in case that is what you want.

I would like to commit it by the end of this week, if there are no objections.

Happy hacking,
Debarshi

diff -urNp inetutils/ftp/cmds.c inetutils-build/ftp/cmds.c
--- inetutils/ftp/cmds.c        2007-08-12 07:17:48.000000000 +0530
+++ inetutils-build/ftp/cmds.c  2007-08-15 20:25:30.000000000 +0530
@@ -134,22 +134,77 @@ int
 another (pargc, pargv, prompt)
      int *pargc;
      char ***pargv;
-     char *prompt;
+     const char *prompt;
 {
+  char *buffer;
   int len = strlen (line), ret;

-  if (len >= sizeof (line) - 3)
+  buffer = (char *) malloc (sizeof (char) * (strlen (prompt) + 4));
+  if (!buffer)
+    intr ();
+
+  sprintf (buffer, "(%s) ", prompt);
+
+#if HAVE_LIBREADLINE
+  char *arg;
+
+  arg = readline (buffer);
+  free (buffer);
+
+  if (arg && *arg)
+    add_history (arg);
+  else if (!arg)
+    intr ();
+  else
     {
-      printf ("sorry, arguments too long\n");
+      free (arg);
+      return 0;
+    }
+
+  line = realloc (line, sizeof (char) * (len + strlen (arg) + 2));
+  if (!line)
+    {
+      free (arg);
       intr ();
     }
-  printf ("(%s) ", prompt);
+
   line[len++] = ' ';
-  if (fgets (&line[len], sizeof (line) - len, stdin) == NULL)
+  strcpy (&line[len], arg);
+  free (arg);
+#else
+  char *status;
+
+  if (len >= MAXLINE - 3)
+    {
+      printf ("Sorry, arguments too long.\n");
+      free (buffer);
+      intr ();
+    }
+
+  printf ("%s", buffer);
+  line[len++] = ' ';
+  status = fgets (&line[len], MAXLINE - len, stdin);
+  free (buffer);
+
+  if (status == NULL)
     intr ();
+  else if (!line[len+1])
+    {
+      putchar ('\n');
+      return 0;
+    }
+
   len += strlen (&line[len]);
-  if (len > 0 && line[len - 1] == '\n')
-    line[len - 1] = '\0';
+  if (line[--len] == '\n')
+    line[len] = '\0';
+  else if (len == MAXLINE - 2)
+    {
+      printf ("Sorry, input line too long.\n");
+      while ((len = getchar ()) != '\n' && len != EOF)
+        /* void */ ;
+      intr ();
+    }
+#endif
   makeargv ();
   ret = margc > *pargc;
   *pargc = margc;
diff -urNp inetutils/ftp/extern.h inetutils-build/ftp/extern.h
--- inetutils/ftp/extern.h      2007-08-12 07:17:48.000000000 +0530
+++ inetutils-build/ftp/extern.h        2007-08-15 16:18:35.000000000 +0530
@@ -37,7 +37,7 @@ void abortpt ();
 void abortrecv ();
 void abortsend ();
 void account (int, char **);
-int another (int *, char ***, char *);
+int another (int *, char ***, const char *);
 void blkfree (char **);
 void cd (int, char **);
 void cdup (int, char **);
diff -urNp inetutils/ftp/main.c inetutils-build/ftp/main.c
--- inetutils/ftp/main.c        2007-08-12 07:17:48.000000000 +0530
+++ inetutils-build/ftp/main.c  2007-08-15 20:24:36.000000000 +0530
@@ -287,7 +287,7 @@ cmdscanner (int top)
       if (line)
        {
          free (line);
-         line = 0;
+         line = NULL;
        }
       line = readline (prompt);
       if (!line)
@@ -309,7 +309,7 @@ cmdscanner (int top)
          fflush (stdout);
        }

-      if (fgets (line, sizeof line, stdin) == NULL)
+      if (fgets (line, MAXLINE, stdin) == NULL)
        quit (0, 0);
       l = strlen (line);
       if (l == 0)
@@ -320,9 +320,9 @@ cmdscanner (int top)
            break;
          line[l] = '\0';
        }
-      else if (l == sizeof (line) - 2)
+      else if (l == MAXLINE - 2)
        {
-         printf ("sorry, input line too long\n");
+         printf ("Sorry, input line too long.\n");
          while ((l = getchar ()) != '\n' && l != EOF)
            /* void */ ;
          break;
diff -urNp inetutils/ChangeLog inetutils-build/ChangeLog
--- inetutils/ChangeLog 2007-08-12 07:17:48.000000000 +0530
+++ inetutils-build/ChangeLog   2007-08-15 20:29:44.000000000 +0530
@@ -1,3 +1,18 @@
+2007-08-15  Debarshi Ray <address@hidden>
+
+       * ftp/cmds.c (another): Changed to 'int another (int *pargc,
+       char ***pargv, const char *prompt)'. Added new variables BUFFER, ARG
+       and STATUS. Use HAVE_LIBREADLINE to differentiate between systems
+       having libreadline and systems not having it. Fix error-checking and
+       use READLINE when HAVE_LIBREADLINE is true. Use MAXLINE and do not
+       assume single-byte characters.
+
+       * ftp/extern.h (another) [notdef]: Changed to 'int another (int
+       *pargc, char ***pargv, const char *prompt)'.
+
+       * ftp/main.c (cmdscanner): Use NULL instead of 0. Use MAXLINE and do
+       not assume single-byte characters. Properly punctuate error message.
+
 2007-07-26  Debarshi Ray <address@hidden>

        * ping/ping_echo.c (ping_echo): Free PING->ping_hostname.


-- 
GPG key ID: 63D4A5A7
Key server: pgp.mit.edu




reply via email to

[Prev in Thread] Current Thread [Next in Thread]