[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
[libmicrohttpd] 15/26: doc/examples: avoid usage of unportable functions
From: |
gnunet |
Subject: |
[libmicrohttpd] 15/26: doc/examples: avoid usage of unportable functions |
Date: |
Mon, 25 Apr 2022 15:08:39 +0200 |
This is an automated email from the git hooks/post-receive script.
karlson2k pushed a commit to branch master
in repository libmicrohttpd.
commit 5c1e130e09b76f7a77dad883bf7459f2ec2e3888
Author: Evgeny Grin (Karlson2k) <k2k@narod.ru>
AuthorDate: Sat Apr 23 17:11:20 2022 +0300
doc/examples: avoid usage of unportable functions
---
doc/examples/sessions.c | 76 ++++++++++++++++++++++++++-----------------------
1 file changed, 41 insertions(+), 35 deletions(-)
diff --git a/doc/examples/sessions.c b/doc/examples/sessions.c
index fc4a498c..0c8e5114 100644
--- a/doc/examples/sessions.c
+++ b/doc/examples/sessions.c
@@ -1,60 +1,66 @@
/* Feel free to use this example code in any way
you see fit (Public Domain) */
-/* needed for asprintf */
-#ifndef _GNU_SOURCE
-#define _GNU_SOURCE 1
-#endif
-
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <errno.h>
#include <time.h>
+#include <stdarg.h>
#include <microhttpd.h>
-#if defined _WIN32 && ! defined(__MINGW64_VERSION_MAJOR)
+/* Emulate 'asprintf()', as it is not portable */
static int
-asprintf (char **resultp, const char *format, ...)
+MHD_asprintf (char **resultp, const char *format, ...)
{
va_list argptr;
- char *result = NULL;
- int len = 0;
-
- if (format == NULL)
- return -1;
+ va_list argcopy;
+ int len;
+ int ret;
+ ret = -1;
va_start (argptr, format);
- len = _vscprintf ((char *) format, argptr);
- if (len >= 0)
+ va_copy (argcopy, argptr);
+#ifndef _WIN32
+ len = vsnprintf (NULL, 0, format, argcopy);
+#else
+ len = _vscprintf (format, argcopy);
+#endif
+ va_end (argcopy);
+ if (0 < len)
{
- len += 1;
- result = (char *) malloc (sizeof (char *) * len);
- if (result != NULL)
+ size_t buf_size;
+ char *buf;
+
+ buf_size = len + 1;
+ buf = (char *) malloc (buf_size * sizeof(char));
+ if (NULL != buf)
{
- int len2 = _vscprintf ((char *) format, argptr);
- if ((len2 != len - 1) || (len2 <= 0))
+ int res;
+
+#ifndef _WIN32
+ res = vsnprintf (buf, buf_size, format, argptr);
+#else
+ res = _vsnprintf (buf, buf_size, format, argptr);
+#endif
+ if (len == res)
{
- free (result);
- result = NULL;
- len = -1;
+ *resultp = buf;
+ ret = res;
}
else
{
- len = len2;
- if (resultp)
- *resultp = result;
+ free (buf);
+ *resultp = NULL;
}
}
}
va_end (argptr);
- return len;
+ return ret;
}
-#endif
-
/**
* Invalid method page.
*/
@@ -343,9 +349,9 @@ fill_v1_form (const void *cls,
struct MHD_Response *response;
(void) cls; /* Unused */
- if (-1 == asprintf (&reply,
- MAIN_PAGE,
- session->value_1))
+ if (-1 == MHD_asprintf (&reply,
+ MAIN_PAGE,
+ session->value_1))
{
/* oops */
return MHD_NO;
@@ -385,10 +391,10 @@ fill_v1_v2_form (const void *cls,
struct MHD_Response *response;
(void) cls; /* Unused */
- if (-1 == asprintf (&reply,
- SECOND_PAGE,
- session->value_1,
- session->value_2))
+ if (-1 == MHD_asprintf (&reply,
+ SECOND_PAGE,
+ session->value_1,
+ session->value_2))
{
/* oops */
return MHD_NO;
--
To stop receiving notification emails like this one, please contact
gnunet@gnunet.org.
- [libmicrohttpd] 06/26: websocket_threaded_example: mark function as static, (continued)
- [libmicrohttpd] 06/26: websocket_threaded_example: mark function as static, gnunet, 2022/04/25
- [libmicrohttpd] 07/26: basicauth: very minor optimisaion, gnunet, 2022/04/25
- [libmicrohttpd] 05/26: Fixed old-style functions definitions, gnunet, 2022/04/25
- [libmicrohttpd] 13/26: doc/examples: do not use non-literals for printf(), gnunet, 2022/04/25
- [libmicrohttpd] 19/26: md5.c: muted compiler alignment warnings, gnunet, 2022/04/25
- [libmicrohttpd] 10/26: microhttpd2.h: fixed code style, gnunet, 2022/04/25
- [libmicrohttpd] 11/26: test_shutdown_select: fixed code style, gnunet, 2022/04/25
- [libmicrohttpd] 08/26: configure: changed warning compiler flags, gnunet, 2022/04/25
- [libmicrohttpd] 14/26: doc/examples: improved formatting, gnunet, 2022/04/25
- [libmicrohttpd] 12/26: src/examples: fixed printf format specifiers, gnunet, 2022/04/25
- [libmicrohttpd] 15/26: doc/examples: avoid usage of unportable functions,
gnunet <=
- [libmicrohttpd] 09/26: Fixed many macros, gnunet, 2022/04/25
- [libmicrohttpd] 20/26: sha256.c: fixed coding style, gnunet, 2022/04/25
- [libmicrohttpd] 26/26: test_postprocessor: fixed typo, gnunet, 2022/04/25
- [libmicrohttpd] 22/26: sha1.c: fixed coding style, gnunet, 2022/04/25
- [libmicrohttpd] 24/26: MHD_get_daemon_info(): fixed unaligned memory access, gnunet, 2022/04/25
- [libmicrohttpd] 16/26: daemon.c: fixed possible unaligned access to 'struct sockaddr_in', gnunet, 2022/04/25
- [libmicrohttpd] 23/26: sha1.c: muted alignment warnings, gnunet, 2022/04/25
- [libmicrohttpd] 25/26: Removed unneeded anymore alignment workarounds., gnunet, 2022/04/25
- [libmicrohttpd] 18/26: mhd_bithelpers: minor improvement of internal macros, gnunet, 2022/04/25
- [libmicrohttpd] 17/26: MHD_get_connection_info(): Fixed possible unaligned access, gnunet, 2022/04/25