+
+
+/*
+ * 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
+ : "?";
+}