xstrtol.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /* A more useful interface to strtol.
  2. Copyright (C) 1995-1996, 1998-1999, 2001-2004, 2006-2015 Free Software
  3. Foundation, Inc.
  4. This program is free software: you can redistribute it and/or modify
  5. it under the terms of the GNU General Public License as published by
  6. the Free Software Foundation; either version 3 of the License, or
  7. (at your option) any later version.
  8. This program is distributed in the hope that it will be useful,
  9. but WITHOUT ANY WARRANTY; without even the implied warranty of
  10. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  11. GNU General Public License for more details.
  12. You should have received a copy of the GNU General Public License
  13. along with this program. If not, see <http://www.gnu.org/licenses/>. */
  14. #ifndef XSTRTOL_H_
  15. # define XSTRTOL_H_ 1
  16. # include <getopt.h>
  17. # include <inttypes.h>
  18. # ifndef _STRTOL_ERROR
  19. enum strtol_error
  20. {
  21. LONGINT_OK = 0,
  22. /* These two values can be ORed together, to indicate that both
  23. errors occurred. */
  24. LONGINT_OVERFLOW = 1,
  25. LONGINT_INVALID_SUFFIX_CHAR = 2,
  26. LONGINT_INVALID_SUFFIX_CHAR_WITH_OVERFLOW = (LONGINT_INVALID_SUFFIX_CHAR
  27. | LONGINT_OVERFLOW),
  28. LONGINT_INVALID = 4
  29. };
  30. typedef enum strtol_error strtol_error;
  31. # endif
  32. # define _DECLARE_XSTRTOL(name, type) \
  33. strtol_error name (const char *, char **, int, type *, const char *);
  34. _DECLARE_XSTRTOL (xstrtol, long int)
  35. _DECLARE_XSTRTOL (xstrtoul, unsigned long int)
  36. _DECLARE_XSTRTOL (xstrtoimax, intmax_t)
  37. _DECLARE_XSTRTOL (xstrtoumax, uintmax_t)
  38. #if HAVE_LONG_LONG_INT
  39. _DECLARE_XSTRTOL (xstrtoll, long long int)
  40. _DECLARE_XSTRTOL (xstrtoull, unsigned long long int)
  41. #endif
  42. /* Report an error for an invalid integer in an option argument.
  43. ERR is the error code returned by one of the xstrto* functions.
  44. Use OPT_IDX to decide whether to print the short option string "C"
  45. or "-C" or a long option string derived from LONG_OPTION. OPT_IDX
  46. is -2 if the short option "C" was used, without any leading "-"; it
  47. is -1 if the short option "-C" was used; otherwise it is an index
  48. into LONG_OPTIONS, which should have a name preceded by two '-'
  49. characters.
  50. ARG is the option-argument containing the integer.
  51. After reporting an error, exit with a failure status. */
  52. _Noreturn void xstrtol_fatal (enum strtol_error,
  53. int, char, struct option const *,
  54. char const *);
  55. #endif /* not XSTRTOL_H_ */