strnlen.c revision 302408
1193323Sed/*-
2193323Sed * Copyright (c) 2009 David Schultz <das@FreeBSD.org>
3193323Sed * All rights reserved.
4193323Sed *
5193323Sed * Redistribution and use in source and binary forms, with or without
6193323Sed * modification, are permitted provided that the following conditions
7193323Sed * are met:
8193323Sed * 1. Redistributions of source code must retain the above copyright
9193323Sed *    notice, this list of conditions and the following disclaimer.
10193323Sed * 2. Redistributions in binary form must reproduce the above copyright
11193323Sed *    notice, this list of conditions and the following disclaimer in the
12193323Sed *    documentation and/or other materials provided with the distribution.
13193323Sed *
14193323Sed * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15193323Sed * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16198892Srdivacky * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17193323Sed * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18224145Sdim * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19205218Srdivacky * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20224145Sdim * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21201360Srdivacky * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22193323Sed * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23198090Srdivacky * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24204642Srdivacky * SUCH DAMAGE.
25193323Sed */
26198090Srdivacky
27224145Sdim#include <sys/cdefs.h>
28193323Sed__FBSDID("$FreeBSD: stable/11/lib/libc/string/strnlen.c 189136 2009-02-28 06:00:58Z das $");
29193323Sed
30224145Sdim#include <string.h>
31205218Srdivacky
32193323Sedsize_t
33193323Sedstrnlen(const char *s, size_t maxlen)
34193323Sed{
35198090Srdivacky	size_t len;
36193323Sed
37202375Srdivacky	for (len = 0; len < maxlen; len++, s++) {
38198090Srdivacky		if (!*s)
39193323Sed			break;
40193323Sed	}
41193323Sed	return (len);
42193323Sed}
43193323Sed