Rev | Line | |
---|
[1] | 1 | #include <stdarg.h> |
---|
| 2 | #include <unistd.h> |
---|
| 3 | #include <stdlib.h> |
---|
| 4 | #include <string.h> |
---|
| 5 | #include "dietstdio.h" |
---|
| 6 | |
---|
| 7 | struct str_data { |
---|
| 8 | char* str; |
---|
| 9 | size_t len; |
---|
| 10 | size_t size; |
---|
| 11 | }; |
---|
| 12 | |
---|
| 13 | static int swrite(void*ptr, size_t nmemb, struct str_data* sd) { |
---|
| 14 | size_t tmp=sd->size-sd->len; |
---|
| 15 | if (tmp>0) { |
---|
| 16 | size_t len=nmemb; |
---|
| 17 | if (len>tmp) len=tmp; |
---|
| 18 | if (sd->str) { |
---|
| 19 | memcpy(sd->str+sd->len,ptr,len); |
---|
| 20 | sd->str[sd->len+len]=0; |
---|
| 21 | } |
---|
| 22 | sd->len+=len; |
---|
| 23 | } |
---|
| 24 | return nmemb; |
---|
| 25 | } |
---|
| 26 | |
---|
| 27 | int vsnprintf(char* str, size_t size, const char *format, va_list arg_ptr) { |
---|
| 28 | int n; |
---|
| 29 | struct str_data sd = { str, 0, size?size-1:0 }; |
---|
| 30 | struct arg_printf ap = { &sd, (int(*)(void*,size_t,void*)) swrite }; |
---|
| 31 | n=__v_printf(&ap,format,arg_ptr); |
---|
| 32 | if (str && size && n>=0) { |
---|
| 33 | if (size!=(size_t)-1 && ((size_t)n>=size)) str[size-1]=0; |
---|
| 34 | else str[n]=0; |
---|
| 35 | } |
---|
| 36 | return n; |
---|
| 37 | } |
---|
Note: See
TracBrowser
for help on using the repository browser.