Re: New version of function mkdirhier()
Svante Signell, le Fri 13 Jan 2012 10:33:23 +0100, a écrit :
> >From the man page of strsep:
> Be cautious when using this function. If you do use it, note that:
> * This function modifies its first argument.
> * This function cannot be used on constant strings.
If libtar is fine with it, it's not a problem
> * The identity of the delimiting character is lost.
It's not a problem for mkdirhier.
> (and you need the extra library libbsd)
Not a problem for libtar.
> strtok is recommended, but additional problems here!
> * The strtok() function uses a static buffer while parsing, so it's
> not thread safe. Use strtok_r() if this matters to you.
Then strtok_r could be used instead.
> > Using strncat OTOH tends to confuse people, the len param is a
> > limiter on the src not the dest string. I see you made that mistake
> > on the code. This implies that when the string has the same len as
> > passed, the call is equivalent to its strcat counterpart,
> and what is wrong with using strncat compared to strcat?
It does not bring any improvement, since it's equivalent in the end in
> > So I'd say, just switch the code to dynamically allocate the strings,
> > and let it use strlcat/strlcpy/strsep, etc.
> The problem with dynamic string allocation is that you cannot free
> strings being modified by the functions, like strsep.
Err, why not? mkdirhier does not need to return anything to the caller,
so it can allocate/free at will. Isn't it possible to simply use
src = strdup(path)?