PatchworkOS
Loading...
Searching...
No Matches
fgets.c
Go to the documentation of this file.
1#include <stdio.h>
2
3#include "user/common/file.h"
4
5char* fgets(char* _RESTRICT s, int size, FILE* _RESTRICT stream)
6{
7 char* dest = s;
8
9 if (size == 0)
10 {
11 return NULL;
12 }
13
14 if (size == 1)
15 {
16 *s = '\0';
17 return s;
18 }
19
20 mtx_lock(&stream->mtx);
21
22 if (_file_prepare_read(stream) != ERR)
23 {
24 do
25 {
26 if (_FILE_CHECK_AVAIL(stream) == ERR)
27 {
28 /* In case of error / EOF before a character is read, this
29 will lead to a \0 be written anyway. Since the results
30 are "indeterminate" by definition, this does not hurt.
31 */
32 break;
33 }
34 } while (((*dest++ = _FILE_GETC(stream)) != '\n') && (--size > 0));
35 }
36
37 mtx_unlock(&stream->mtx);
38
39 *dest = '\0';
40 return (dest == s) ? NULL : s;
41}
char * fgets(char *_RESTRICT s, int size, FILE *_RESTRICT stream)
Definition fgets.c:5
#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_read(FILE *stream)
Definition file.c:224
#define _FILE_GETC(stream)
Definition file.h:49
#define _FILE_CHECK_AVAIL(fh)
Definition file.h:53
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