]> git.ipfire.org Git - thirdparty/lldpd.git/commitdiff
asprintf: globally define _GNU_SOURCE
authorVincent Bernat <vbe@deezer.com>
Tue, 1 Apr 2014 16:29:07 +0000 (18:29 +0200)
committerVincent Bernat <vbe@deezer.com>
Tue, 1 Apr 2014 16:29:07 +0000 (18:29 +0200)
And also provide a replacement for asprintf if it is not available. I
would prefer to not use _GNU_SOURCE everywhere but it seems cleaner than
putting it at the top of random files. Ideally, I should wrap every
function in a separate file to just use _GNU_SOURCE for those
functions. There are `asprintf()` and `setres[ug]id()`.

13 files changed:
configure.ac
src/client/lldpcli.c
src/compat/asprintf.c [new file with mode: 0644]
src/compat/compat.h
src/daemon/lldpd.h
src/lib/fixedpoint.c
src/log.c
src/marshal.c
tests/check_cdp.c
tests/check_edp.c
tests/check_lldp.c
tests/check_sonmp.c
tests/common.c

index eefab1d6bc7c9bcefbe7de42781dbe3ddca88043..4ccd6ba3fb272399440fbcbe29853a6b32ef5f9a 100644 (file)
@@ -76,6 +76,7 @@ AX_CFLAGS_GCC_OPTION([-Winline])
 AX_CFLAGS_GCC_OPTION([-fstack-protector])
 AX_CFLAGS_GCC_OPTION([-fno-omit-frame-pointer])
 AX_CFLAGS_GCC_OPTION([-D_FORTIFY_SOURCE=2])
+AX_CFLAGS_GCC_OPTION([-D_GNU_SOURCE=1])
 AX_CFLAGS_GCC_OPTION([-Wno-unused-parameter])
 AX_CFLAGS_GCC_OPTION([-Wno-missing-field-initializers])
 AX_CFLAGS_GCC_OPTION([-Wno-sign-compare]) dnl Should be fixed later
@@ -106,7 +107,7 @@ AC_FUNC_FORK
 AC_SEARCH_LIBS([setproctitle], [util bsd])
 AC_REPLACE_FUNCS([setproctitle])
 AC_CHECK_FUNCS([setproctitle_init])
-AC_REPLACE_FUNCS([strlcpy strnlen strndup fgetln])
+AC_REPLACE_FUNCS([strlcpy strnlen strndup fgetln asprintf])
 AC_CHECK_FUNCS([setresuid setresgid])
 
 case " $LIBS " in
index 35c1b94d0d3ecc32005837f70e4a6ac7289140a5..be3d3622625e1060e820f4910fd09ec61981ca7e 100644 (file)
@@ -16,7 +16,6 @@
  */
 
 
-#define _GNU_SOURCE
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
diff --git a/src/compat/asprintf.c b/src/compat/asprintf.c
new file mode 100644 (file)
index 0000000..4fd3f94
--- /dev/null
@@ -0,0 +1,83 @@
+/*
+ * Copyright (c) 2004 Darren Tucker.
+ *
+ * Based originally on asprintf.c from OpenBSD:
+ * Copyright (c) 1997 Todd C. Miller <Todd.Miller@courtesan.com>
+ *
+ * Permission to use, copy, modify, and distribute this software for any
+ * purpose with or without fee is hereby granted, provided that the above
+ * copyright notice and this permission notice appear in all copies.
+ *
+ * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
+ * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
+ * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
+ * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
+ * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
+ * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
+ * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
+ */
+
+#include <errno.h>
+#include <stdio.h>
+#include <stdarg.h>
+#include <stdlib.h>
+#include <limits.h>
+
+#define INIT_SZ        128
+
+static int _vasprintf(char **str, const char *fmt, va_list ap)
+{
+       int ret = -1;
+       va_list ap2;
+       char *string, *newstr;
+       size_t len;
+
+       va_copy(ap2, ap);
+       if ((string = malloc(INIT_SZ)) == NULL)
+               goto fail;
+
+       ret = vsnprintf(string, INIT_SZ, fmt, ap2);
+       if (ret >= 0 && ret < INIT_SZ) { /* succeeded with initial alloc */
+               *str = string;
+       } else if (ret == INT_MAX || ret < 0) { /* Bad length */
+               free(string);
+               goto fail;
+       } else {        /* bigger than initial, realloc allowing for nul */
+               len = (size_t)ret + 1;
+               if ((newstr = realloc(string, len)) == NULL) {
+                       free(string);
+                       goto fail;
+               } else {
+                       va_end(ap2);
+                       va_copy(ap2, ap);
+                       ret = vsnprintf(newstr, len, fmt, ap2);
+                       if (ret >= 0 && (size_t)ret < len) {
+                               *str = newstr;
+                       } else { /* failed with realloc'ed string, give up */
+                               free(newstr);
+                               goto fail;
+                       }
+               }
+       }
+       va_end(ap2);
+       return (ret);
+
+fail:
+       *str = NULL;
+       errno = ENOMEM;
+       va_end(ap2);
+       return (-1);
+}
+
+int asprintf(char **str, const char *fmt, ...)
+{
+       va_list ap;
+       int ret;
+
+       *str = NULL;
+       va_start(ap, fmt);
+       ret = _vasprintf(str, fmt, ap);
+       va_end(ap);
+
+       return ret;
+}
index cf623fcaa451b99a60bffbc838421db1ef0a932c..e4cc969a9ab24c444ef82af9a4454d9028f28ba6 100644 (file)
 # include <bsd/unistd.h>
 #endif
 
