From e9169a633d399c074c84c94d2f44a01eb1537b5a Mon Sep 17 00:00:00 2001 From: Philippe PITTOLI Date: Wed, 7 Sep 2016 23:46:00 +0200 Subject: [PATCH] pubsubd: channel copy improved --- lib/pubsubd.c | 36 ++++++++++++++++++++++++------------ lib/pubsubd.h | 1 + pubsub/test-chan-lists.c | 20 +++----------------- 3 files changed, 28 insertions(+), 29 deletions(-) diff --git a/lib/pubsubd.c b/lib/pubsubd.c index ed7e222..5fce9c7 100644 --- a/lib/pubsubd.c +++ b/lib/pubsubd.c @@ -59,15 +59,34 @@ struct channel * pubsubd_channel_copy (struct channel *c) memcpy (copy, c, sizeof(struct channel)); if (c->chan != NULL) { - // copy->chan = strndup (c->chan, c->chanlen); - copy->chan = malloc (BUFSIZ); - memcpy (copy->chan, c->chan, BUFSIZ); + copy->chan = malloc (c->chanlen); + memset (copy->chan, 0, c->chanlen); + memcpy (copy->chan, c->chan, c->chanlen); copy->chanlen = c->chanlen; } return copy; } +int pubsubd_channel_new (struct channel *c, const char * name) +{ + if (c == NULL) { + return 1; + } + + size_t nlen = (strlen (name) > BUFSIZ) ? BUFSIZ : strlen (name) + 1; + + printf ("NAME : %s, SIZE : %ld\n", name, nlen); + + if (c->chan == NULL) + c->chan = malloc (nlen); + + memset (c->chan, 0, nlen); + memcpy (c->chan, name, nlen); + c->chanlen = nlen; + return 0; +} + void pubsubd_channel_free (struct channel * c) { // TODO @@ -425,16 +444,9 @@ int pubsubd_get_new_process (struct service *srv, struct app_list_elm *ale *c = malloc (sizeof (struct channel)); } - if (c[0]->chan != NULL) { - free (c[0]->chan); - c[0]->chan = NULL; - } - chan[BUFSIZ -1] = '\0'; - // c[0]->chan = strndup (chan, BUFSIZ); - c[0]->chan = malloc (BUFSIZ); - memcpy(c[0]->chan, chan, BUFSIZ); - c[0]->chanlen = strlen (chan); + pubsubd_channel_new (*c, chan); + struct channel *new_chan = NULL; new_chan = pubsubd_channel_get (chans, *c); diff --git a/lib/pubsubd.h b/lib/pubsubd.h index 30f5336..c261ac7 100644 --- a/lib/pubsubd.h +++ b/lib/pubsubd.h @@ -60,6 +60,7 @@ struct channel { }; // simple channel +int pubsubd_channel_new (struct channel *c, const char *name); struct channel * pubsubd_channel_copy (struct channel *c); struct channel * pubsubd_channel_get (struct channels *chans, struct channel *c); void pubsubd_channel_free (struct channel *c); diff --git a/pubsub/test-chan-lists.c b/pubsub/test-chan-lists.c index fbcada6..826b64b 100644 --- a/pubsub/test-chan-lists.c +++ b/pubsub/test-chan-lists.c @@ -3,20 +3,6 @@ #define TEST_NAME "test-chan-lists" -int -create_chan (struct channel *c, const char * name) { - if (c == NULL) { - return 1; - } - - if (c->chan == NULL) - c->chan = malloc (BUFSIZ); - memset (c->chan, 0, BUFSIZ); - memcpy (c->chan, name, strlen (name)); - c->chanlen = strlen (name); - return 0; -} - void ohshit(int rvalue, const char* str) { fprintf(stderr, "%s\n", str); @@ -47,7 +33,7 @@ main(int argc, char **argv, char **env) // warning : this is a local structure, not exactly the same in the prog. struct channel chan; memset (&chan, 0, sizeof (struct channel)); - create_chan (&chan, "coucou"); + pubsubd_channel_new (&chan, "coucou"); // to emulate // pubsubd_get_new_process (&srv, &ale1, &chans, &chan); @@ -71,7 +57,7 @@ main(int argc, char **argv, char **env) printf ("--\n"); // SAME CHAN, SHOULD NOT BE ADDED - create_chan (&chan, "coucou"); + pubsubd_channel_new (&chan, "coucou"); // search for the chan in channels, add it if not found new_chan = pubsubd_channel_get (&chans, &chan); if (new_chan == NULL) { @@ -87,7 +73,7 @@ main(int argc, char **argv, char **env) printf ("--\n"); // NEW CHAN, SHOULD BE ADDED - create_chan (&chan, "salut"); + pubsubd_channel_new (&chan, "salut"); // search for the chan in channels, add it if not found new_chan = pubsubd_channel_get (&chans, &chan); if (new_chan == NULL) {