Imported from ../bash-3.1.tar.gz.
[platform/upstream/bash.git] / lib / sh / strnlen.c
1 /* Copyright (C) 2004 Free Software Foundation, Inc.
2
3    This program is free software; you can redistribute it and/or modify it
4    under the terms of the GNU General Public License as published by the
5    Free Software Foundation; either version 2, or (at your option) any
6    later version.
7
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12
13    You should have received a copy of the GNU General Public License
14    along with this program; if not, write to the Free Software Foundation,
15    Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.  */
16
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
20
21 #if !defined (HAVE_STRNLEN)
22
23 #include <sys/types.h>
24
25 #if defined (HAVE_UNISTD_H)
26 #  include <unistd.h>
27 #endif
28
29 #include <stdc.h>
30
31 /* Find the length of S, but scan at most MAXLEN characters.  If no '\0'
32    terminator is found within the first MAXLEN characters, return MAXLEN. */
33 size_t
34 strnlen (s, maxlen)
35      register const char *s;
36      size_t maxlen;
37 {
38   register const char *e;
39   size_t n;
40
41   for (e = s, n = 0; *e && n < maxlen; e++, n++)
42     ;
43   return n;
44 }
45 #endif