vsprintf considered dangerous

der Mouse mouse at mcgill-vision.UUCP
Tue Apr 12 19:13:45 AEST 1988


In article <18746 at think.UUCP>, bromley at think.COM (Mark Bromley) writes:
> In article <1219 at ucsfcca.ucsf.edu> roland at rtsg.lbl.gov (Roland McGrath) writes:
>> The GNU C library has such a function.  It's called "vsnprintf" and
>> it takes a maximum-length argument (there's also an "snprintf").
> [...would prefer something like] an sopen function, which is given a
> buffer and its size and returns a stream in which i/o is done
> directly to the buffer.

I did this, though I called it fopenstr.  Very nice when you have a
function that takes a FILE * argument and outputs something to it, or
reads something from it, and you want it to talk to a string instead.
I also added

unfdopen()
	Undoes the effects of fdopen, that is, shut down a stdio stream
	without close(2)ing the underlying file descriptor.  Can't
	understand why it wasn't there to begin with.

fopenfxn()
	Creates a stream which calls a function to perform I/O.  For a
	read stream, the function should return one byte at a time; for
	a write stream, it gets called with each byte in turn.
	Extremely useful in some circumstances.  For example,  you have
	error messages generated in pieces all over the place and you
	want to prefix each line of error output with the program name
	and process ID (say).  Rather than try to figure out which of
	the error fprintfs are at the beginnings of lines, you just
	create a function stream whose function keeps a state variable
	and prints out the extra info when stuff is printed after a
	newline.

I also tossed vprintf, vfprintf, and vsprintf into a Berkeley stdio
(they were just popcorn routines though, they don't really count).

					der Mouse

			uucp: mouse at mcgill-vision.uucp
			arpa: mouse at larry.mcrcim.mcgill.edu



More information about the Comp.lang.c mailing list