xref: /csrg-svn/lib/libc/locale/frune.c (revision 60471)
1*60471Sbostic /*-
2*60471Sbostic  * Copyright (c) 1993 The Regents of the University of California.
3*60471Sbostic  * All rights reserved.
4*60471Sbostic  *
5*60471Sbostic  * This code is derived from software contributed to Berkeley by
6*60471Sbostic  * Paul Borman at Krystal Technologies.
7*60471Sbostic  *
8*60471Sbostic  * %sccs.include.redist.c%
9*60471Sbostic  */
10*60471Sbostic 
11*60471Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*60471Sbostic static char sccsid[] = "@(#)frune.c	5.1 (Berkeley) 05/26/93";
13*60471Sbostic #endif /* LIBC_SCCS and not lint */
14*60471Sbostic 
15*60471Sbostic #include <limits.h>
16*60471Sbostic #include <rune.h>
17*60471Sbostic #include <stddef.h>
18*60471Sbostic #include <stdio.h>
19*60471Sbostic 
20*60471Sbostic long
21*60471Sbostic fgetrune(fp)
22*60471Sbostic 	FILE *fp;
23*60471Sbostic {
24*60471Sbostic 	rune_t  r;
25*60471Sbostic 	int c, len;
26*60471Sbostic 	char buf[MB_LEN_MAX];
27*60471Sbostic 	char const *result;
28*60471Sbostic 
29*60471Sbostic 	len = 0;
30*60471Sbostic 	do {
31*60471Sbostic 		if ((c = getc(fp)) == EOF) {
32*60471Sbostic 			if (len)
33*60471Sbostic 				break;
34*60471Sbostic 			return (EOF);
35*60471Sbostic 		}
36*60471Sbostic 		buf[len++] = c;
37*60471Sbostic 
38*60471Sbostic 		if ((r = sgetrune(buf, len, &result)) != _INVALID_RUNE)
39*60471Sbostic 			return (r);
40*60471Sbostic 	} while (result == buf && len < MB_LEN_MAX);
41*60471Sbostic 
42*60471Sbostic 	while (--len > 0)
43*60471Sbostic 		ungetc(buf[len], fp);
44*60471Sbostic 	return (_INVALID_RUNE);
45*60471Sbostic }
46*60471Sbostic 
47*60471Sbostic int
48*60471Sbostic fungetrune(r, fp)
49*60471Sbostic 	rune_t r;
50*60471Sbostic 	FILE* fp;
51*60471Sbostic {
52*60471Sbostic 	int len;
53*60471Sbostic 	char buf[MB_LEN_MAX];
54*60471Sbostic 
55*60471Sbostic 	len = sputrune(r, buf, MB_LEN_MAX, 0);
56*60471Sbostic 	while (len-- > 0)
57*60471Sbostic 		if (ungetc(buf[len], fp) == EOF)
58*60471Sbostic 			return (EOF);
59*60471Sbostic 	return (0);
60*60471Sbostic }
61*60471Sbostic 
62*60471Sbostic int
63*60471Sbostic fputrune(r, fp)
64*60471Sbostic 	rune_t r;
65*60471Sbostic 	FILE *fp;
66*60471Sbostic {
67*60471Sbostic 	int i, len;
68*60471Sbostic 	char buf[MB_LEN_MAX];
69*60471Sbostic 
70*60471Sbostic 	len = sputrune(r, buf, MB_LEN_MAX, 0);
71*60471Sbostic 
72*60471Sbostic 	for (i = 0; i < len; ++i)
73*60471Sbostic 		if (putc(buf[i], fp) == EOF)
74*60471Sbostic 			return (EOF);
75*60471Sbostic 
76*60471Sbostic 	return (0);
77*60471Sbostic }
78