xgetlarg.c 748 B

12345678910111213141516171819202122232425262728293031323334353637
  1. /* vi: set sw=4 ts=4: */
  2. /*
  3. * Copyright (C) 2003-2004 Erik Andersen <andersen@codepoet.org>
  4. */
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <getopt.h>
  8. #include <errno.h>
  9. #include <assert.h>
  10. #include <ctype.h>
  11. #include "busybox.h"
  12. long bb_xgetlarg(const char *arg, int base, long lower, long upper)
  13. {
  14. long result;
  15. char *endptr;
  16. int errno_save = errno;
  17. assert(arg!=NULL);
  18. /* Don't allow leading whitespace.
  19. * Wrap isspace in () to make sure we call the
  20. * function rather than the macro. */
  21. if ((isspace)(*arg)) {
  22. bb_show_usage();
  23. }
  24. errno = 0;
  25. result = strtol(arg, &endptr, base);
  26. if (errno != 0 || *endptr!='\0' || endptr==arg || result < lower || result > upper)
  27. bb_show_usage();
  28. errno = errno_save;
  29. return result;
  30. }