1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
|
#include "__stdio.h" // for __FILE, _IO_ERR
#include "stddef.h" // for NULL
#include <atomic.h> // for LIBC_UNLOCK, LIBC_LOCK
#include <errno.h> // for errno, EBADF, EIO
#include <fcntl.h> // for O_ACCMODE, O_RDONLY
#include <stdio.h> // for EOF, FILE, fflush
#include <string.h> // for memmove
#include <unistd.h> // for size_t, write, ssize_t
int fflush(FILE *stream)
{
if (stream == NULL) {
// TODO: Implement flushing all open streams
// For now, just return success
return 0;
}
if (__FILE(stream)->buf_len == 0) {
return 0;
}
if (__FILE(stream)->fd == -1) {
__FILE(stream)->buf_len = 0;
return 0;
}
if (__FILE(stream)->flags & _IO_ERR) {
errno = EIO;
return EOF;
}
if ((__FILE(stream)->flags & O_ACCMODE) == O_RDONLY) {
errno = EBADF;
return EOF;
}
LIBC_LOCK(__FILE(stream)->lock);
size_t bytes_to_write = __FILE(stream)->buf_len;
size_t total_written = 0;
char *buf_ptr = __FILE(stream)->buf;
while (total_written < bytes_to_write) {
ssize_t result = write(__FILE(stream)->fd,
buf_ptr + total_written,
bytes_to_write - total_written);
if (result < 0) {
__FILE(stream)->flags |= _IO_ERR;
LIBC_UNLOCK(__FILE(stream)->lock);
return EOF;
}
if (result == 0) {
break;
}
total_written += result;
}
if (total_written == bytes_to_write) {
__FILE(stream)->buf_len = 0;
__FILE(stream)->buf_pos = 0;
} else {
size_t remaining = bytes_to_write - total_written;
memmove(__FILE(stream)->buf,
__FILE(stream)->buf + total_written, remaining);
__FILE(stream)->buf_len = remaining;
__FILE(stream)->buf_pos = 0;
}
LIBC_UNLOCK(__FILE(stream)->lock);
return (total_written == bytes_to_write) ? 0 : EOF;
}
|