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