mbstowcs.c (58d38e25205c6ee5ef0796ffa2cd8e2ca6c6e7f3) mbstowcs.c (b6f33850e0b8ee9cc071000b1ed108a9144a8609)
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

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

39
40#include <errno.h>
41#include <stdlib.h>
42#include <limits.h>
43#include <stddef.h>
44#include <rune.h>
45
46size_t
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

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

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}