PatchworkOS
Loading...
Searching...
No Matches
fputs.c
Go to the documentation of this file.
1#include <stdio.h>
2
3#include "user/common/file.h"
4
5int fputs(const char* _RESTRICT s, FILE* _RESTRICT stream)
6{
7 mtx_lock(&stream->mtx);
8
9 if (_file_prepare_write(stream) == ERR)
10 {
11 mtx_unlock(&stream->mtx);
12 return EOF;
13 }
14
15 while (*s != '\0')
16 {
17 stream->buf[stream->bufIndex++] = *s;
18
19 if ((stream->bufIndex == stream->bufSize) || ((stream->flags & _FILE_LINE_BUFFERED) && *s == '\n'))
20 {
21 if (_file_flush_buffer(stream) == ERR)
22 {
23 mtx_unlock(&stream->mtx);
24 return EOF;
25 }
26 }
27
28 ++s;
29 }
30
31 if (stream->flags & _FILE_UNBUFFERED)
32 {
33 if (_file_flush_buffer(stream) == ERR)
34 {
35 mtx_unlock(&stream->mtx);
36 return EOF;
37 }
38 }
39
40 mtx_unlock(&stream->mtx);
41
42 return 0;
43}
int fputs(const char *_RESTRICT s, FILE *_RESTRICT stream)
Definition fputs.c:5
#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
@ _FILE_UNBUFFERED
Definition file.h:18
@ _FILE_LINE_BUFFERED
Definition file.h:17
#define EOF
Definition stdio.h:25
Definition file.h:34
_PUBLIC int mtx_lock(mtx_t *mtx)
Definition mtx_lock.c:11
_PUBLIC int mtx_unlock(mtx_t *mtx)
Definition mtx_unlock.c:10