LWN.net Logo

strcpy() / strlcpy() / asprintf()

strcpy() / strlcpy() / asprintf()

Posted Mar 30, 2012 17:50 UTC (Fri) by nix (subscriber, #2304)
In reply to: strcpy() / strlcpy() / asprintf() by nybble41
Parent article: A turning point for GNU libc

Ah yes, of course if you had a vsnprintf() you can do it. I forgot about that. (I spent way too long on deprived platforms with either no snprintf() or none that worked.)


(Log in to post comments)

strcpy() / strlcpy() / asprintf()

Posted Mar 31, 2012 21:10 UTC (Sat) by lacos (subscriber, #70616) [Link]

You can still implement it easily (but perhaps not too elegantly): vfprintf() the stuff to "/dev/null", and use the return value for allocation. Both the vfprintf() function and the /dev/null special file are mandated by SUSv1 (UNIX(R) 95) and possibly by earlier standards.

(SUSv1 doesn't have snprintf(). Once I needed to compile a glibc-oriented program on OSF/1 4.0E, which one might have consider a reference implementation of SUSv1, even though it wasn't formally certified. The program needed snprintf() and there was none, so I printed the string first to /dev/null (for the size), then fully to a malloc()'d area, then copied the bytes that had room, then free()'d the area. ... Sorry if this sounds trivial and/or retarded :))

strcpy() / strlcpy() / asprintf()

Posted Apr 5, 2012 8:36 UTC (Thu) by nix (subscriber, #2304) [Link]

Never thought of using vfprintf(). That's a good idea. Ah well. It would surely be more elegant than the longjmp() thing.

Copyright © 2013, Eklektix, Inc.
Comments and public postings are copyrighted by their creators.
Linux is a registered trademark of Linus Torvalds