1*cc5d5811Skamil /* $NetBSD: wcsnlen.c,v 1.2 2016/10/15 14:30:36 kamil Exp $ */
2*cc5d5811Skamil
36245abaaSkamil /*-
46245abaaSkamil * Copyright (c) 2016 The NetBSD Foundation, Inc.
56245abaaSkamil * All rights reserved.
66245abaaSkamil *
76245abaaSkamil * This code is derived from software contributed to The NetBSD Foundation
86245abaaSkamil * by Kamil Rytarowski.
96245abaaSkamil *
106245abaaSkamil * Redistribution and use in source and binary forms, with or without
116245abaaSkamil * modification, are permitted provided that the following conditions
126245abaaSkamil * are met:
136245abaaSkamil * 1. Redistributions of source code must retain the above copyright
146245abaaSkamil * notice, this list of conditions and the following disclaimer.
156245abaaSkamil * 2. Redistributions in binary form must reproduce the above copyright
166245abaaSkamil * notice, this list of conditions and the following disclaimer in the
176245abaaSkamil * documentation and/or other materials provided with the distribution.
186245abaaSkamil *
196245abaaSkamil * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
206245abaaSkamil * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
216245abaaSkamil * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
226245abaaSkamil * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
236245abaaSkamil * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
246245abaaSkamil * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
256245abaaSkamil * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
266245abaaSkamil * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
276245abaaSkamil * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
286245abaaSkamil * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
296245abaaSkamil * POSSIBILITY OF SUCH DAMAGE.
306245abaaSkamil */
316245abaaSkamil
326245abaaSkamil #include <sys/cdefs.h>
33*cc5d5811Skamil __RCSID("$NetBSD: wcsnlen.c,v 1.2 2016/10/15 14:30:36 kamil Exp $");
346245abaaSkamil
356245abaaSkamil #include <assert.h>
366245abaaSkamil #include <wchar.h>
376245abaaSkamil
386245abaaSkamil size_t
wcsnlen(const wchar_t * s,size_t maxlen)396245abaaSkamil wcsnlen(const wchar_t *s, size_t maxlen)
406245abaaSkamil {
416245abaaSkamil const wchar_t *p;
426245abaaSkamil
436245abaaSkamil _DIAGASSERT(s != NULL);
446245abaaSkamil
456245abaaSkamil p = s;
466245abaaSkamil while (maxlen-->0 && *p) {
476245abaaSkamil p++;
486245abaaSkamil }
496245abaaSkamil
506245abaaSkamil return p - s;
516245abaaSkamil }
52