1*60470Sbostic /*- 2*60470Sbostic * Copyright (c) 1993 The Regents of the University of California. 3*60470Sbostic * All rights reserved. 4*60470Sbostic * 5*60470Sbostic * This code is derived from software contributed to Berkeley by 6*60470Sbostic * Paul Borman at Krystal Technologies. 7*60470Sbostic * 8*60470Sbostic * %sccs.include.redist.c% 9*60470Sbostic */ 10*60470Sbostic 11*60470Sbostic #if defined(LIBC_SCCS) && !defined(lint) 12*60470Sbostic static char sccsid[] = "@(#)ansi.c 5.1 (Berkeley) 05/26/93"; 13*60470Sbostic #endif /* LIBC_SCCS and not lint */ 14*60470Sbostic 15*60470Sbostic #include <stdlib.h> 16*60470Sbostic #include <limits.h> 17*60470Sbostic #include <stddef.h> 18*60470Sbostic #include <rune.h> 19*60470Sbostic 20*60470Sbostic int 21*60470Sbostic mblen(s, n) 22*60470Sbostic const char *s; 23*60470Sbostic size_t n; 24*60470Sbostic { 25*60470Sbostic char const *e; 26*60470Sbostic 27*60470Sbostic if (s == 0 || *s == 0) 28*60470Sbostic return (0); /* No support for state dependent encodings. */ 29*60470Sbostic 30*60470Sbostic if (sgetrune(s, (int)n, &e) == _INVALID_RUNE) 31*60470Sbostic return (s - e); 32*60470Sbostic return (e - s); 33*60470Sbostic } 34*60470Sbostic 35*60470Sbostic int 36*60470Sbostic mbtowc(pwc, s, n) 37*60470Sbostic wchar_t *pwc; 38*60470Sbostic const char *s; 39*60470Sbostic size_t n; 40*60470Sbostic { 41*60470Sbostic char const *e; 42*60470Sbostic rune_t r; 43*60470Sbostic 44*60470Sbostic if (s == 0 || *s == 0) 45*60470Sbostic return (0); /* No support for state dependent encodings. */ 46*60470Sbostic 47*60470Sbostic if ((r = sgetrune(s, (int)n, &e)) == _INVALID_RUNE) 48*60470Sbostic return (s - e); 49*60470Sbostic if (pwc) 50*60470Sbostic *pwc = r; 51*60470Sbostic return (e - s); 52*60470Sbostic } 53*60470Sbostic 54*60470Sbostic int 55*60470Sbostic wctomb(s, wchar) 56*60470Sbostic char *s; 57*60470Sbostic wchar_t wchar; 58*60470Sbostic { 59*60470Sbostic char *e; 60*60470Sbostic 61*60470Sbostic if (s == 0) 62*60470Sbostic return (0); /* No support for state dependent encodings. */ 63*60470Sbostic 64*60470Sbostic if (wchar == 0) { 65*60470Sbostic *s = 0; 66*60470Sbostic return (1); 67*60470Sbostic } 68*60470Sbostic 69*60470Sbostic sputrune(wchar, s, MB_CUR_MAX, &e); 70*60470Sbostic return (e ? e - s : -1); 71*60470Sbostic } 72*60470Sbostic 73*60470Sbostic size_t 74*60470Sbostic mbstowcs(pwcs, s, n) 75*60470Sbostic wchar_t *pwcs; 76*60470Sbostic const char *s; 77*60470Sbostic size_t n; 78*60470Sbostic { 79*60470Sbostic char const *e; 80*60470Sbostic int cnt = 0; 81*60470Sbostic 82*60470Sbostic if (!pwcs || !s) 83*60470Sbostic return (-1); 84*60470Sbostic 85*60470Sbostic while (n-- > 0) { 86*60470Sbostic *pwcs = sgetrune(s, MB_LEN_MAX, &e); 87*60470Sbostic if (*pwcs == _INVALID_RUNE) 88*60470Sbostic return (-1); 89*60470Sbostic if (*pwcs++ == 0) 90*60470Sbostic break; 91*60470Sbostic s = e; 92*60470Sbostic ++cnt; 93*60470Sbostic } 94*60470Sbostic return (cnt); 95*60470Sbostic } 96*60470Sbostic 97*60470Sbostic size_t 98*60470Sbostic wcstombs(s, pwcs, n) 99*60470Sbostic char *s; 100*60470Sbostic const wchar_t *pwcs; 101*60470Sbostic size_t n; 102*60470Sbostic { 103*60470Sbostic char *e; 104*60470Sbostic int cnt; 105*60470Sbostic 106*60470Sbostic if (!pwcs || !s) 107*60470Sbostic return (-1); 108*60470Sbostic 109*60470Sbostic while (n > 0) { 110*60470Sbostic if (*pwcs == 0) { 111*60470Sbostic *s = 0; 112*60470Sbostic break; 113*60470Sbostic } 114*60470Sbostic if (!sputrune(*pwcs++, s, (int)n, &e)) 115*60470Sbostic return (-1); /* encoding error */ 116*60470Sbostic if (!e) /* too long */ 117*60470Sbostic return (cnt); 118*60470Sbostic cnt += e - s; 119*60470Sbostic s = e; 120*60470Sbostic } 121*60470Sbostic return (cnt); 122*60470Sbostic } 123