Mirror of strace – the linux syscall tracer
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

string_to_uint.c 773B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /*
  2. * Copyright (c) 2001-2018 The strace developers.
  3. * All rights reserved.
  4. *
  5. * SPDX-License-Identifier: LGPL-2.1-or-later
  6. */
  7. #ifdef HAVE_CONFIG_H
  8. # include "config.h"
  9. #endif
  10. #include <errno.h>
  11. #include <stdlib.h>
  12. #include <string.h>
  13. #include "string_to_uint.h"
  14. long long
  15. string_to_uint_ex(const char *const str, char **const endptr,
  16. const unsigned long long max_val,
  17. const char *const accepted_ending)
  18. {
  19. char *end;
  20. long long val;
  21. if (!*str)
  22. return -1;
  23. errno = 0;
  24. val = strtoll(str, &end, 10);
  25. if (str == end || val < 0 || (unsigned long long) val > max_val
  26. || (val == LLONG_MAX && errno == ERANGE))
  27. return -1;
  28. if (*end && (!accepted_ending || !strchr(accepted_ending, *end)))
  29. return -1;
  30. if (endptr)
  31. *endptr = end;
  32. return val;
  33. }