strsep.c 1.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. /* Copyright (C) 2004, 2007, 2009, 2010 Free Software Foundation, Inc.
  2. Written by Yoann Vandoorselaere <yoann@prelude-ids.org>.
  3. This program is free software; you can redistribute it and/or modify
  4. it under the terms of the GNU General Public License as published by
  5. the Free Software Foundation; either version 3, or (at your option)
  6. any later version.
  7. This program 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
  10. GNU General Public License for more details.
  11. You should have received a copy of the GNU General Public License
  12. along with this program; if not, write to the Free Software Foundation,
  13. Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
  14. #ifdef HAVE_CONFIG_H
  15. # include <config.h>
  16. #endif
  17. /* Specification. */
  18. #include <string.h>
  19. char *
  20. strsep (char **stringp, const char *delim)
  21. {
  22. char *start = *stringp;
  23. char *ptr;
  24. if (start == NULL)
  25. return NULL;
  26. /* Optimize the case of no delimiters. */
  27. if (delim[0] == '\0')
  28. {
  29. *stringp = NULL;
  30. return start;
  31. }
  32. /* Optimize the case of one delimiter. */
  33. if (delim[1] == '\0')
  34. ptr = strchr (start, delim[0]);
  35. else
  36. /* The general case. */
  37. ptr = strpbrk (start, delim);
  38. if (ptr == NULL)
  39. {
  40. *stringp = NULL;
  41. return start;
  42. }
  43. *ptr = '\0';
  44. *stringp = ptr + 1;
  45. return start;
  46. }