/contrib/ntp/libntp/octtoint.c

https://bitbucket.org/freebsd/freebsd-head/ · C · 35 lines · 26 code · 5 blank · 4 comment · 10 complexity · ab12b8e29808821e212092c43de4a9cd MD5 · raw file

  1. /*
  2. * octtoint - convert an ascii string in octal to an unsigned
  3. * long, with error checking
  4. */
  5. #include <stdio.h>
  6. #include <ctype.h>
  7. #include "ntp_stdlib.h"
  8. int
  9. octtoint(
  10. const char *str,
  11. u_long *ival
  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) || *cp == '8' || *cp == '9')
  22. return 0;
  23. if (u >= 0x20000000)
  24. return 0; /* overflow */
  25. u <<= 3;
  26. u += *cp++ - '0'; /* ascii dependent */
  27. }
  28. *ival = u;
  29. return 1;
  30. }