diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 19:12:14 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-04-15 19:12:14 +0000 |
commit | 4b8a0f3f3dcf60dac2ce308ea08d413a535af29f (patch) | |
tree | 0f09c0ad2a4d0f535d89040a63dc3a866a6606e6 /mprintf.c | |
parent | Initial commit. (diff) | |
download | reprepro-81d3a2ce7e9c7d762cae2cc4d01ae11e2ed9b92d.tar.xz reprepro-81d3a2ce7e9c7d762cae2cc4d01ae11e2ed9b92d.zip |
Adding upstream version 5.4.4.upstream/5.4.4
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to '')
-rw-r--r-- | mprintf.c | 70 |
1 files changed, 70 insertions, 0 deletions
diff --git a/mprintf.c b/mprintf.c new file mode 100644 index 0000000..581d9ae --- /dev/null +++ b/mprintf.c @@ -0,0 +1,70 @@ +#include <config.h> + +#include <stdio.h> +#include <stdarg.h> +#include <string.h> +#include <unistd.h> +#include <stdlib.h> + +#include "mprintf.h" + +// TODO: check for asprintf in configure and +// write a replacement for such situations. + +char * mprintf(const char *fmt, ...) { + char *p; + int r; + va_list va; + + va_start(va, fmt); + r = vasprintf(&p, fmt, va); + va_end(va); + /* return NULL both when r is < 0 and when NULL was returned */ + if (r < 0) + return NULL; + else + return p; +} + +char * vmprintf(const char *fmt, va_list va) { + char *p; + int r; + + r = vasprintf(&p, fmt, va); + /* return NULL both when r is < 0 and when NULL was returned */ + if (r < 0) + return NULL; + else + return p; +} + +#ifndef HAVE_DPRINTF +int dprintf(int fd, const char *format, ...){ + char *buffer; + int ret; + + va_list va; + + va_start(va, format); + buffer = vmprintf(format, va); + va_end(va); + if (buffer == NULL) + return -1; + ret = write(fd, buffer, strlen(buffer)); + free(buffer); + return ret; +} +#endif + +#ifndef HAVE_STRNDUP +/* That's not the best possible strndup implementation, but it suffices for what + * it is used here */ +char *strndup(const char *str, size_t n) { + char *r = malloc(n+1); + if (r == NULL) + return r; + memcpy(r, str, n); + r[n] = '\0'; + return r; +} +#endif |