To follow myself up:

> > 
> > Any comments, suggestions, swears concerning adding a new function,
> > strndup(), to libc?
> > 
> > So that instead of permitting it to attempt to allocate a large
> > chunk of memory, it is possible to give it a max length.
> 
> How about just knowing what you are passing to the function
> before you call it, so that it isn't a problem?

There might actually be tiny value in such a function, but
not the way you've written it.

The value would be in avoiding the strlen() call prior to
the malloc(), which would permit a single traversal of the
string, relative to the standard strdup() function, which
always ens up doing two traversals (one to get the length,
and a second to do the copy).  See the strncpy() sources.

The downside risk would be that you could end up allocating
too much memory, e.g.:

        char    string[BIGFREAKINGARRAY];
        char    *s;

        ...

        s = strndup( string, BIGFREAKINGARRAY);

So it seems to me that the people who would want to use it
would probably end up shooting themselves anyway.

Something that seems more useful would be an asnprintf(),
and you could (ab)use it to get the same functionality, but
in this case "more useful" still doesn't mean "generally
useful".

-- Terry

To Unsubscribe: send mail to [EMAIL PROTECTED]
with "unsubscribe freebsd-hackers" in the body of the message

Reply via email to