5158 sed dumps core in new multibyte code
[illumos-gate.git] / usr / src / lib / libc / port / locale / none.c
blob0511563cb1691f9a29b5894808b78d36fd438834
1 /*
2 * Copyright 2013 Garrett D'Amore <garrett@damore.org>
3 * Copyright 2010 Nexenta Systems, Inc. All rights reserved.
4 * Copyright (c) 2002-2004 Tim J. Robbins. All rights reserved.
5 * Copyright (c) 1993
6 * The Regents of the University of California. All rights reserved.
8 * This code is derived from software contributed to Berkeley by
9 * Paul Borman at Krystal Technologies.
11 * Redistribution and use in source and binary forms, with or without
12 * modification, are permitted provided that the following conditions
13 * are met:
14 * 1. Redistributions of source code must retain the above copyright
15 * notice, this list of conditions and the following disclaimer.
16 * 2. Redistributions in binary form must reproduce the above copyright
17 * notice, this list of conditions and the following disclaimer in the
18 * documentation and/or other materials provided with the distribution.
19 * 4. Neither the name of the University nor the names of its contributors
20 * may be used to endorse or promote products derived from this software
21 * without specific prior written permission.
23 * THIS SOFTWARE IS PROVIDED BY THE REGENTS AND CONTRIBUTORS ``AS IS'' AND
24 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
25 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
26 * ARE DISCLAIMED. IN NO EVENT SHALL THE REGENTS OR CONTRIBUTORS BE LIABLE
27 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
28 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
29 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
30 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
31 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
32 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
33 * SUCH DAMAGE.
36 #include "lint.h"
37 #include <errno.h>
38 #include <limits.h>
39 #include <stddef.h>
40 #include <stdio.h>
41 #include <stdlib.h>
42 #include <string.h>
43 #include <wchar.h>
44 #include <note.h>
45 #include "mblocal.h"
46 #include "lctype.h"
48 /* setup defaults */
50 void
51 _none_init(struct lc_ctype *lct)
53 lct->lc_is_ascii = 1;
54 lct->lc_mbrtowc = __mbrtowc_ascii;
55 lct->lc_mbsinit = __mbsinit_ascii;
56 lct->lc_mbsnrtowcs = __mbsnrtowcs_ascii;
57 lct->lc_wcrtomb = __wcrtomb_ascii;
58 lct->lc_wcsnrtombs = __wcsnrtombs_ascii;
59 lct->lc_max_mblen = 1;
62 int
63 __mbsinit_ascii(const mbstate_t *unused)
65 _NOTE(ARGUNUSED(unused));
68 * Encoding is not state dependent - we are always in the
69 * initial state.
71 return (1);
74 size_t
75 __mbrtowc_ascii(wchar_t *_RESTRICT_KYWD pwc, const char *_RESTRICT_KYWD s,
76 size_t n, mbstate_t *_RESTRICT_KYWD unused)
78 _NOTE(ARGUNUSED(unused));
80 if (s == NULL)
81 /* Reset to initial shift state (no-op) */
82 return (0);
83 if (n == 0)
84 /* Incomplete multibyte sequence */
85 return ((size_t)-2);
86 if (pwc != NULL)
87 *pwc = (unsigned char)*s;
88 return (*s == '\0' ? 0 : 1);
91 size_t
92 __wcrtomb_ascii(char *_RESTRICT_KYWD s, wchar_t wc,
93 mbstate_t *_RESTRICT_KYWD unused)
95 _NOTE(ARGUNUSED(unused));
97 if (s == NULL)
98 /* Reset to initial shift state (no-op) */
99 return (1);
100 if (wc < 0 || wc > UCHAR_MAX) {
101 errno = EILSEQ;
102 return ((size_t)-1);
104 *s = (unsigned char)wc;
105 return (1);
108 size_t
109 __mbsnrtowcs_ascii(wchar_t *_RESTRICT_KYWD dst, const char **_RESTRICT_KYWD src,
110 size_t nms, size_t len, mbstate_t *_RESTRICT_KYWD unused)
112 const char *s;
113 size_t nchr;
115 _NOTE(ARGUNUSED(unused));
117 if (dst == NULL) {
118 s = memchr(*src, '\0', nms);
119 return (s != NULL ? s - *src : nms);
122 s = *src;
123 nchr = 0;
124 while (len-- > 0 && nms-- > 0) {
125 if ((*dst++ = (unsigned char)*s++) == L'\0') {
126 *src = NULL;
127 return (nchr);
129 nchr++;
131 *src = s;
132 return (nchr);
135 size_t
136 __wcsnrtombs_ascii(char *_RESTRICT_KYWD dst, const wchar_t **_RESTRICT_KYWD src,
137 size_t nwc, size_t len, mbstate_t *_RESTRICT_KYWD unused)
139 const wchar_t *s;
140 size_t nchr;
142 _NOTE(ARGUNUSED(unused));
144 if (dst == NULL) {
145 for (s = *src; nwc > 0 && *s != L'\0'; s++, nwc--) {
146 if (*s < 0 || *s > UCHAR_MAX) {
147 errno = EILSEQ;
148 return ((size_t)-1);
151 return (s - *src);
154 s = *src;
155 nchr = 0;
156 while (len-- > 0 && nwc-- > 0) {
157 if (*s < 0 || *s > UCHAR_MAX) {
158 errno = EILSEQ;
159 return ((size_t)-1);
161 if ((*dst++ = *s++) == '\0') {
162 *src = NULL;
163 return (nchr);
165 nchr++;
167 *src = s;
168 return (nchr);