]> git.ipfire.org Git - thirdparty/haproxy.git/commitdiff
MINOR: add list_append_word function
authorMaxime de Roucy <maxime.deroucy@gmail.com>
Fri, 13 May 2016 21:52:54 +0000 (23:52 +0200)
committerWilly Tarreau <w@1wt.eu>
Fri, 13 May 2016 22:00:54 +0000 (00:00 +0200)
int list_append_word(struct list *li, const char *str, char **err)

Append a copy of string <str> (inside a wordlist) at the end of
the list <li>.
The caller is responsible for freeing the <err> and <str> copy memory
area using free().

On failure : return 0 and <err> filled with an error message.

include/common/standard.h
src/standard.c

index cd2208ca1b0119b12b3f79a416833043b19e3e89..f123f1a41dfb5ae7021f27f0eee34a3b72fa539e 100644 (file)
@@ -1089,4 +1089,12 @@ static inline unsigned long long rdtsc()
 }
 #endif
 
+/* append a copy of string <str> (in a wordlist) at the end of the list <li>
+ * On failure : return 0 and <err> filled with an error message.
+ * The caller is responsible for freeing the <err> and <str> copy
+ * memory area using free()
+ */
+struct list;
+int list_append_word(struct list *li, const char *str, char **err);
+
 #endif /* _COMMON_STANDARD_H */
index a4d20978a02e3427c3cc6f1549235534905b7df7..cfed94d24fbe01c830e2f1978925bec042197d6f 100644 (file)
@@ -3439,6 +3439,38 @@ unsigned char utf8_next(const char *s, int len, unsigned int *c)
        return code | ((p-(unsigned char *)s)&0x0f);
 }
 
+/* append a copy of string <str> (in a wordlist) at the end of the list <li>
+ * On failure : return 0 and <err> filled with an error message.
+ * The caller is responsible for freeing the <err> and <str> copy
+ * memory area using free()
+ */
+int list_append_word(struct list *li, const char *str, char **err)
+{
+       struct wordlist *wl;
+
+       wl = calloc(1, sizeof(*wl));
+       if (!wl) {
+               memprintf(err, "out of memory");
+               goto fail_wl;
+       }
+
+       wl->s = strdup(str);
+       if (!wl->s) {
+               memprintf(err, "out of memory");
+               goto fail_wl_s;
+       }
+
+       LIST_ADDQ(li, &wl->list);
+
+       return 1;
+
+fail_wl_s:
+       free(wl->s);
+fail_wl:
+       free(wl);
+       return 0;
+}
+
 /*
  * Local variables:
  *  c-indent-level: 8