This is the mail archive of the
libc-alpha@sourceware.org
mailing list for the glibc project.
small but tough idea for a new function
- From: Patrice Schoumi <pschoumacher at hotmail dot com>
- To: glibc <libc-alpha at sourceware dot org>
- Date: Mon, 17 Sep 2012 15:13:38 +0000
- Subject: small but tough idea for a new function
Hello,
It is difficult for a beginner programmer to create its own function with variable argument ex : printf, sprintf etc !!
There exists a function : int sprintf( ...) but not char *ssprintf( ....) !!!
I found in the library GLBC code sprintf :
int ssprintf(char *s, const char *format, ...)
{
va_list arg;
int done;
va_start (arg, format);
done = vsprintf (s, format, arg);
va_end (arg);
return (done);
}
If you create this function : char *ssprintf(...
char *ssprintf(char *s, const char *format, ...)
{
va_list arg;
int done;
va_start (arg, format);
done = vsprintf (s, format, arg);
va_end (arg);
return ((char *)s);
}
This function return : s = programmeur string
With this function it is possible to transform :
A basic function int fct(char *) to a function with a variable argument int fct(char *s,char *fmt,...) with a simple #define !!!!!
My example:
#include <stdio.h>
#include <string.h>
#include <strings.h>
#include <stdarg.h>
// simple stderr print
int fct(char *s)
{
if( s )
{
fprintf(stderr,"%s");
return(0);
}
return(-1);
}
void main(void)
{
char text[80];
sprintf(text,"Hello glibc my name is : %s","fct");
fct( text );
}
// => My idea
char buff[1024];
#define newfct(format,....) fct( ssprintf(buff,format,__VA_ARGS__))
// In main() now you can write
int num=15;
newfct("Hello %s my name is : %s and num=%d ","glibc","newfct",num);
With char *ssprintf(...) any programmer can make a function with arguments !!
best regards,
www.llist.fr