mirror of
https://gitlab.com/80486DX2-66/gists
synced 2025-05-31 08:31:41 +05:30
C: categorize files, update .gitignore
This commit is contained in:
66
c-programming/strings/asprintf.c
Normal file
66
c-programming/strings/asprintf.c
Normal file
@ -0,0 +1,66 @@
|
||||
/*
|
||||
* asprintf.c
|
||||
*
|
||||
* Author: Intel A80486DX2-66
|
||||
* License: Creative Commons Zero 1.0 Universal
|
||||
*/
|
||||
|
||||
#include "asprintf.h"
|
||||
|
||||
ssize_t asprintf(char** strp, char* format, ...) {
|
||||
va_list args;
|
||||
va_start(args, format);
|
||||
|
||||
ssize_t result = (ssize_t) vsnprintf(NULL, 0, format, args);
|
||||
if (result < 0) {
|
||||
va_end(args);
|
||||
return -1;
|
||||
}
|
||||
|
||||
size_t size = (size_t) result + 1;
|
||||
*strp = malloc(size * sizeof(char));
|
||||
if (*strp == NULL) {
|
||||
va_end(args);
|
||||
return -1;
|
||||
}
|
||||
|
||||
result = (ssize_t) vsnprintf(*strp, size, format, args);
|
||||
va_end(args);
|
||||
|
||||
if (result < 0) {
|
||||
free(*strp);
|
||||
return -1;
|
||||
}
|
||||
|
||||
return result;
|
||||
}
|
||||
|
||||
#ifdef TEST
|
||||
# include <inttypes.h>
|
||||
# include <math.h>
|
||||
# include <stdbool.h>
|
||||
# define PRINT_ASSERT(caption, test) do { \
|
||||
bool test_result = (test); \
|
||||
printf("[test] " caption ": %s\n", test_result ? "OK" : "failed"); \
|
||||
if (test_result == false) \
|
||||
exit(EXIT_FAILURE); \
|
||||
} while (0);
|
||||
|
||||
int main(void) {
|
||||
const char* world = " world";
|
||||
|
||||
char* output;
|
||||
size_t result = asprintf(&output, "Hello%s! -- Unsigned 32-bit type is "
|
||||
"constrained to lie\nwithin the range of integer values from "
|
||||
"%" PRIu32 " to %" PRIu32 " inclusive.", world, 0,
|
||||
(uint32_t) (powf(2.0f, 32.0f) - 1.0f));
|
||||
|
||||
PRINT_ASSERT("segmentation fault", true);
|
||||
PRINT_ASSERT("no errors", errno == 0);
|
||||
PRINT_ASSERT("zero termination test", output[result] == '\0');
|
||||
|
||||
printf("[asprintf (%zu)] '%s'\n", result, output);
|
||||
|
||||
return 0;
|
||||
}
|
||||
#endif
|
Reference in New Issue
Block a user