strtok.c 2.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 1998 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
  9. *
  10. * This software is licensed as described in the file COPYING, which
  11. * you should have received as part of this distribution. The terms
  12. * are also available at https://curl.haxx.se/docs/copyright.html.
  13. *
  14. * You may opt to use, copy, modify, merge, publish, distribute and/or sell
  15. * copies of the Software, and permit persons to whom the Software is
  16. * furnished to do so, under the terms of the COPYING file.
  17. *
  18. * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
  19. * KIND, either express or implied.
  20. *
  21. ***************************************************************************/
  22. #include "curl_setup.h"
  23. #ifndef HAVE_STRTOK_R
  24. #include <stddef.h>
  25. #include "strtok.h"
  26. char *
  27. Curl_strtok_r(char *ptr, const char *sep, char **end)
  28. {
  29. if(!ptr)
  30. /* we got NULL input so then we get our last position instead */
  31. ptr = *end;
  32. /* pass all letters that are including in the separator string */
  33. while(*ptr && strchr(sep, *ptr))
  34. ++ptr;
  35. if(*ptr) {
  36. /* so this is where the next piece of string starts */
  37. char *start = ptr;
  38. /* set the end pointer to the first byte after the start */
  39. *end = start + 1;
  40. /* scan through the string to find where it ends, it ends on a
  41. null byte or a character that exists in the separator string */
  42. while(**end && !strchr(sep, **end))
  43. ++*end;
  44. if(**end) {
  45. /* the end is not a null byte */
  46. **end = '\0'; /* null-terminate it! */
  47. ++*end; /* advance the last pointer to beyond the null byte */
  48. }
  49. return start; /* return the position where the string starts */
  50. }
  51. /* we ended up on a null byte, there are no more strings to find! */
  52. return NULL;
  53. }
  54. #endif /* this was only compiled if strtok_r wasn't present */