xref: /dflybsd-src/contrib/cvs-1.12/lib/strnlen1.c (revision 86d7f5d305c6adaa56ff4582ece9859d73106103)
186d7f5d3SJohn Marino /* Find the length of STRING + 1, but scan at most MAXLEN bytes.
286d7f5d3SJohn Marino    Copyright (C) 2005 Free Software Foundation, Inc.
386d7f5d3SJohn Marino 
486d7f5d3SJohn Marino    This program is free software; you can redistribute it and/or modify it
586d7f5d3SJohn Marino    under the terms of the GNU Library General Public License as published
686d7f5d3SJohn Marino    by the Free Software Foundation; either version 2, or (at your option)
786d7f5d3SJohn Marino    any later version.
886d7f5d3SJohn Marino 
986d7f5d3SJohn Marino    This program is distributed in the hope that it will be useful,
1086d7f5d3SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
1186d7f5d3SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
1286d7f5d3SJohn Marino    Library General Public License for more details.
1386d7f5d3SJohn Marino 
1486d7f5d3SJohn Marino    You should have received a copy of the GNU Library General Public
1586d7f5d3SJohn Marino    License along with this program; if not, write to the Free Software
1686d7f5d3SJohn Marino    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
1786d7f5d3SJohn Marino    USA.  */
1886d7f5d3SJohn Marino 
1986d7f5d3SJohn Marino #ifdef HAVE_CONFIG_H
2086d7f5d3SJohn Marino # include <config.h>
2186d7f5d3SJohn Marino #endif
2286d7f5d3SJohn Marino 
2386d7f5d3SJohn Marino /* Specification.  */
2486d7f5d3SJohn Marino #include "strnlen1.h"
2586d7f5d3SJohn Marino 
2686d7f5d3SJohn Marino #include <string.h>
2786d7f5d3SJohn Marino 
2886d7f5d3SJohn Marino /* Find the length of STRING + 1, but scan at most MAXLEN bytes.
2986d7f5d3SJohn Marino    If no '\0' terminator is found in that many characters, return MAXLEN.  */
3086d7f5d3SJohn Marino /* This is the same as strnlen (string, maxlen - 1) + 1.  */
3186d7f5d3SJohn Marino size_t
strnlen1(const char * string,size_t maxlen)3286d7f5d3SJohn Marino strnlen1 (const char *string, size_t maxlen)
3386d7f5d3SJohn Marino {
3486d7f5d3SJohn Marino   const char *end = memchr (string, '\0', maxlen);
3586d7f5d3SJohn Marino   if (end != NULL)
3686d7f5d3SJohn Marino     return end - string + 1;
3786d7f5d3SJohn Marino   else
3886d7f5d3SJohn Marino     return maxlen;
3986d7f5d3SJohn Marino }
40