xref: /csrg-svn/lib/libc/locale/frune.c (revision 61136)
160471Sbostic /*-
2*61136Sbostic  * Copyright (c) 1993
3*61136Sbostic  *	The Regents of the University of California.  All rights reserved.
460471Sbostic  *
560471Sbostic  * This code is derived from software contributed to Berkeley by
660471Sbostic  * Paul Borman at Krystal Technologies.
760471Sbostic  *
860471Sbostic  * %sccs.include.redist.c%
960471Sbostic  */
1060471Sbostic 
1160471Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*61136Sbostic static char sccsid[] = "@(#)frune.c	8.1 (Berkeley) 06/04/93";
1360471Sbostic #endif /* LIBC_SCCS and not lint */
1460471Sbostic 
1560471Sbostic #include <limits.h>
1660471Sbostic #include <rune.h>
1760471Sbostic #include <stddef.h>
1860471Sbostic #include <stdio.h>
1960471Sbostic 
2060471Sbostic long
fgetrune(fp)2160471Sbostic fgetrune(fp)
2260471Sbostic 	FILE *fp;
2360471Sbostic {
2460471Sbostic 	rune_t  r;
2560471Sbostic 	int c, len;
2660471Sbostic 	char buf[MB_LEN_MAX];
2760471Sbostic 	char const *result;
2860471Sbostic 
2960471Sbostic 	len = 0;
3060471Sbostic 	do {
3160471Sbostic 		if ((c = getc(fp)) == EOF) {
3260471Sbostic 			if (len)
3360471Sbostic 				break;
3460471Sbostic 			return (EOF);
3560471Sbostic 		}
3660471Sbostic 		buf[len++] = c;
3760471Sbostic 
3860471Sbostic 		if ((r = sgetrune(buf, len, &result)) != _INVALID_RUNE)
3960471Sbostic 			return (r);
4060471Sbostic 	} while (result == buf && len < MB_LEN_MAX);
4160471Sbostic 
4260471Sbostic 	while (--len > 0)
4360471Sbostic 		ungetc(buf[len], fp);
4460471Sbostic 	return (_INVALID_RUNE);
4560471Sbostic }
4660471Sbostic 
4760471Sbostic int
fungetrune(r,fp)4860471Sbostic fungetrune(r, fp)
4960471Sbostic 	rune_t r;
5060471Sbostic 	FILE* fp;
5160471Sbostic {
5260471Sbostic 	int len;
5360471Sbostic 	char buf[MB_LEN_MAX];
5460471Sbostic 
5560471Sbostic 	len = sputrune(r, buf, MB_LEN_MAX, 0);
5660471Sbostic 	while (len-- > 0)
5760471Sbostic 		if (ungetc(buf[len], fp) == EOF)
5860471Sbostic 			return (EOF);
5960471Sbostic 	return (0);
6060471Sbostic }
6160471Sbostic 
6260471Sbostic int
fputrune(r,fp)6360471Sbostic fputrune(r, fp)
6460471Sbostic 	rune_t r;
6560471Sbostic 	FILE *fp;
6660471Sbostic {
6760471Sbostic 	int i, len;
6860471Sbostic 	char buf[MB_LEN_MAX];
6960471Sbostic 
7060471Sbostic 	len = sputrune(r, buf, MB_LEN_MAX, 0);
7160471Sbostic 
7260471Sbostic 	for (i = 0; i < len; ++i)
7360471Sbostic 		if (putc(buf[i], fp) == EOF)
7460471Sbostic 			return (EOF);
7560471Sbostic 
7660471Sbostic 	return (0);
7760471Sbostic }
78