breakage.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  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. /*
  24. * We have to multiply 'ret' by 10 before absorbing the next digit.
  25. * If this will overflow, catch it now.
  26. */
  27. if (ret && (((ULONG_MAX + 10) / ret) < 10))
  28. return 0;
  29. ret *= 10;
  30. if (!isdigit(*str))
  31. return 0;
  32. buf[0] = *str;
  33. ret += atoi(buf);
  34. str++;
  35. }
  36. *val = ret;
  37. return 1;
  38. #endif
  39. }
  40. #ifndef HAVE_STRSTR
  41. char *int_strstr(const char *haystack, const char *needle)
  42. {
  43. const char *sub_haystack = haystack, *sub_needle = needle;
  44. unsigned int offset = 0;
  45. if (!needle)
  46. return haystack;
  47. if (!haystack)
  48. return NULL;
  49. while ((*sub_haystack != '\0') && (*sub_needle != '\0')) {
  50. if (sub_haystack[offset] == sub_needle) {
  51. /* sub_haystack is still a candidate */
  52. offset++;
  53. sub_needle++;
  54. } else {
  55. /* sub_haystack is no longer a possibility */
  56. sub_haystack++;
  57. offset = 0;
  58. sub_needle = needle;
  59. }
  60. }
  61. if (*sub_haystack == '\0')
  62. /* Found nothing */
  63. return NULL;
  64. return sub_haystack;
  65. }
  66. #endif