xgetlarg.c 723 B

1234567891011121314151617181920212223242526272829303132333435
  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. extern 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. if ((isspace)(*arg)) { /* Use an actual funciton call for minimal size. */
  20. bb_show_usage();
  21. }
  22. errno = 0;
  23. result = strtol(arg, &endptr, base);
  24. if (errno != 0 || *endptr!='\0' || endptr==arg || result < lower || result > upper)
  25. bb_show_usage();
  26. errno = errno_save;
  27. return result;
  28. }