diff options
author | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-04-23 17:11:20 +0300 |
---|---|---|
committer | Evgeny Grin (Karlson2k) <k2k@narod.ru> | 2022-04-23 17:11:20 +0300 |
commit | 5c1e130e09b76f7a77dad883bf7459f2ec2e3888 (patch) | |
tree | 3a38015fa1f79f193720ac43f869ec2018e494e7 | |
parent | ec2d818cc4975bef2f6d0c6f36ffd5bcf9ea4443 (diff) | |
download | libmicrohttpd-5c1e130e09b76f7a77dad883bf7459f2ec2e3888.tar.gz libmicrohttpd-5c1e130e09b76f7a77dad883bf7459f2ec2e3888.zip |
doc/examples: avoid usage of unportable functions
-rw-r--r-- | doc/examples/sessions.c | 76 |
1 files 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 @@ | |||
1 | /* Feel free to use this example code in any way | 1 | /* Feel free to use this example code in any way |
2 | you see fit (Public Domain) */ | 2 | you see fit (Public Domain) */ |
3 | 3 | ||
4 | /* needed for asprintf */ | ||
5 | #ifndef _GNU_SOURCE | ||
6 | #define _GNU_SOURCE 1 | ||
7 | #endif | ||
8 | |||
9 | #include <stdlib.h> | 4 | #include <stdlib.h> |
10 | #include <string.h> | 5 | #include <string.h> |
11 | #include <stdio.h> | 6 | #include <stdio.h> |
12 | #include <errno.h> | 7 | #include <errno.h> |
13 | #include <time.h> | 8 | #include <time.h> |
9 | #include <stdarg.h> | ||
14 | #include <microhttpd.h> | 10 | #include <microhttpd.h> |
15 | 11 | ||
16 | #if defined _WIN32 && ! defined(__MINGW64_VERSION_MAJOR) | 12 | /* Emulate 'asprintf()', as it is not portable */ |
17 | static int | 13 | static int |
18 | asprintf (char **resultp, const char *format, ...) | 14 | MHD_asprintf (char **resultp, const char *format, ...) |
19 | { | 15 | { |
20 | va_list argptr; | 16 | va_list argptr; |
21 | char *result = NULL; | 17 | va_list argcopy; |
22 | int len = 0; | 18 | int len; |
23 | 19 | int ret; | |
24 | if (format == NULL) | ||
25 | return -1; | ||
26 | 20 | ||
21 | ret = -1; | ||
27 | va_start (argptr, format); | 22 | va_start (argptr, format); |
28 | 23 | ||
29 | len = _vscprintf ((char *) format, argptr); | 24 | va_copy (argcopy, argptr); |
30 | if (len >= 0) | 25 | #ifndef _WIN32 |
26 | len = vsnprintf (NULL, 0, format, argcopy); | ||
27 | #else | ||
28 | len = _vscprintf (format, argcopy); | ||
29 | #endif | ||
30 | va_end (argcopy); | ||
31 | if (0 < len) | ||
31 | { | 32 | { |
32 | len += 1; | 33 | size_t buf_size; |
33 | result = (char *) malloc (sizeof (char *) * len); | 34 | char *buf; |
34 | if (result != NULL) | 35 | |
36 | buf_size = len + 1; | ||
37 | buf = (char *) malloc (buf_size * sizeof(char)); | ||
38 | if (NULL != buf) | ||
35 | { | 39 | { |
36 | int len2 = _vscprintf ((char *) format, argptr); | 40 | int res; |
37 | if ((len2 != len - 1) || (len2 <= 0)) | 41 | |
42 | #ifndef _WIN32 | ||
43 | res = vsnprintf (buf, buf_size, format, argptr); | ||
44 | #else | ||
45 | res = _vsnprintf (buf, buf_size, format, argptr); | ||
46 | #endif | ||
47 | if (len == res) | ||
38 | { | 48 | { |
39 | free (result); | 49 | *resultp = buf; |
40 | result = NULL; | 50 | ret = res; |
41 | len = -1; | ||
42 | } | 51 | } |
43 | else | 52 | else |
44 | { | 53 | { |
45 | len = len2; | 54 | free (buf); |
46 | if (resultp) | 55 | *resultp = NULL; |
47 | *resultp = result; | ||
48 | } | 56 | } |
49 | } | 57 | } |
50 | } | 58 | } |
51 | va_end (argptr); | 59 | va_end (argptr); |
52 | return len; | 60 | return ret; |
53 | } | 61 | } |
54 | 62 | ||
55 | 63 | ||
56 | #endif | ||
57 | |||
58 | /** | 64 | /** |
59 | * Invalid method page. | 65 | * Invalid method page. |
60 | */ | 66 | */ |
@@ -343,9 +349,9 @@ fill_v1_form (const void *cls, | |||
343 | struct MHD_Response *response; | 349 | struct MHD_Response *response; |
344 | (void) cls; /* Unused */ | 350 | (void) cls; /* Unused */ |
345 | 351 | ||
346 | if (-1 == asprintf (&reply, | 352 | if (-1 == MHD_asprintf (&reply, |
347 | MAIN_PAGE, | 353 | MAIN_PAGE, |
348 | session->value_1)) | 354 | session->value_1)) |
349 | { | 355 | { |
350 | /* oops */ | 356 | /* oops */ |
351 | return MHD_NO; | 357 | return MHD_NO; |
@@ -385,10 +391,10 @@ fill_v1_v2_form (const void *cls, | |||
385 | struct MHD_Response *response; | 391 | struct MHD_Response *response; |
386 | (void) cls; /* Unused */ | 392 | (void) cls; /* Unused */ |
387 | 393 | ||
388 | if (-1 == asprintf (&reply, | 394 | if (-1 == MHD_asprintf (&reply, |
389 | SECOND_PAGE, | 395 | SECOND_PAGE, |
390 | session->value_1, | 396 | session->value_1, |
391 | session->value_2)) | 397 | session->value_2)) |
392 | { | 398 | { |
393 | /* oops */ | 399 | /* oops */ |
394 | return MHD_NO; | 400 | return MHD_NO; |