xref: /dflybsd-src/contrib/grep/lib/strnlen1.c (revision 91b9ed38d3db6a8a8ac5b66da1d43e6e331e259a)
195b7b453SJohn Marino /* Find the length of STRING + 1, but scan at most MAXLEN bytes.
2*09d4459fSDaniel Fojt    Copyright (C) 2005-2006, 2009-2020 Free Software Foundation, Inc.
395b7b453SJohn Marino 
495b7b453SJohn Marino    This program is free software: you can redistribute it and/or modify
595b7b453SJohn Marino    it under the terms of the GNU General Public License as published by
695b7b453SJohn Marino    the Free Software Foundation; either version 3 of the License, or
795b7b453SJohn Marino    (at your option) any later version.
895b7b453SJohn Marino 
995b7b453SJohn Marino    This program is distributed in the hope that it will be useful,
1095b7b453SJohn Marino    but WITHOUT ANY WARRANTY; without even the implied warranty of
1195b7b453SJohn Marino    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
1295b7b453SJohn Marino    GNU General Public License for more details.
1395b7b453SJohn Marino 
1495b7b453SJohn Marino    You should have received a copy of the GNU General Public License
15*09d4459fSDaniel Fojt    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
1695b7b453SJohn Marino 
1795b7b453SJohn Marino #include <config.h>
1895b7b453SJohn Marino 
1995b7b453SJohn Marino /* Specification.  */
2095b7b453SJohn Marino #include "strnlen1.h"
2195b7b453SJohn Marino 
2295b7b453SJohn Marino #include <string.h>
2395b7b453SJohn Marino 
2495b7b453SJohn Marino /* Find the length of STRING + 1, but scan at most MAXLEN bytes.
2595b7b453SJohn Marino    If no '\0' terminator is found in that many characters, return MAXLEN.  */
2695b7b453SJohn Marino /* This is the same as strnlen (string, maxlen - 1) + 1.  */
2795b7b453SJohn Marino size_t
strnlen1(const char * string,size_t maxlen)2895b7b453SJohn Marino strnlen1 (const char *string, size_t maxlen)
2995b7b453SJohn Marino {
3095b7b453SJohn Marino   const char *end = (const char *) memchr (string, '\0', maxlen);
3195b7b453SJohn Marino   if (end != NULL)
3295b7b453SJohn Marino     return end - string + 1;
3395b7b453SJohn Marino   else
3495b7b453SJohn Marino     return maxlen;
3595b7b453SJohn Marino }
36