1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
|
#include <stdarg.h>
#include <sys/types.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include "dietstdio.h"
struct str_data {
unsigned char* str;
size_t len;
size_t size;
};
static int swrite(void*ptr, size_t nmemb, struct str_data* sd) {
size_t tmp=sd->size-sd->len;
if (tmp>0) {
size_t len=nmemb;
if (len>tmp) len=tmp;
if (sd->str) {
memcpy(sd->str+sd->len,ptr,len);
sd->str[sd->len+len]=0;
}
sd->len+=len;
}
return nmemb;
}
int vsnprintf(char* str, size_t size, const char *format, va_list arg_ptr) {
struct str_data sd = { str, 0, size };
struct arg_printf ap = { &sd, (int(*)(void*,size_t,void*)) swrite };
if (size) --sd.size;
return __v_printf(&ap,format,arg_ptr);
}
|