strsep.c 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657
  1. /* Copyright (C) 2004, 2007, 2009-2015 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, see <http://www.gnu.org/licenses/>. */
  13. #ifdef HAVE_CONFIG_H
  14. # include <config.h>
  15. #endif
  16. /* Specification. */
  17. #include <string.h>
  18. char *
  19. strsep (char **stringp, const char *delim)
  20. {
  21. char *start = *stringp;
  22. char *ptr;
  23. if (start == NULL)
  24. return NULL;
  25. /* Optimize the case of no delimiters. */
  26. if (delim[0] == '\0')
  27. {
  28. *stringp = NULL;
  29. return start;
  30. }
  31. /* Optimize the case of one delimiter. */
  32. if (delim[1] == '\0')
  33. ptr = strchr (start, delim[0]);
  34. else
  35. /* The general case. */
  36. ptr = strpbrk (start, delim);
  37. if (ptr == NULL)
  38. {
  39. *stringp = NULL;
  40. return start;
  41. }
  42. *ptr = '\0';
  43. *stringp = ptr + 1;
  44. return start;
  45. }