strtok_r.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455
  1. /* Copyright (C) 1991 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 Library General Public License as
  5. published by the Free Software Foundation; either version 2 of the
  6. 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. Library General Public License for more details.
  11. You should have received a copy of the GNU Library General Public
  12. License along with the GNU C Library; see the file COPYING.LIB. If
  13. not, write to the Free Software Foundation, Inc., 675 Mass Ave,
  14. Cambridge, MA 02139, USA. */
  15. /*
  16. * Modified by Manuel Novoa III Mar 1, 2001
  17. *
  18. * Converted original strtok.c code of strtok to __strtok_r.
  19. * Cleaned up logic and reduced code size.
  20. */
  21. #include <string.h>
  22. char *__strtok_r(char *s, const char *delim, char **save_ptr)
  23. {
  24. char *token;
  25. token = 0; /* Initialize to no token. */
  26. if (s == 0) { /* If not first time called... */
  27. s = *save_ptr; /* restart from where we left off. */
  28. }
  29. if (s != 0) { /* If not finished... */
  30. *save_ptr = 0;
  31. s += strspn(s, delim); /* Skip past any leading delimiters. */
  32. if (*s != '\0') { /* We have a token. */
  33. token = s;
  34. *save_ptr = strpbrk(token, delim); /* Find token's end. */
  35. if (*save_ptr != 0) {
  36. /* Terminate the token and make SAVE_PTR point past it. */
  37. *(*save_ptr)++ = '\0';
  38. }
  39. }
  40. }
  41. return token;
  42. }
  43. weak_alias(__strtok_r, strtok_r);