strsep.c 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. /* Copyright (C) 1992, 93, 96, 97, 98, 99, 2004 Free Software Foundation, Inc.
  2. This file is part of the GNU C Library.
  3. The GNU C Library is free software; you can redistribute it and/or
  4. modify it under the terms of the GNU Lesser General Public
  5. License as published by the Free Software Foundation; either
  6. version 2.1 of the License, or (at your option) any later version.
  7. The GNU C Library is distributed in the hope that it will be useful,
  8. but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  10. Lesser General Public License for more details.
  11. You should have received a copy of the GNU Lesser General Public
  12. License along with the GNU C Library; if not, write to the Free
  13. Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
  14. 02111-1307 USA. */
  15. #include <string.h>
  16. #ifdef __USE_BSD
  17. libc_hidden_proto(strchr)
  18. libc_hidden_proto(strpbrk)
  19. libc_hidden_proto(strsep)
  20. char *strsep (char **stringp, const char *delim)
  21. {
  22. char *begin, *end;
  23. begin = *stringp;
  24. if (begin == NULL)
  25. return NULL;
  26. /* A frequent case is when the delimiter string contains only one
  27. character. Here we don't need to call the expensive `strpbrk'
  28. function and instead work using `strchr'. */
  29. if (delim[0] == '\0' || delim[1] == '\0')
  30. {
  31. char ch = delim[0];
  32. if (ch == '\0')
  33. end = NULL;
  34. else
  35. {
  36. if (*begin == ch)
  37. end = begin;
  38. else if (*begin == '\0')
  39. end = NULL;
  40. else
  41. end = strchr (begin + 1, ch);
  42. }
  43. }
  44. else
  45. /* Find the end of the token. */
  46. end = strpbrk (begin, delim);
  47. if (end)
  48. {
  49. /* Terminate the token and set *STRINGP past NUL character. */
  50. *end++ = '\0';
  51. *stringp = end;
  52. }
  53. else
  54. /* No more delimiters; this is the last token. */
  55. *stringp = NULL;
  56. return begin;
  57. }
  58. libc_hidden_def(strsep)
  59. #endif