/contrib/ntp/libntp/atouint.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 35 lines · 27 code · 5 blank · 3 comment · 9 complexity · ac32de88587f9ac542487850b30321ac MD5 · raw file

  1. /*
  2. * atouint - convert an ascii string to an unsigned long, with error checking
  3. */
  4. #include <sys/types.h>
  5. #include <ctype.h>
  6. #include "ntp_types.h"
  7. #include "ntp_stdlib.h"
  8. int
  9. atouint(
  10. const char *str,
  11. u_long *uval
  12. )
  13. {
  14. register u_long u;
  15. register const char *cp;
  16. cp = str;
  17. if (*cp == '\0')
  18. return 0;
  19. u = 0;
  20. while (*cp != '\0') {
  21. if (!isdigit((int)*cp))
  22. return 0;
  23. if (u > 429496729 || (u == 429496729 && *cp >= '6'))
  24. return 0; /* overflow */
  25. u = (u << 3) + (u << 1);
  26. u += *cp++ - '0'; /* ascii dependent */
  27. }
  28. *uval = u;
  29. return 1;
  30. }