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