PatchworkOS
Loading...
Searching...
No Matches
vfprintf.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <stdlib.h>
3
4#include "common/print.h"
5#include "user/common/file.h"
7
8int vfprintf(FILE* _RESTRICT stream, const char* _RESTRICT format, va_list arg)
9{
10 _format_ctx_t ctx;
11 ctx.base = 0;
12 ctx.flags = 0;
13 ctx.maxChars = SIZE_MAX;
14 ctx.totalChars = 0;
15 ctx.currentChars = 0;
16 ctx.buffer = NULL;
17 ctx.width = 0;
18 ctx.precision = EOF;
19 ctx.stream = stream;
20
21 mtx_lock(&stream->mtx);
22
23 if (_file_prepare_write(stream) == ERR)
24 {
25 mtx_unlock(&stream->mtx);
26 return EOF;
27 }
28
29 va_copy(ctx.arg, arg);
30
31 while (*format != '\0')
32 {
33 const char* rc;
34
35 if ((*format != '%') || ((rc = _print(format, &ctx)) == format))
36 {
37 /* No conversion specifier, print verbatim */
38 stream->buf[stream->bufIndex++] = *format;
39
40 if ((stream->bufIndex == stream->bufSize) || ((stream->flags & _FILE_LINE_BUFFERED) && (*format == '\n')) ||
41 (stream->flags & _FILE_UNBUFFERED))
42 {
43 if (_file_flush_buffer(stream) == ERR)
44 {
45 mtx_unlock(&stream->mtx);
46 return EOF;
47 }
48 }
49
50 ++format;
51 ctx.totalChars++;
52 }
53 else
54 {
55 /* Continue parsing after conversion specifier */
56 format = rc;
57 }
58 }
59
60 va_end(ctx.arg);
61 mtx_unlock(&stream->mtx);
62 return ctx.totalChars;
63}
#define NULL
Pointer error value.
Definition NULL.h:23
#define ERR
Integer error value.
Definition ERR.h:17
#define _RESTRICT
Definition config.h:17
uint64_t _file_prepare_write(FILE *stream)
Definition file.c:239
uint64_t _file_flush_buffer(FILE *stream)
Definition file.c:158
const char * _print(const char *spec, _format_ctx_t *ctx)
Definition print.c:561
@ _FILE_UNBUFFERED
Definition file.h:18
@ _FILE_LINE_BUFFERED
Definition file.h:17
#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 SIZE_MAX
Definition stdint.h:127
#define EOF
Definition stdio.h:25
Definition file.h:34
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
_PUBLIC int mtx_lock(mtx_t *mtx)
Definition mtx_lock.c:11
_PUBLIC int mtx_unlock(mtx_t *mtx)
Definition mtx_unlock.c:10
int vfprintf(FILE *_RESTRICT stream, const char *_RESTRICT format, va_list arg)
Definition vfprintf.c:8