openpkg/patch.strnlen.c

changeset 428
f880f219c566
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/openpkg/patch.strnlen.c	Tue Jul 31 12:23:42 2012 +0200
     1.3 @@ -0,0 +1,31 @@
     1.4 +/* Find the length of STRING, but scan at most MAXLEN characters.
     1.5 +   Copyright (C) 2005, 2006, 2007, 2009 Free Software Foundation, Inc.
     1.6 +   Written by Simon Josefsson.
     1.7 +
     1.8 +   This program is free software; you can redistribute it and/or modify
     1.9 +   it under the terms of the GNU General Public License as published by
    1.10 +   the Free Software Foundation; either version 2, or (at your option)
    1.11 +   any later version.
    1.12 +
    1.13 +   This program is distributed in the hope that it will be useful,
    1.14 +   but WITHOUT ANY WARRANTY; without even the implied warranty of
    1.15 +   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    1.16 +   GNU General Public License for more details.
    1.17 +
    1.18 +   You should have received a copy of the GNU General Public License
    1.19 +   along with this program; if not, write to the Free Software Foundation,
    1.20 +   Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.  */
    1.21 +
    1.22 +#include <config.h>
    1.23 +
    1.24 +#include <string.h>
    1.25 +
    1.26 +/* Find the length of STRING, but scan at most MAXLEN characters.
    1.27 +   If no '\0' terminator is found in that many characters, return MAXLEN.  */
    1.28 +
    1.29 +size_t
    1.30 +strnlen (const char *string, size_t maxlen)
    1.31 +{
    1.32 +  const char *end = memchr (string, '\0', maxlen);
    1.33 +  return end ? (size_t) (end - string) : maxlen;
    1.34 +}

mercurial