From: David Lamparter Date: Thu, 26 Jan 2017 20:57:46 +0000 (+0100) Subject: lib/tests: add cmd_hostname_set() X-Git-Tag: frr-3.0-branchpoint~58^2~2 X-Git-Url: https://git.puffer.fish/?a=commitdiff_plain;h=bff9c3e9d3270a524eee693f4d44cd83f13e9963;p=matthieu%2Ffrr.git lib/tests: add cmd_hostname_set() To make it possible for testcli to get a clean memory management bill. (Note: XFREE() is NULL-safe, just like free().) Signed-off-by: David Lamparter --- diff --git a/lib/command.c b/lib/command.c index 9485beddd9..510699f91b 100644 --- a/lib/command.c +++ b/lib/command.c @@ -1541,6 +1541,14 @@ DEFUN (show_startup_config, return CMD_SUCCESS; } +int +cmd_hostname_set (const char *hostname) +{ + XFREE (MTYPE_HOST, host.name); + host.name = hostname ? XSTRDUP (MTYPE_HOST, hostname) : NULL; + return CMD_SUCCESS; +} + /* Hostname configuration */ DEFUN (config_hostname, hostname_cmd, @@ -1556,11 +1564,7 @@ DEFUN (config_hostname, return CMD_WARNING; } - if (host.name) - XFREE (MTYPE_HOST, host.name); - - host.name = XSTRDUP (MTYPE_HOST, word->arg); - return CMD_SUCCESS; + return cmd_hostname_set (word->arg); } DEFUN (config_no_hostname, @@ -1570,10 +1574,7 @@ DEFUN (config_no_hostname, "Reset system's network name\n" "Host name of this router\n") { - if (host.name) - XFREE (MTYPE_HOST, host.name); - host.name = NULL; - return CMD_SUCCESS; + return cmd_hostname_set (NULL); } /* VTY interface password set. */ diff --git a/lib/command.h b/lib/command.h index 3c3c3ae370..8986703708 100644 --- a/lib/command.h +++ b/lib/command.h @@ -424,6 +424,8 @@ extern void cmd_terminate (void); extern void cmd_exit (struct vty *vty); extern int cmd_list_cmds (struct vty *vty, int do_permute); +extern int cmd_hostname_set (const char *hostname); + /* NOT safe for general use; call this only if DEV_BUILD! */ extern void grammar_sandbox_init (void); diff --git a/tests/common-cli.c b/tests/common-cli.c index dc1f052bc2..7f7d253e6c 100644 --- a/tests/common-cli.c +++ b/tests/common-cli.c @@ -72,7 +72,7 @@ main (int argc, char **argv) /* Library inits. */ cmd_init (1); - host.name = XSTRDUP(MTYPE_HOST,"test"); + cmd_hostname_set ("test"); vty_init (master); memory_init ();