snprintf() and sxprintf()
PAD Powell
padpowell at wateng.UUCP
Fri Nov 23 23:09:18 AEST 1984
The snprintf() function is a well behaved version of the ill-mannered
sprintf() function, in the sense that it provides a bound on the lenght
of the buffer that is used. Unfortunately, without some additional
minor changes to _doprnt(), it is not TOTALLY problem free, but will at
least fail in a non-fatal manner.
In addition, sxprintf() is like snprintf(), but will not put a trailing
'\0' at the end. This makes it useful in doing "form-filling".
Patrick Powell
/* @(#)sprintf.c 4.2 (Waterloo) 19/08/84 */
#include <stdio.h>
/* snprintf- sprintf with bounds check */
/*LINTLIBRARY*/
/*VARARGS3*/
char *snprintf(count, str, fmt, args)
int count;
char *str, *fmt;
{
int s;
struct _iobuf _strbuf;
_strbuf._flag = _IOSTRG;
_strbuf._ptr = str;
_strbuf._cnt = count;
s = _doprnt(fmt, &args, &_strbuf);
if( s >= 0 ){
if( _strbuf._cnt > 0 ){
*_strbuf._ptr = 0;
} else {
s = -1;
}
}
return(s < 0? NULL : str );
}
/* sxprintf- exact count printf */
/*VARARGS3*/
char *sxprintf(count, str, fmt, args)
int count;
char *str, *fmt;
{
int s;
struct _iobuf _strbuf;
_strbuf._flag = _IOSTRG;
_strbuf._ptr = str;
_strbuf._cnt = count;
s = _doprnt(fmt, &args, &_strbuf);
return(s < 0? NULL : str );
}
More information about the Comp.unix.wizards
mailing list