Deleted Added
full compact
mbstowcs.c (103012) mbstowcs.c (106032)
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 * Redistribution and use in source and binary forms, with or without

--- 21 unchanged lines hidden (view full) ---

30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#include <sys/cdefs.h>
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 * Redistribution and use in source and binary forms, with or without

--- 21 unchanged lines hidden (view full) ---

30 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
31 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
32 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
33 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
34 * SUCH DAMAGE.
35 */
36
37#include <sys/cdefs.h>
38__FBSDID("$FreeBSD: head/lib/libc/locale/mbstowcs.c 103012 2002-09-06 11:24:06Z tjr $");
38__FBSDID("$FreeBSD: head/lib/libc/locale/mbstowcs.c 106032 2002-10-27 10:41:21Z tjr $");
39
40#include <errno.h>
41#include <stdlib.h>
42#include <limits.h>
43#include <stddef.h>
44#include <rune.h>
45
46size_t
39
40#include <errno.h>
41#include <stdlib.h>
42#include <limits.h>
43#include <stddef.h>
44#include <rune.h>
45
46size_t
47mbstowcs(pwcs, s, n)
48 wchar_t * __restrict pwcs;
49 const char * __restrict s;
50 size_t n;
47mbstowcs(wchar_t * __restrict pwcs, const char * __restrict s, size_t n)
51{
48{
52 char const *e;
53 int cnt = 0;
49 const char *e;
50 int cnt;
54 rune_t r;
55
51 rune_t r;
52
56 if (!s) {
53 if (s == NULL) {
57 errno = EINVAL;
58 return (-1);
59 }
60
61 if (pwcs == NULL) {
62 /* Convert and count only, do not store. */
54 errno = EINVAL;
55 return (-1);
56 }
57
58 if (pwcs == NULL) {
59 /* Convert and count only, do not store. */
60 cnt = 0;
63 while ((r = sgetrune(s, MB_LEN_MAX, &e)) != _INVALID_RUNE &&
64 r != 0) {
65 s = e;
66 cnt++;
67 }
68 if (r == _INVALID_RUNE) {
69 errno = EILSEQ;
70 return (-1);
71 }
72 }
73
74 /* Convert, store and count characters. */
61 while ((r = sgetrune(s, MB_LEN_MAX, &e)) != _INVALID_RUNE &&
62 r != 0) {
63 s = e;
64 cnt++;
65 }
66 if (r == _INVALID_RUNE) {
67 errno = EILSEQ;
68 return (-1);
69 }
70 }
71
72 /* Convert, store and count characters. */
73 cnt = 0;
75 while (n-- > 0) {
76 *pwcs = sgetrune(s, MB_LEN_MAX, &e);
77 if (*pwcs == _INVALID_RUNE) {
78 errno = EILSEQ;
79 return (-1);
80 }
74 while (n-- > 0) {
75 *pwcs = sgetrune(s, MB_LEN_MAX, &e);
76 if (*pwcs == _INVALID_RUNE) {
77 errno = EILSEQ;
78 return (-1);
79 }
81 if (*pwcs++ == 0)
80 if (*pwcs++ == L'\0')
82 break;
83 s = e;
84 ++cnt;
85 }
86
87 return (cnt);
88}
81 break;
82 s = e;
83 ++cnt;
84 }
85
86 return (cnt);
87}