blob: 9c249fb8cdbcc6634c8f354b8f8aa1c16fa0b071 (
plain)
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
43
44
45
46
|
#include "__stdio.h" // for __FILE
#include "stddef.h" // for NULL
#include <fcntl.h> // for O_WRONLY
#include <stdatomic.h> // for atomic_flag_clear
#include <stdio.h> // for vfprintf, vsnprintf, _IONBF, size_t, va_list
#include <string.h> // for memset
int vsnprintf(char *restrict s, size_t n, const char *restrict format, va_list ap)
{
int r;
struct __FILE stream;
if (n == 0) {
return 0;
}
if (s == NULL) {
return -1;
}
memset(&stream, 0, sizeof(stream));
stream.fd = -1;
stream.flags = O_WRONLY;
stream.type = _IONBF;
atomic_flag_clear(&stream.lock);
stream.buf = s;
stream.buf_size = n;
stream.buf_len = 0;
stream.buf_pos = 0;
stream.eof = 0;
stream.unget_cnt = 0;
stream.offset = 0;
stream.next = NULL;
r = vfprintf(&stream, format, ap);
if (stream.buf_len < n) {
s[stream.buf_len] = '\0';
} else if (n > 0) {
s[n - 1] = '\0';
r = n - 1;
}
return r;
}
|