News for hackers. Stuff that matters.
Re:The abominable strtok() by __david__ on Friday September 24, 2010 @04:25PM (#1725286)
First off, strtok() is dead, long live strsep().

Secondly, this is C code--it's pretty low-level. Yes, strsep() messes with your input. But that is very well documented. If you don't want it to, strdup() beforehand (or strndup() if you don't trust the input).

His whole example of strtok() dying on a character constant is stupid--why on earth would you do that? If you've got a string constant you may as well just have the constant array and save yourself the parsing headache.

This isn't rocket science.

Re:The abominable strtok() by dfox on Friday September 24, 2010 @06:00PM (#1725584)
I tend to use and recommend str(c)spn, which althought more low-level are more flexible and often what you want. And also you don't have to modify input if you don't want to.
Re:The abominable strtok() by tptacek on Friday September 24, 2010 @08:43PM (#1725954)
strcspn is really, really fussy. When what you want is the semantic equivalent of "foo,bar, baz".split(','), I don't think it's worth the potential for error, or the tangliness of the code.
Re:The abominable strtok() by scdlbx on Friday September 24, 2010 @04:26PM (#1725288)
Always use strndup(). Never trust your input.
Re:The abominable strtok() by apenwarr on Friday September 24, 2010 @05:24PM (#1725494)
If you're trying to strdup() something you're not even sure is null-terminated, you're already doomed.
Re:The abominable strtok() by scott_s on Friday September 24, 2010 @05:28PM (#1725503)
The parent said to use strndup(), which takes a max value of characters to copy.
Re:The abominable strtok() by CamperBob on Friday September 24, 2010 @05:49PM (#1725549)
And adds a \0 after the characters copied, which means you have to remember to do the sizeof(dest)-1 thing to avoid an overwrite.

The people who designed the C string functions should have been made to pee in a cup before they were allowed into the building. So much trouble could have been avoided if they had only been consistent about using memory block sizes as parameters rather than "string" (sic) lengths.

Re:The abominable strtok() by __david__ on Friday September 24, 2010 @08:19PM (#1725904)
What are you talking about? There is no sizeof(dest) with strndup()--it's newly allocated memory sized appropriately so there's no chance of an overwrite.
Re:The abominable strtok() by tptacek on Friday September 24, 2010 @08:44PM (#1725956)
While we're on the subject, what is the point of strndup()? Keeping people from running you out of memory with big long strings? They'll just find other ways to do the same thing (by repeatedly feeding you strings of threshold length).
Re:The abominable strtok() by CamperBob on Friday September 24, 2010 @11:44PM (#1726230)
(Actually it was strncpy's wacky behavior I was thinking of. I don't think I've ever used strndup().)
Re:The abominable strtok() by scott_s on Friday September 24, 2010 @05:54PM (#1725567)
Which is annoying, yes, but using strndup() negates apenwarr's point.