summaryrefslogtreecommitdiff
path: root/lib/libc/stdio/stdout.c
blob: 471a54b9d917b15d0cc57eff373b43e48c079196 (plain)
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
#include "__stdio.h" // for __FILE, __libc_fadd

#include <atomic.h> // for LIBC_LOCK, LIBC_UNLOCK
#include <fcntl.h>  // for O_WRONLY
#include <stddef.h> // for NULL
#include <stdio.h>  // for stdout
#include <sys/cdefs.h>
#include <unistd.h> // for STDOUT_FILENO

#define BUFSIZ 4096

__weak char __stdout_buffer[0];
static atomic_flag __stdio_lock = ATOMIC_FLAG_INIT;
struct __FILE __stdout = { .fd = STDOUT_FILENO,
			   .flags = O_WRONLY,
			   .type = 1,
			   .buf = __stdout_buffer,
			   .buf_len = 0,
			   .buf_size = BUFSIZ,
			   .buf_pos = 0,
			   .eof = 0,
			   .unget_cnt = 0,
			   .offset = 0,
			   .next = NULL,
			   .lock = ATOMIC_FLAG_INIT };

struct __FILE *const stdout = (struct __FILE *)&__stdout;

void __libc_fadd(struct __FILE *f)
{
	LIBC_LOCK(__stdio_lock);
	struct __FILE *cur = &__stdout;
	while (cur->next)
		cur = cur->next;
	cur->next = f;
	LIBC_UNLOCK(__stdio_lock);
}