summaryrefslogtreecommitdiff
path: root/lib/libc/stdio/fputc.c
blob: 08ab0cfcc48cd150347119056ec1f6650068f328 (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
#include "stddef.h" // for NULL

#include <errno.h> // for EINVAL, errno
#include <libc.h>  // for __IMPL
#include <stdio.h> // for EOF, fwrite, FILE, fputc

int fputc(int c, FILE *stream)
{
	if (stream == NULL) {
		errno = EINVAL;
		return EOF;
	}

	if (__IMPL(stream)->fd == -1 && __IMPL(stream)->buf != NULL) {
		if (__IMPL(stream)->buf_len >= __IMPL(stream)->buf_size - 1) {
			return EOF;
		}

		__IMPL(stream)->buf[__IMPL(stream)->buf_len++] = (char)c;
		return (unsigned char)c;
	}

	return fwrite(&c, 1, 1, stream) ? c : EOF;
}