160470Sbostic /*-
2*63598Sbostic * Copyright (c) 1993
3*63598Sbostic * The Regents of the University of California. All rights reserved.
460470Sbostic *
560470Sbostic * This code is derived from software contributed to Berkeley by
660470Sbostic * Paul Borman at Krystal Technologies.
760470Sbostic *
860470Sbostic * %sccs.include.redist.c%
960470Sbostic */
1060470Sbostic
1160470Sbostic #if defined(LIBC_SCCS) && !defined(lint)
12*63598Sbostic static char sccsid[] = "@(#)ansi.c 8.1 (Berkeley) 06/27/93";
1360470Sbostic #endif /* LIBC_SCCS and not lint */
1460470Sbostic
1560470Sbostic #include <stdlib.h>
1660470Sbostic #include <limits.h>
1760470Sbostic #include <stddef.h>
1860470Sbostic #include <rune.h>
1960470Sbostic
2060470Sbostic int
mblen(s,n)2160470Sbostic mblen(s, n)
2260470Sbostic const char *s;
2360470Sbostic size_t n;
2460470Sbostic {
2560470Sbostic char const *e;
2660470Sbostic
2760470Sbostic if (s == 0 || *s == 0)
2860470Sbostic return (0); /* No support for state dependent encodings. */
2960470Sbostic
3060470Sbostic if (sgetrune(s, (int)n, &e) == _INVALID_RUNE)
3160470Sbostic return (s - e);
3260470Sbostic return (e - s);
3360470Sbostic }
3460470Sbostic
3560470Sbostic int
mbtowc(pwc,s,n)3660470Sbostic mbtowc(pwc, s, n)
3760470Sbostic wchar_t *pwc;
3860470Sbostic const char *s;
3960470Sbostic size_t n;
4060470Sbostic {
4160470Sbostic char const *e;
4260470Sbostic rune_t r;
4360470Sbostic
4460470Sbostic if (s == 0 || *s == 0)
4560470Sbostic return (0); /* No support for state dependent encodings. */
4660470Sbostic
4760470Sbostic if ((r = sgetrune(s, (int)n, &e)) == _INVALID_RUNE)
4860470Sbostic return (s - e);
4960470Sbostic if (pwc)
5060470Sbostic *pwc = r;
5160470Sbostic return (e - s);
5260470Sbostic }
5360470Sbostic
5460470Sbostic int
wctomb(s,wchar)5560470Sbostic wctomb(s, wchar)
5660470Sbostic char *s;
5760470Sbostic wchar_t wchar;
5860470Sbostic {
5960470Sbostic char *e;
6060470Sbostic
6160470Sbostic if (s == 0)
6260470Sbostic return (0); /* No support for state dependent encodings. */
6360470Sbostic
6460470Sbostic if (wchar == 0) {
6560470Sbostic *s = 0;
6660470Sbostic return (1);
6760470Sbostic }
6860470Sbostic
6960470Sbostic sputrune(wchar, s, MB_CUR_MAX, &e);
7060470Sbostic return (e ? e - s : -1);
7160470Sbostic }
7260470Sbostic
7360470Sbostic size_t
mbstowcs(pwcs,s,n)7460470Sbostic mbstowcs(pwcs, s, n)
7560470Sbostic wchar_t *pwcs;
7660470Sbostic const char *s;
7760470Sbostic size_t n;
7860470Sbostic {
7960470Sbostic char const *e;
8060470Sbostic int cnt = 0;
8160470Sbostic
8260470Sbostic if (!pwcs || !s)
8360470Sbostic return (-1);
8460470Sbostic
8560470Sbostic while (n-- > 0) {
8660470Sbostic *pwcs = sgetrune(s, MB_LEN_MAX, &e);
8760470Sbostic if (*pwcs == _INVALID_RUNE)
8860470Sbostic return (-1);
8960470Sbostic if (*pwcs++ == 0)
9060470Sbostic break;
9160470Sbostic s = e;
9260470Sbostic ++cnt;
9360470Sbostic }
9460470Sbostic return (cnt);
9560470Sbostic }
9660470Sbostic
9760470Sbostic size_t
wcstombs(s,pwcs,n)9860470Sbostic wcstombs(s, pwcs, n)
9960470Sbostic char *s;
10060470Sbostic const wchar_t *pwcs;
10160470Sbostic size_t n;
10260470Sbostic {
10360470Sbostic char *e;
10460503Sbostic int cnt = 0;
10560470Sbostic
10660470Sbostic if (!pwcs || !s)
10760470Sbostic return (-1);
10860470Sbostic
10960470Sbostic while (n > 0) {
11060470Sbostic if (*pwcs == 0) {
11160470Sbostic *s = 0;
11260470Sbostic break;
11360470Sbostic }
11460470Sbostic if (!sputrune(*pwcs++, s, (int)n, &e))
11560470Sbostic return (-1); /* encoding error */
11660470Sbostic if (!e) /* too long */
11760470Sbostic return (cnt);
11860470Sbostic cnt += e - s;
11960470Sbostic s = e;
12060470Sbostic }
12160470Sbostic return (cnt);
12260470Sbostic }
123