summaryrefslogtreecommitdiff
path: root/lib/libc/stdio/fputc.c
blob: c544d5dd19c2b774fed707b6fc482c048ae4c6f8 (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
#include <stdio.h>
#include <errno.h>
#include <string.h>
#include <io.h>

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

	// Special case for string buffer operations (snprintf)
	// When fd is -1, we're writing to a string buffer
	if (stream->fd == -1 && stream->buf != NULL) {
		// Check if there's space (leave room for null terminator)
		if (stream->buf_len >= stream->buf_size - 1) {
			return EOF;
		}

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

	// For regular file operations, use fwrite
	return fwrite(&c, 1, 1, stream) ? c : EOF;
}