From: David Levine Date: Sat, 29 Jul 2017 14:02:28 +0000 (-0400) Subject: Added m_str() and m_strn() functions to convert int to string. X-Git-Url: https://diplodocus.org/git/nmh/commitdiff_plain/1dd139e04f59bbfa3a310c2776c4bb2e2fdf1fdd?hp=6e1c913f4e0ced806180c7069ca12f79f15e5e23 Added m_str() and m_strn() functions to convert int to string. Allows better fix to uip/forw.c than commit d711510305. --- diff --git a/h/utils.h b/h/utils.h index 0c6c7347..944a3ccb 100644 --- a/h/utils.h +++ b/h/utils.h @@ -106,6 +106,19 @@ int contains8bit(const char *start, const char *end); */ int scan_input (int fd, int *eightbit); +/* + * Returns string representation of int, in static memory. + */ +char *m_str(int value); + +/* + * Returns string representation of an int, in static memory. If width + * == 0, does not limit the width. If width > 0 and value will not fit + * in field of that size, including any negative sign but excluding + * terminating null, then returns "?". If width < 0, returns "?". + */ +char *m_strn(int value, unsigned int width); + /* * program initialization * diff --git a/sbr/m_name.c b/sbr/m_name.c index a4b49af8..9bc8c0b5 100644 --- a/sbr/m_name.c +++ b/sbr/m_name.c @@ -7,6 +7,7 @@ #include #include +#include #define STR(s) #s #define SIZE(n) (sizeof STR(n)) /* Includes NUL. */ @@ -14,12 +15,7 @@ char * m_name (int num) { - static char name[SIZE(INT_MAX)]; + if (num <= 0) return "?"; - if (num <= 0) - return "?"; - - snprintf(name, sizeof name, "%d", num); - - return name; + return m_strn(num, SIZE(INT_MAX)); } diff --git a/sbr/utils.c b/sbr/utils.c index 8281b4fc..ef5116e8 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; @@ -572,3 +573,43 @@ 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 +#define SIZE(n) (sizeof STR(n)) /* Includes NUL. */ + +char * +m_strn(int value, unsigned int width) { + /* +1 to allow negative sign. */ + static char buffer[SIZE(INT_MAX) + 1]; + + if (width == 0) { + snprintf(buffer, sizeof buffer, "%d", value); + } else { + int max_val = 1; + unsigned int i; + for (i = 0; i < (value >= 0 ? width : width-1); ++i) { + max_val *= 10; + } + + if (abs(value) <= max_val && width > 0) { + snprintf(buffer, sizeof buffer, "%d", value); + } else { + snprintf(buffer, sizeof buffer, "%c", '?'); + } + } + + return buffer; +} diff --git a/uip/forw.c b/uip/forw.c index 331be16b..cb025541 100644 --- a/uip/forw.c +++ b/uip/forw.c @@ -454,9 +454,9 @@ try_it_again: if (digest) { snprintf (buf, sizeof(buf), IFORMAT, digest); - context_replace (buf, mh_xstrdup(m_name(issue))); + context_replace (buf, mh_xstrdup(m_str(issue))); snprintf (buf, sizeof(buf), VFORMAT, digest); - context_replace (buf, mh_xstrdup(m_name(volume))); + context_replace (buf, mh_xstrdup(m_str(volume))); } context_replace (pfolder, folder); /* update current folder */