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

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

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

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

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

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