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
34
35
36
37
38
39
40
41
42
|
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
struct sn_cookie {
char *s;
size_t n;
};
#define MIN(a, b) ((a) < (b) ? (a) : (b))
size_t sn_write(FILE *f, const unsigned char *s, const size_t l) {
struct sn_cookie *c = f->cookie;
size_t k = MIN(l, c->n);
memcpy(c->s, s, k);
c->s += k;
c->n -= k;
*(c->s) = '\0';
// Upon successful completion, the snprintf() function shall return the number
// of bytes that would be written to s had n been sufficiently large excluding
// the terminating null byte.
return l;
}
int vsnprintf(char *str, size_t size, const char *format, va_list ap) {
char dummy[1];
struct sn_cookie c = {.s = (size ? str : dummy), .n = (size ? size - 1 : 0)};
FILE f = {
.write = sn_write,
.cookie = &c,
};
return vfprintf(&f, format, ap);
}
int snprintf(char *str, size_t size, const char *format, ...) {
va_list ap;
va_start(ap, format);
int rc = vsnprintf(str, size, format, ap);
va_end(ap);
return rc;
}
|