strtok.c 2.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. /*-
  2. * SPDX-License-Identifier: BSD-3-Clause
  3. *
  4. * Copyright (c) 1998 Softweyr LLC. All rights reserved.
  5. *
  6. * strtok_r, from Berkeley strtok
  7. * Oct 13, 1998 by Wes Peters <wes@softweyr.com>
  8. *
  9. * Copyright (c) 1988, 1993
  10. * The Regents of the University of California. All rights reserved.
  11. *
  12. * Redistribution and use in source and binary forms, with or without
  13. * modification, are permitted provided that the following conditions
  14. * are met:
  15. * 1. Redistributions of source code must retain the above copyright
  16. * notices, this list of conditions and the following disclaimer.
  17. * 2. Redistributions in binary form must reproduce the above copyright
  18. * notices, this list of conditions and the following disclaimer in the
  19. * documentation and/or other materials provided with the distribution.
  20. * 3. Neither the name of the University nor the names of its contributors
  21. * may be used to endorse or promote products derived from this software
  22. * without specific prior written permission.
  23. *
  24. * THIS SOFTWARE IS PROVIDED BY SOFTWEYR LLC, THE REGENTS AND CONTRIBUTORS
  25. * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
  26. * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
  27. * PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL SOFTWEYR LLC, THE
  28. * REGENTS, OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
  29. * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED
  30. * TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
  31. * PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
  32. * LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
  33. * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
  34. * SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include <string.h>
  37. char *
  38. strtok_r(char *s, const char *delim, char **last)
  39. {
  40. char *spanp, *tok;
  41. int c, sc;
  42. if (s == NULL && (s = *last) == NULL)
  43. return (NULL);
  44. /*
  45. * Skip (span) leading delimiters (s += strspn(s, delim), sort of).
  46. */
  47. cont:
  48. c = *s++;
  49. for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
  50. if (c == sc)
  51. goto cont;
  52. }
  53. if (c == 0) { /* no non-delimiter characters */
  54. *last = NULL;
  55. return (NULL);
  56. }
  57. tok = s - 1;
  58. /*
  59. * Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
  60. * Note that delim must have one NUL; we stop if we see that, too.
  61. */
  62. for (;;) {
  63. c = *s++;
  64. spanp = (char *)delim;
  65. do {
  66. if ((sc = *spanp++) == c) {
  67. if (c == 0)
  68. s = NULL;
  69. else
  70. s[-1] = '\0';
  71. *last = s;
  72. return (tok);
  73. }
  74. } while (sc != 0);
  75. }
  76. /* NOTREACHED */
  77. }