2
0

strtok.c 2.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768
  1. /***************************************************************************
  2. * _ _ ____ _
  3. * Project ___| | | | _ \| |
  4. * / __| | | | |_) | |
  5. * | (__| |_| | _ <| |___
  6. * \___|\___/|_| \_\_____|
  7. *
  8. * Copyright (C) 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.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. * SPDX-License-Identifier: curl
  22. *
  23. ***************************************************************************/
  24. #include "curl_setup.h"
  25. #ifndef HAVE_STRTOK_R
  26. #include <stddef.h>
  27. #include "strtok.h"
  28. char *
  29. Curl_strtok_r(char *ptr, const char *sep, char **end)
  30. {
  31. if(!ptr)
  32. /* we got NULL input so then we get our last position instead */
  33. ptr = *end;
  34. /* pass all letters that are including in the separator string */
  35. while(*ptr && strchr(sep, *ptr))
  36. ++ptr;
  37. if(*ptr) {
  38. /* so this is where the next piece of string starts */
  39. char *start = ptr;
  40. /* set the end pointer to the first byte after the start */
  41. *end = start + 1;
  42. /* scan through the string to find where it ends, it ends on a
  43. null byte or a character that exists in the separator string */
  44. while(**end && !strchr(sep, **end))
  45. ++*end;
  46. if(**end) {
  47. /* the end is not a null byte */
  48. **end = '\0'; /* null-terminate it! */
  49. ++*end; /* advance the last pointer to beyond the null byte */
  50. }
  51. return start; /* return the position where the string starts */
  52. }
  53. /* we ended up on a null byte, there are no more strings to find! */
  54. return NULL;
  55. }
  56. #endif /* this was only compiled if strtok_r wasn't present */