]> git.puffer.fish Git - mirror/frr.git/commitdiff
2005-01-28 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
authorajs <ajs>
Fri, 28 Jan 2005 20:28:35 +0000 (20:28 +0000)
committerajs <ajs>
Fri, 28 Jan 2005 20:28:35 +0000 (20:28 +0000)
* lib/command.h: Document behavior of argv_concat function.
* lib/command.c: (argv_concat) Calculate total string length first so
  we can call malloc just once (instead of realloc'ing to add each
  string element).
  (do_echo,config_logmsg) Allow for possible NULL return value from
  argv_concat.

lib/ChangeLog
lib/command.c
lib/command.h

index e3b7d377e819d485828d20310de4ba0b09cb5798..750e310f9aec135979289d13a4ea763c32b75869 100644 (file)
@@ -1,3 +1,12 @@
+2005-01-28 Andrew J. Schorr <ajschorr@alumni.princeton.edu>
+
+       * lib/command.h: Document behavior of argv_concat function.
+       * lib/command.c: (argv_concat) Calculate total string length first so
+         we can call malloc just once (instead of realloc'ing to add each
+         string element).
+         (do_echo,config_logmsg) Allow for possible NULL return value from
+         argv_concat.
+
 2005-01-23 Hasso Tepper <hasso at quagga.net>
 
        * lib/command.[ch]: Make node_parent() function nonstatic. vtyh.c will
index 6b65a5d2d46295b053da3669d56d22f37e0fdccd..be38fcc009c1093875ed32944cc2ea73b9796dd2 100644 (file)
@@ -1,5 +1,5 @@
 /*
-   $Id: command.c,v 1.35 2005/01/23 21:42:25 hasso Exp $
+   $Id: command.c,v 1.36 2005/01/28 20:28:35 ajs Exp $
 
    Command interpreter routine for virtual terminal [aka TeletYpe]
    Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
@@ -157,31 +157,24 @@ char *
 argv_concat (const char **argv, int argc, int shift)
 {
   int i;
-  int len;
-  int index;
+  size_t len;
   char *str;
+  char *p;
 
-  str = NULL;
-  index = 0;
-
+  len = 0;
+  for (i = shift; i < argc; i++)
+    len += strlen(argv[i])+1;
+  if (!len)
+    return NULL;
+  p = str = XMALLOC(MTYPE_TMP, len);
   for (i = shift; i < argc; i++)
     {
-      len = strlen (argv[i]);
-
-      if (i == shift)
-       {
-         str = XSTRDUP (MTYPE_TMP, argv[i]);
-         index = len;
-       }
-      else
-       {
-         str = XREALLOC (MTYPE_TMP, str, (index + len + 2));
-         str[index++] = ' ';
-         memcpy (str + index, argv[i], len);
-         index += len;
-         str[index] = '\0';
-       }
+      size_t arglen;
+      memcpy(p, argv[i], (arglen = strlen(argv[i])));
+      p += arglen;
+      *p++ = ' ';
     }
+  *(p-1) = '\0';
   return str;
 }
 
@@ -3008,8 +3001,10 @@ DEFUN_HIDDEN (do_echo,
 {
   char *message;
 
-  vty_out (vty, "%s%s",(message = argv_concat(argv, argc, 0)), VTY_NEWLINE);
-  XFREE(MTYPE_TMP, message);
+  vty_out (vty, "%s%s", ((message = argv_concat(argv, argc, 0)) ? message : ""),
+          VTY_NEWLINE);
+  if (message)
+    XFREE(MTYPE_TMP, message);
   return CMD_SUCCESS;
 }
 
@@ -3026,8 +3021,9 @@ DEFUN (config_logmsg,
   if ((level = level_match(argv[0])) == ZLOG_DISABLED)
     return CMD_ERR_NO_MATCH;
 
-  zlog(NULL, level, (message = argv_concat(argv, argc, 1)));
-  XFREE(MTYPE_TMP, message);
+  zlog(NULL, level, ((message = argv_concat(argv, argc, 1)) ? message : ""));
+  if (message)
+    XFREE(MTYPE_TMP, message);
   return CMD_SUCCESS;
 }
 
index 8fdec98ff3b05e28733f3302c39f5d9eb5cce495..eba919cbdf62991bdd805f988ee1cb86bd1f78e7 100644 (file)
@@ -322,7 +322,11 @@ void install_default (enum node_type);
 void install_element (enum node_type, struct cmd_element *);
 void sort_node ();
 
-char *argv_concat (const char **, int, int);
+/* Concatenates argv[shift] through argv[argc-1] into a single NUL-terminated
+   string with a space between each element (allocated using
+   XMALLOC(MTYPE_TMP)).  Returns NULL if shift >= argc. */
+char *argv_concat (const char **argv, int argc, int shift);
+
 vector cmd_make_strvec (const char *);
 void cmd_free_strvec (vector);
 vector cmd_describe_command ();