_print/_doprnt
chris at umcp-cs.UUCP
chris at umcp-cs.UUCP
Sun Jul 8 11:12:42 AEST 1984
As long as we're getting into *printf:
Here's a bunch of things I'd like to see in the stdio library:
a) sprintfl (or some similar name): like sprintf, but takes a maximum
buffer length specification. For example:
char buf[N];
.
.
.
sprintfl(buf, N, "%s", foo);
Avoiding overrun with plain ol' sprintf() is a pain.
b) v*printf versions of everything on Berkeley Unix(tm)
c) _IOSTRG flag working properly. At least in BSD, _IOSTRG is
*never* checked (not once!) so if you try to write sprintfl as
/* sprintfl - vax version */
char *
sprintfl(buf, len, fmt, arg)
register char *buf;
register int len;
char *fmt;
{
struct _iobuf _strbuf;
strbuf._flag = _IOWRT | _IOSTRG;
strbuf._cnt = len - 1;
strbuf._ptr = buf;
_doprnt(fmt, &arg, _strbuf);
buf[len - 1] = '\0';
return buf;
}
and call sprintfl with a `len' of (say) 50 and a fmt+arglist
that comes out to more than 50 characters, guess what happens?
_flsbuf writes the first 49 characters [len-1, remember?] to
whatever file descriptor happens to be in _strbuf._file, or
other similarly nasty things (the exact behaviour depends on
what is in _strbuf._base).
This means that if you pass a sufficient amount of stuff to
sprintf, it will break, since it sets strbuf._flag as above
(but sets strbuf._len to 32767).
The (temporary) way around this is to leave out _IOWRT, which
makes _flsbuf return an error, which makes _doprnt quit, which
is essentially what is desired.
--
In-Real-Life: Chris Torek, Univ of MD Comp Sci (301) 454-7690
UUCP: {seismo,allegra,brl-bmd}!umcp-cs!chris
CSNet: chris at umcp-cs ARPA: chris at maryland
More information about the Comp.unix.wizards
mailing list