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.
diff --git a/lib/command.c b/lib/command.c
index 6b65a5d..be38fcc 100644
--- a/lib/command.c
+++ b/lib/command.c
@@ -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 @@
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 @@
{
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 @@
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;
}