1 /*
2 FUNCTION
3         <<wcsnlen>>---get fixed-size wide-character string length
4 
5 INDEX
6         wcsnlen
7 
8 SYNOPSIS
9         #include <wchar.h>
10         size_t wcsnlen(const wchar_t *<[s]>, size_t <[maxlen]>);
11 
12 DESCRIPTION
13         The <<wcsnlen>> function computes the number of wide-character codes
14         in the wide-character string pointed to by <[s]> not including the
15         terminating L'\0' wide character but at most <[maxlen]> wide
16         characters.
17 
18 RETURNS
19         <<wcsnlen>> returns the length of <[s]> if it is less then <[maxlen]>,
20         or <[maxlen]> if there is no L'\0' wide character in first <[maxlen]>
21         characters.
22 
23 PORTABILITY
24 <<wcsnlen>> is a GNU extension.
25 
26 <<wcsnlen>> requires no supporting OS subroutines.
27 */
28 
29 /*
30  * Copyright (c) 2003, Artem B. Bityuckiy (dedekind@mail.ru).
31  *
32  * Redistribution and use in source and binary forms, with or without
33  * modification, are permitted provided that the above copyright notice,
34  * this condition statement, and the following disclaimer are retained
35  * in any redistributions of the source code.
36  *
37  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
38  * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
39  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
40  * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
41  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
42  * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
43  * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
44  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
45  * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
46  * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
47  * SUCH DAMAGE.
48  */
49 
50 #include <_ansi.h>
51 #include <sys/types.h>
52 #include <wchar.h>
53 
54 size_t
wcsnlen(const wchar_t * s,size_t maxlen)55 wcsnlen (const wchar_t *s,
56                  size_t maxlen)
57 {
58   const wchar_t *p;
59 
60   p = s;
61   while (*p && maxlen-- > 0)
62     p++;
63 
64   return (size_t)(p - s);
65 }
66 
67 
68 
69