PatchworkOS
Loading...
Searching...
No Matches
vsnprintf.c
Go to the documentation of this file.
1#include <stdio.h>
2
3#include "common/print.h"
4
5int vsnprintf(char* _RESTRICT s, size_t n, const char* _RESTRICT format, va_list arg)
6{
8 ctx.base = 0;
9 ctx.flags = 0;
10 ctx.maxChars = n;
11 ctx.totalChars = 0;
12 ctx.currentChars = 0;
13 ctx.buffer = s;
14 ctx.width = 0;
15 ctx.precision = EOF;
16 ctx.stream = NULL;
17 va_copy(ctx.arg, arg);
18
19 while (*format != '\0')
20 {
21 const char* rc;
22
23 if ((*format != '%') || ((rc = _print(format, &ctx)) == format))
24 {
25 if (ctx.totalChars < n)
26 {
27 s[ctx.totalChars] = *format;
28 }
29
30 ctx.totalChars++;
31 format++;
32 }
33 else
34 {
35 /* Continue parsing after conversion specifier */
36 format = rc;
37 }
38 }
39
40 if (ctx.totalChars < n)
41 {
42 s[ctx.totalChars] = '\0';
43 }
44
45 va_end(ctx.arg);
46 return ctx.totalChars;
47}
#define NULL
Pointer error value.
Definition NULL.h:23
#define _RESTRICT
Definition config.h:17
const char * _print(const char *spec, _format_ctx_t *ctx)
Definition print.c:561
#define va_copy(dest, src)
Definition stdarg.h:12
#define va_end(ap)
Definition stdarg.h:13
__builtin_va_list va_list
Definition stdarg.h:9
#define EOF
Definition stdio.h:25
va_list arg
Definition format.h:45
int64_t precision
Definition format.h:43
FILE * stream
Definition format.h:44
uint64_t maxChars
Definition format.h:38
uint64_t width
Definition format.h:42
_format_flags_t flags
Definition format.h:37
int32_t base
Definition format.h:36
uint64_t currentChars
Definition format.h:40
char * buffer
Definition format.h:41
uint64_t totalChars
Definition format.h:39
int vsnprintf(char *_RESTRICT s, size_t n, const char *_RESTRICT format, va_list arg)
Definition vsnprintf.c:5