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.
This commit is contained in:
ajs 2005-01-28 20:28:35 +00:00
parent 8bfb0cba00
commit f6834d4c40
3 changed files with 35 additions and 26 deletions

View 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> 2005-01-23 Hasso Tepper <hasso at quagga.net>
* lib/command.[ch]: Make node_parent() function nonstatic. vtyh.c will * lib/command.[ch]: Make node_parent() function nonstatic. vtyh.c will

View 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] Command interpreter routine for virtual terminal [aka TeletYpe]
Copyright (C) 1997, 98, 99 Kunihiro Ishiguro Copyright (C) 1997, 98, 99 Kunihiro Ishiguro
@ -157,31 +157,24 @@ char *
argv_concat (const char **argv, int argc, int shift) argv_concat (const char **argv, int argc, int shift)
{ {
int i; int i;
int len; size_t len;
int index;
char *str; char *str;
char *p;
str = NULL; len = 0;
index = 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++) for (i = shift; i < argc; i++)
{ {
len = strlen (argv[i]); size_t arglen;
memcpy(p, argv[i], (arglen = strlen(argv[i])));
if (i == shift) p += arglen;
{ *p++ = ' ';
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';
}
} }
*(p-1) = '\0';
return str; return str;
} }
@ -3008,8 +3001,10 @@ DEFUN_HIDDEN (do_echo,
{ {
char *message; char *message;
vty_out (vty, "%s%s",(message = argv_concat(argv, argc, 0)), VTY_NEWLINE); vty_out (vty, "%s%s", ((message = argv_concat(argv, argc, 0)) ? message : ""),
XFREE(MTYPE_TMP, message); VTY_NEWLINE);
if (message)
XFREE(MTYPE_TMP, message);
return CMD_SUCCESS; return CMD_SUCCESS;
} }
@ -3026,8 +3021,9 @@ DEFUN (config_logmsg,
if ((level = level_match(argv[0])) == ZLOG_DISABLED) if ((level = level_match(argv[0])) == ZLOG_DISABLED)
return CMD_ERR_NO_MATCH; return CMD_ERR_NO_MATCH;
zlog(NULL, level, (message = argv_concat(argv, argc, 1))); zlog(NULL, level, ((message = argv_concat(argv, argc, 1)) ? message : ""));
XFREE(MTYPE_TMP, message); if (message)
XFREE(MTYPE_TMP, message);
return CMD_SUCCESS; return CMD_SUCCESS;
} }

View File

@ -322,7 +322,11 @@ void install_default (enum node_type);
void install_element (enum node_type, struct cmd_element *); void install_element (enum node_type, struct cmd_element *);
void sort_node (); 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 *); vector cmd_make_strvec (const char *);
void cmd_free_strvec (vector); void cmd_free_strvec (vector);
vector cmd_describe_command (); vector cmd_describe_command ();