Path: utzoo!mnetor!uunet!lll-winken!lll-tis!ames!mailrus!tut.cis.ohio-state.edu!bloom-beacon!mcgill-vision!mouse From: mouse@mcgill-vision.UUCP (der Mouse) Newsgroups: comp.lang.c Subject: Re: vsprintf considered dangerous Message-ID: <1047@mcgill-vision.UUCP> Date: 12 Apr 88 09:13:45 GMT References: <36@lotus.UUCP> <1219@ucsfcca.ucsf.edu> <18746@think.UUCP> Organization: McGill University, Montreal Lines: 37 In article <18746@think.UUCP>, bromley@think.COM (Mark Bromley) writes: > In article <1219@ucsfcca.ucsf.edu> roland@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@mcgill-vision.uucp arpa: mouse@larry.mcrcim.mcgill.edu