summaryrefslogtreecommitdiff
path: root/src/wchar/fputwc.c
blob: e7b9c50353eb30c766aaf0446b8da83c81b6bc66 (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
38
39
40
41
#include <wchar.h>
#include "stdio.h"
#include "nonstd/io.h"
#include "limits.h"
#include "errno.h"

wint_t fputwc(wchar_t c, FILE * stream)
{
	size_t i;

	if (fwide(stream, 1) <= 0) {
		/* not a wide stream */
		return WEOF;
	}

	flockfile(stream);
	stream->orientation = -1;

	char mbs[MB_LEN_MAX+1] = {0};
	mbstate_t ps = 0;
	size_t len = wcrtomb(mbs, c, &ps);

	if (len == (size_t)-1) {
		errno = EILSEQ;
		return WEOF;
	}

	/* FIXME: check for errors here */
	for (i = 0; i < len; i++) {
		fputc(mbs[i], stream);
	}

	stream->orientation = 1;
	funlockfile(stream);

	return c;
}

/*
STDC(199409)
*/