1*4e179ddaSchristos /* $OpenBSD: strnlen.c,v 1.8 2016/10/16 17:37:39 dtucker Exp $ */
2*4e179ddaSchristos
3*4e179ddaSchristos /*
4*4e179ddaSchristos * Copyright (c) 2010 Todd C. Miller <Todd.Miller@courtesan.com>
5*4e179ddaSchristos *
6*4e179ddaSchristos * Permission to use, copy, modify, and distribute this software for any
7*4e179ddaSchristos * purpose with or without fee is hereby granted, provided that the above
8*4e179ddaSchristos * copyright notice and this permission notice appear in all copies.
9*4e179ddaSchristos *
10*4e179ddaSchristos * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11*4e179ddaSchristos * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12*4e179ddaSchristos * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13*4e179ddaSchristos * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14*4e179ddaSchristos * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15*4e179ddaSchristos * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16*4e179ddaSchristos * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*4e179ddaSchristos */
18*4e179ddaSchristos
19*4e179ddaSchristos #include <sys/types.h>
20*4e179ddaSchristos
21*4e179ddaSchristos #include <string.h>
22*4e179ddaSchristos
23*4e179ddaSchristos #include "compat.h"
24*4e179ddaSchristos
25*4e179ddaSchristos size_t
strnlen(const char * str,size_t maxlen)26*4e179ddaSchristos strnlen(const char *str, size_t maxlen)
27*4e179ddaSchristos {
28*4e179ddaSchristos const char *cp;
29*4e179ddaSchristos
30*4e179ddaSchristos for (cp = str; maxlen != 0 && *cp != '\0'; cp++, maxlen--)
31*4e179ddaSchristos ;
32*4e179ddaSchristos
33*4e179ddaSchristos return (size_t)(cp - str);
34*4e179ddaSchristos }
35