PatchworkOS
Loading...
Searching...
No Matches
fputc.c
Go to the documentation of this file.
1#include <stdio.h>
2#include <stdlib.h>
3
4#include "user/common/file.h"
6
7int fputc(int c, FILE* stream)
8{
9 mtx_lock(&stream->mtx);
10
11 if (_file_prepare_write(stream) == ERR)
12 {
13 mtx_unlock(&stream->mtx);
14 return EOF;
15 }
16
17 stream->buf[stream->bufIndex++] = (char)c;
18 if ((stream->bufIndex == stream->bufSize) || ((stream->flags & _FILE_LINE_BUFFERED) && ((char)c == '\n')) ||
19 (stream->flags & _FILE_UNBUFFERED))
20 {
21 // buffer filled, unbuffered stream, or end-of-line.
22 c = (_file_flush_buffer(stream) != ERR) ? c : EOF;
23 }
24
25 mtx_unlock(&stream->mtx);
26
27 return c;
28}
int fputc(int c, FILE *stream)
Definition fputc.c:7
#define ERR
Integer error value.
Definition ERR.h:17
uint64_t _file_prepare_write(FILE *stream)
Definition file.c:239
uint64_t _file_flush_buffer(FILE *stream)
Definition file.c:158
@ _FILE_UNBUFFERED
Definition file.h:18
@ _FILE_LINE_BUFFERED
Definition file.h:17
#define EOF
Definition stdio.h:25
Definition file.h:34
uint8_t * buf
Definition file.h:37
uint64_t bufIndex
Definition file.h:39
uint64_t bufSize
Definition file.h:38
_file_flags_t flags
Definition file.h:44
mtx_t mtx
Definition file.h:45
_PUBLIC int mtx_lock(mtx_t *mtx)
Definition mtx_lock.c:11
_PUBLIC int mtx_unlock(mtx_t *mtx)
Definition mtx_unlock.c:10