+/* Convert VALUE, which is converted from a system integer type whose
+ minimum value is MINVAL and maximum MINVAL, to an decimal
+ integer string. Use the storage in BUF and return a pointer to the
+ converted string. If VALUE is converted from a negative integer in
+ the range MINVAL .. -1, represent it with a string representation
+ of the negative integer, using leading '-'. */
+#if ! (INTMAX_MAX <= UINTMAX_MAX / 2)
+# error "sysinttostr: uintmax_t cannot represent all intmax_t values"
+#endif
+char *
+sysinttostr (uintmax_t value, intmax_t minval, uintmax_t maxval,
+ char buf[SYSINT_BUFSIZE])
+{
+ if (value <= maxval)
+ return umaxtostr (value, buf);
+ else
+ {
+ intmax_t i = value - minval;
+ return imaxtostr (i + minval, buf);
+ }
+}
+
+/* Convert a prefix of the string ARG to a system integer type whose
+ minimum value is MINVAL and maximum MAXVAL. If MINVAL is negative,
+ negative integers MINVAL .. -1 are assumed to be represented using
+ leading '-' in the usual way. If the represented value exceeds
+ INTMAX_MAX, return a negative integer V such that (uintmax_t) V
+ yields the represented value. If ARGLIM is nonnull, store into
+ *ARGLIM a pointer to the first character after the prefix.
+
+ This is the inverse of sysinttostr.
+
+ On a normal return, set errno = 0.
+ On conversion error, return 0 and set errno = EINVAL.
+ On overflow, return an extreme value and set errno = ERANGE. */
+#if ! (INTMAX_MAX <= UINTMAX_MAX)
+# error "strtosysint: nonnegative intmax_t does not fit in uintmax_t"
+#endif
+intmax_t
+strtosysint (char const *arg, char **arglim, intmax_t minval, uintmax_t maxval)
+{
+ errno = 0;
+ if (maxval <= INTMAX_MAX)
+ {
+ if (ISDIGIT (arg[*arg == '-']))
+ {
+ intmax_t i = strtoimax (arg, arglim, 10);
+ intmax_t imaxval = maxval;
+ if (minval <= i && i <= imaxval)
+ return i;
+ errno = ERANGE;
+ return i < minval ? minval : maxval;
+ }
+ }
+ else
+ {
+ if (ISDIGIT (*arg))
+ {
+ uintmax_t i = strtoumax (arg, arglim, 10);
+ if (i <= maxval)
+ return represent_uintmax (i);
+ errno = ERANGE;
+ return maxval;
+ }
+ }
+
+ errno = EINVAL;
+ return 0;
+}
+