breakage.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #include "tunala.h"
  2. int int_strtoul(const char *str, unsigned long *val)
  3. {
  4. #ifdef HAVE_STRTOUL
  5. char *tmp;
  6. unsigned long ret = strtoul(str, &tmp, 10);
  7. if((str == tmp) || (*tmp != '\0'))
  8. /* The value didn't parse cleanly */
  9. return 0;
  10. if(ret == ULONG_MAX)
  11. /* We hit a limit */
  12. return 0;
  13. *val = ret;
  14. return 1;
  15. #else
  16. char buf[2];
  17. unsigned long ret = 0;
  18. buf[1] = '\0';
  19. if(str == '\0')
  20. /* An empty string ... */
  21. return 0;
  22. while(*str != '\0') {
  23. /* We have to multiply 'ret' by 10 before absorbing the next
  24. * digit. If this will overflow, catch it now. */
  25. if(ret && (((ULONG_MAX + 10) / ret) < 10))
  26. return 0;
  27. ret *= 10;
  28. if(!isdigit(*str))
  29. return 0;
  30. buf[0] = *str;
  31. ret += atoi(buf);
  32. str++;
  33. }
  34. *val = ret;
  35. return 1;
  36. #endif
  37. }
  38. #ifndef HAVE_STRSTR
  39. char *int_strstr(const char *haystack, const char *needle)
  40. {
  41. const char *sub_haystack = haystack, *sub_needle = needle;
  42. unsigned int offset = 0;
  43. if(!needle)
  44. return haystack;
  45. if(!haystack)
  46. return NULL;
  47. while((*sub_haystack != '\0') && (*sub_needle != '\0')) {
  48. if(sub_haystack[offset] == sub_needle) {
  49. /* sub_haystack is still a candidate */
  50. offset++;
  51. sub_needle++;
  52. } else {
  53. /* sub_haystack is no longer a possibility */
  54. sub_haystack++;
  55. offset = 0;
  56. sub_needle = needle;
  57. }
  58. }
  59. if(*sub_haystack == '\0')
  60. /* Found nothing */
  61. return NULL;
  62. return sub_haystack;
  63. }
  64. #endif