elemental-ircd/libratbox/src/snprintf.c

95 lines
2.0 KiB
C
Raw Normal View History

/*
* Modified and hacked into libratbox by Aaron Sethman <androsyn@ratbox.org>
* The original headers are below..
* Note that this implementation does not process floating point numbers so
* you will likely need to fall back to using sprintf yourself to do those...
*/
/*
* linux/lib/vsprintf.c
*
* Copyright (C) 1991, 1992 Linus Torvalds
*/
/* vsprintf.c -- Lars Wirzenius & Linus Torvalds. */
/*
* Wirzenius wrote this portably, Torvalds fucked it up :-)
*/
/*
* Fri Jul 13 2001 Crutcher Dunnavant <crutcher+kernel@datastacks.com>
* - changed to provide snprintf and vsnprintf functions
* So Feb 1 16:51:32 CET 2004 Juergen Quade <quade@hsnr.de>
* - scnprintf and vscnprintf
*/
#include <libratbox_config.h>
#include <ratbox_lib.h>
2014-12-16 05:07:22 +00:00
#include <string.h>
/*
2014-12-16 05:07:22 +00:00
* vsprintf_append()
* appends sprintf formatted string to the end of the buffer
*/
int
2014-12-16 05:07:22 +00:00
vsprintf_append(char *str, const char *format, va_list ap)
{
size_t x = strlen(str);
2014-12-16 05:07:22 +00:00
return (vsprintf(str + x, format, ap) + x);
}
/*
2014-12-16 05:07:22 +00:00
* sprintf_append()
* appends sprintf formatted string to the end of the buffer
*/
int
2014-12-16 05:07:22 +00:00
sprintf_append(char *str, const char *format, ...)
{
int x;
va_list ap;
va_start(ap, format);
2014-12-16 05:07:22 +00:00
x = vsprintf_append(str, format, ap);
va_end(ap);
return (x);
}
/*
2014-12-16 05:07:22 +00:00
* vsnprintf_append()
* appends sprintf formatted string to the end of the buffer but not
* exceeding len
*/
int
2014-12-16 05:07:22 +00:00
vsnprintf_append(char *str, size_t len, const char *format, va_list ap)
{
size_t x;
if(len == 0)
return 0;
x = strlen(str);
if(len < x) {
str[len - 1] = '\0';
return len - 1;
}
2014-12-16 05:07:22 +00:00
return (vsnprintf(str + x, len - x, format, ap) + x);
}
/*
2014-12-16 05:07:22 +00:00
* snprintf_append()
* appends snprintf formatted string to the end of the buffer but not
* exceeding len
*/
int
2014-12-16 05:07:22 +00:00
nprintf_append(char *str, size_t len, const char *format, ...)
{
int x;
va_list ap;
va_start(ap, format);
2014-12-16 05:07:22 +00:00
x = vsnprintf_append(str, len, format, ap);
va_end(ap);
return (x);
}