Source code of Windows XP (NT5)
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.

62 lines
1.6 KiB

  1. /***
  2. *atox.c - atoi and atol conversion
  3. *
  4. * Copyright (c) 1989-1997, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Converts a character string into an int or long.
  8. *
  9. *******************************************************************************/
  10. #include <cruntime.h>
  11. #include <stdlib.h>
  12. #include <ctype.h>
  13. /***
  14. *long atol(char *nptr) - Convert string to long
  15. *
  16. *Purpose:
  17. * Converts ASCII string pointed to by nptr to binary.
  18. * Overflow is not detected.
  19. *
  20. *Entry:
  21. * nptr = ptr to string to convert
  22. *
  23. *Exit:
  24. * return long int value of the string
  25. *
  26. *Exceptions:
  27. * None - overflow is not detected.
  28. *
  29. *******************************************************************************/
  30. long __cdecl atol(
  31. const char *nptr
  32. )
  33. {
  34. int c; /* current char */
  35. long total; /* current total */
  36. int sign; /* if '-', then negative, otherwise positive */
  37. /* skip whitespace */
  38. while ( isspace((int)(unsigned char)*nptr) )
  39. ++nptr;
  40. c = (int)(unsigned char)*nptr++;
  41. sign = c; /* save sign indication */
  42. if (c == '-' || c == '+')
  43. c = (int)(unsigned char)*nptr++; /* skip sign */
  44. total = 0;
  45. while (isdigit(c)) {
  46. total = 10 * total + (c - '0'); /* accumulate digit */
  47. c = (int)(unsigned char)*nptr++; /* get next char */
  48. }
  49. if (sign == '-')
  50. return -total;
  51. else
  52. return total; /* return result, negated if necessary */
  53. }