xref: /netbsd-src/external/gpl3/gdb/dist/gnulib/import/strnlen.c (revision 4b169a6ba595ae283ca507b26b15fdff40495b1c)
18dffb485Schristos /* Find the length of STRING, but scan at most MAXLEN characters.
2*4b169a6bSchristos    Copyright (C) 2005-2007, 2009-2022 Free Software Foundation, Inc.
38dffb485Schristos    Written by Simon Josefsson.
48dffb485Schristos 
5*4b169a6bSchristos    This file is free software: you can redistribute it and/or modify
6*4b169a6bSchristos    it under the terms of the GNU Lesser General Public License as
7*4b169a6bSchristos    published by the Free Software Foundation; either version 2.1 of the
8*4b169a6bSchristos    License, or (at your option) any later version.
98dffb485Schristos 
10*4b169a6bSchristos    This file is distributed in the hope that it will be useful,
118dffb485Schristos    but WITHOUT ANY WARRANTY; without even the implied warranty of
128dffb485Schristos    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13*4b169a6bSchristos    GNU Lesser General Public License for more details.
148dffb485Schristos 
15*4b169a6bSchristos    You should have received a copy of the GNU Lesser General Public License
16*4b169a6bSchristos    along with this program.  If not, see <https://www.gnu.org/licenses/>.  */
178dffb485Schristos 
188dffb485Schristos #include <config.h>
198dffb485Schristos 
208dffb485Schristos #include <string.h>
218dffb485Schristos 
228dffb485Schristos /* Find the length of STRING, but scan at most MAXLEN characters.
238dffb485Schristos    If no '\0' terminator is found in that many characters, return MAXLEN.  */
248dffb485Schristos 
258dffb485Schristos size_t
strnlen(const char * string,size_t maxlen)268dffb485Schristos strnlen (const char *string, size_t maxlen)
278dffb485Schristos {
288dffb485Schristos   const char *end = memchr (string, '\0', maxlen);
298dffb485Schristos   return end ? (size_t) (end - string) : maxlen;
308dffb485Schristos }
31