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.

strtoull.c 1.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960
  1. /*
  2. * Copyright (C) 2006 Michael Brown <mbrown@fensystems.co.uk>
  3. * Copyright (C) 2010 Piotr Jaroszyński <p.jaroszynski@gmail.com>
  4. *
  5. * This program is free software; you can redistribute it and/or
  6. * modify it under the terms of the GNU General Public License as
  7. * published by the Free Software Foundation; either version 2 of the
  8. * License, or any later version.
  9. *
  10. * This program is distributed in the hope that it will be useful, but
  11. * WITHOUT ANY WARRANTY; without even the implied warranty of
  12. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  13. * General Public License for more details.
  14. *
  15. * You should have received a copy of the GNU General Public License
  16. * along with this program; if not, write to the Free Software
  17. * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
  18. */
  19. FILE_LICENCE ( GPL2_OR_LATER );
  20. #include <stdlib.h>
  21. #include <ctype.h>
  22. /*
  23. * Despite being exactly the same as strtoul() except the long long instead of
  24. * long it ends up being much bigger so provide a separate implementation in a
  25. * separate object so that it won't be linked in if not used.
  26. */
  27. unsigned long long strtoull ( const char *p, char **endp, int base ) {
  28. unsigned long long ret = 0;
  29. int negative = 0;
  30. unsigned int charval;
  31. while ( isspace ( *p ) )
  32. p++;
  33. if ( *p == '-' ) {
  34. negative = 1;
  35. p++;
  36. }
  37. base = strtoul_base ( &p, base );
  38. while ( 1 ) {
  39. charval = strtoul_charval ( *p );
  40. if ( charval >= ( unsigned int ) base )
  41. break;
  42. ret = ( ( ret * base ) + charval );
  43. p++;
  44. }
  45. if ( negative )
  46. ret = -ret;
  47. if ( endp )
  48. *endp = ( char * ) p;
  49. return ( ret );
  50. }