+#if !HAVE_ASPRINTF
+int asprintf (char **, const char *, ...) __attribute__ ((format (printf, 2, 3)));
+#endif
+
 #if !HAVE_STRLCPY
 size_t strlcpy(char *, const char *, size_t);
 #endif
index fa66636abcd68da6a7d72c835e7cf3c0dfa203fb..f2b5f1f473b11074fff071e7325abb74bd6d7adb 100644 (file)
@@ -17,7 +17,6 @@
 
 #ifndef _LLDPD_H
 #define _LLDPD_H
-#define _GNU_SOURCE 1
 
 #if HAVE_CONFIG_H
 #  include <config.h>
index 7c99eef9f456af0e058e75f0310f5740c4f1fc97..155fad11e16ffd5f110a7b8d1336fd58b1f4f819 100644 (file)
@@ -15,7 +15,6 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#define _GNU_SOURCE 1
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
index 7bb128c43d1718e275bb1279bb0d0de398b1956d..5a1db2215d057467354f87b098544d311c4f2cc0 100644 (file)
--- a/src/log.c
+++ b/src/log.c
@@ -17,7 +17,6 @@
  * OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#define _GNU_SOURCE
 #include <unistd.h>
 #include <stdio.h>
 #include <stdlib.h>
index 943eb893ecc48d7dba2db2f29ceb40232d75beb7..16dabe574e1efa463bb278cb42bb16e2f0f45708 100644 (file)
@@ -15,7 +15,6 @@
  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
  */
 
-#define _GNU_SOURCE 1
 #define MARSHAL_EXPORT
 #include "marshal.h"
 
index cf6ce13fb18a93a4c89393c50da1be6370cc361b..237fce18880dab60335089270743648619550555 100644 (file)
@@ -1,4 +1,3 @@
-#define _GNU_SOURCE 1
 #include <stdlib.h>
 #include <sys/socket.h>
 #include <arpa/inet.h>
index 0f9905dd2e536efa0292f0a083d8dca975ecbf07..040d7820df262c7d09cf43db16318b07bbf21726 100644 (file)
@@ -1,4 +1,3 @@
-#define _GNU_SOURCE 1
 #include <stdlib.h>
 #include <sys/socket.h>
 #include <arpa/inet.h>
index b5d2a782a9c6dc0cebfe5225bc15c3ee9c427565..1b48372ba4a8e5e2b46f805ae9ecc683028503a0 100644 (file)
@@ -1,4 +1,3 @@
-#define _GNU_SOURCE 1
 #include <stdlib.h>
 #include <sys/socket.h>
 #include <arpa/inet.h>
index 596618d375833e7d57c0b49512ad36bb81a67793..b3cf73195ab7a99d43430d4f516986a097a2a42c 100644 (file)
@@ -1,4 +1,3 @@
-#define _GNU_SOURCE 1
 #include <stdlib.h>
 #include <sys/socket.h>
 #include <arpa/inet.h>
index 45869982deeeace84bc3500de54a571a1f4fbb5c..e1ecc7077f5cfa15f81d81a42ca398893f7139ca 100644 (file)
@@ -1,4 +1,3 @@
-#define _GNU_SOURCE 1
 #include <stdlib.h>
 #include <stdio.h>
 #include <unistd.h>