]> err.no Git - varnish/commitdiff
Introduce a "replace()" function to replace a malloc'ed string.
authorphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Sun, 5 Aug 2007 19:52:23 +0000 (19:52 +0000)
committerphk <phk@d4fa192b-c00b-0410-8231-f00ffab90ce4>
Sun, 5 Aug 2007 19:52:23 +0000 (19:52 +0000)
git-svn-id: svn+ssh://projects.linpro.no/svn/varnish/trunk@1796 d4fa192b-c00b-0410-8231-f00ffab90ce4

varnish-cache/bin/varnishd/mgt_param.c

index 80d5eadceb0effab55a7e311d673a2c9fa7dd1ca..75d5e0a2c12aaa6b389a16bd5b9b36644d840ca2 100644 (file)
@@ -65,6 +65,18 @@ struct parspec {
 
 static struct params master;
 
+/* XXX: Far too generic to live here ? */
+static void
+replace(char **p, const char *q)
+{
+
+       AN(*q);
+       if (*p != NULL)
+               free(*p);
+       *p = strdup(q);
+       AN(*p);
+}
+
 /*--------------------------------------------------------------------*/
 
 static void
@@ -156,21 +168,14 @@ tweak_user(struct cli *cli, struct parspec *par, const char *arg)
                        cli_result(cli, CLIS_PARAM);
                        return;
                }
-               if (master.user)
-                       free(master.user);
-               master.user = strdup(pw->pw_name);
-               AN(master.user);
+               replace(&master.user, pw->pw_name);
                master.uid = pw->pw_uid;
 
                /* set group to user's primary group */
-               if (master.group)
-                       free(master.group);
                if ((gr = getgrgid(pw->pw_gid)) != NULL &&
                    (gr = getgrnam(gr->gr_name)) != NULL &&
-                   gr->gr_gid == pw->pw_gid) {
-                       master.group = strdup(gr->gr_name);
-                       AN(master.group);
-               }
+                   gr->gr_gid == pw->pw_gid) 
+                       replace(&master.group, gr->gr_name);
                master.gid = pw->pw_gid;
        } else if (master.user) {
                cli_out(cli, "%s (%d)", master.user, (int)master.uid);
@@ -193,10 +198,7 @@ tweak_group(struct cli *cli, struct parspec *par, const char *arg)
                        cli_result(cli, CLIS_PARAM);
                        return;
                }
-               if (master.group)
-                       free(master.group);
-               master.group = strdup(gr->gr_name);
-               AN(master.group);
+               replace(&master.group, gr->gr_name);
                master.gid = gr->gr_gid;
        } else if (master.group) {
                cli_out(cli, "%s (%d)", master.group, (int)master.gid);
@@ -428,9 +430,7 @@ tweak_listen_address(struct cli *cli, struct parspec *par, const char *arg)
                return;
        }
 
-       free(master.listen_address);
-       master.listen_address = strdup(arg);
-       AN(master.listen_address);
+       replace(&master.listen_address, arg);
 
        clean_listen_sock_head(&heritage.socks);
        heritage.nsocks = 0;