X-Git-Url: https://diplodocus.org/git/nmh/blobdiff_plain/0bb0a48241ac6130afe4cf8eb5616f2f33ee1a84..210b50a5ac585ae99db277fdae5638772df9c3d9:/sbr/utils.c diff --git a/sbr/utils.c b/sbr/utils.c index 8281b4fc..205c8592 100644 --- a/sbr/utils.c +++ b/sbr/utils.c @@ -11,6 +11,7 @@ #include "m_mktemp.h" #include "makedir.h" #include +#include extern char *mhdocdir; @@ -34,7 +35,7 @@ void *mh_xmalloc(size_t size) size = 1; /* Some mallocs don't like 0. */ p = malloc(size); if (!p) - adios(NULL, "malloc failed, size wanted: %zu", size); + adios(NULL, "malloc failed, size wanted: %lu", (unsigned long)size); return p; } @@ -54,7 +55,7 @@ void *mh_xrealloc(void *ptr, size_t size) new = realloc(ptr, size); if (!new) - adios(NULL, "realloc failed, size wanted: %zu", size); + adios(NULL, "realloc failed, size wanted: %lu", (unsigned long)size); return new; } @@ -69,7 +70,8 @@ void *mh_xcalloc(size_t nelem, size_t elsize) p = calloc(nelem, elsize); if (!p) - adios(NULL, "calloc failed, size wanted: %zu * %zu", nelem, elsize); + adios(NULL, "calloc failed, size wanted: %lu * %lu", + (unsigned long)nelem, (unsigned long)elsize); return p; } @@ -572,3 +574,32 @@ scan_input (int fd, int *eightbit) { return state == NOTOK ? NOTOK : OK; } + + +/* + * Convert an int to a char string. + */ +char * +m_str(int value) { + return m_strn(value, 0); +} + + +/* + * Convert an int to a char string, of limited width if > 0. + */ +#define STR(s) #s +/* SIZE(n) includes NUL. n must just be digits, not an equation. */ +#define SIZE(n) (sizeof STR(n)) + +char * +m_strn(int value, unsigned int width) { + /* Need to include space for negative sign. But don't use INT_MIN + because it could be a macro that would fool SIZE(n). */ + static char buffer[SIZE(-INT_MAX)]; + const int num_chars = snprintf(buffer, sizeof buffer, "%d", value); + + return num_chars > 0 && (width == 0 || (unsigned int) num_chars <= width) + ? buffer + : "?"; +}