2

The (BSD) man pages for strtok state in the first line under description

This interface is obsoleted by strsep(3).

Does that mean that strtok should on the whole not be used? It does not provide any usage guidelines about strtok's being obsolete beyond that. On strsep's man page, it states under history

The strsep() function is intended as a replacement for the strtok() function. While the strtok() function should be preferred for portability reasons (it conforms to ISO/IEC 9899:1990 (``ISO C90'')) it is unable to handle empty fields, i.e., detect fields delimited by two adjacent delimiter characters, or to be used for more than a single string at a time.

So, my questions are this

  1. Should strsep on the whole be used over strtok?
  2. How much of a portability issue is there with strsep as it appeared in BSD4.4 which was in June of 1994?
  3. Do all POSIX compliant machines have strsep?
Jonathan Leffler
  • 730,956
  • 141
  • 904
  • 1,278
Eli Sadoff
  • 7,173
  • 6
  • 33
  • 61
  • 2
    Do you want your code to be portable? Then you should keep using `strtok`. It is, after all, part of the C standard which `strsep` isn't. As an example, `strsep` doesn't exist on Windows using the Visual Studio run-time. – Some programmer dude Jan 07 '17 at 00:25
  • 3
    `strsep` is not a part of POSIX, so there may be a POSIX compliant OS out there that does not have `strsep`. – DYZ Jan 07 '17 at 00:27
  • I'm actually going to close this question. I just found an answer [here](http://stackoverflow.com/a/7219504/5021321). Sorry! – Eli Sadoff Jan 07 '17 at 00:28
  • "The man pages for strtok state in the first line under description This interface is obsoleted by strsep(3)." -- **What** manual pages? The one I'm looking at, http://man7.org/linux/man-pages/man3/strtok.3.html, doesn't say that. In any case, it's not true. Use strtok_r, not strsep or strtok. – Jim Balter Jan 07 '17 at 00:36
  • @JimBalter [BSD Man Pages](https://www.freebsd.org/cgi/man.cgi?query=strtok&apropos=0&sektion=0&manpath=FreeBSD+11.0-RELEASE+and+Ports&arch=default&format=html) – Eli Sadoff Jan 07 '17 at 00:37
  • In the future, please provide such links in your question ... it makes a world of difference. That BSD says that something is "obsoleted" is irrelevant outside of BSD. – Jim Balter Jan 07 '17 at 00:39
  • 1
    @JimBalter I did not know that it said otherwise outside of the BSD man pages until you mentioned it, but I will do so in the future. – Eli Sadoff Jan 07 '17 at 00:40
  • Well sure, you didn't know, that's why you're asking questions. :-) Again, that's why it's important to be specific and not just say "the" -- there is rarely a "the", there are numerous. – Jim Balter Jan 07 '17 at 00:41
  • In general, if the 'multiple separator' problem isn't a problem, you should use `strtok_s()` on Windows (technically, it's also a part of the Annex K Safer Functions in C11 — ISO/IEC 9899:2011 — but usually not available on Unix) and `strtok_r()` on Unix (POSIX). These are call compatible and take a pointer to extra space which fixes the reentrancy problem. If you need to detect empty fields between adjacent delimiter characters, the `strtok()` functions are not the correct functions to be using. If you don't need to detect empty fields, then `strsep()` is the wrong function to be using. – Jonathan Leffler Jan 07 '17 at 00:47
  • See also [Do you use the TR 24731 'safe' functions?](https://stackoverflow.com/questions/372980/do-you-use-the-tr-24731-safe-functions) – Jonathan Leffler Jan 07 '17 at 00:49

0 Answers0