File: strnlen.c

package info (click to toggle)
picolibc 1.8.11-2
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 50,064 kB
  • sloc: ansic: 404,031; asm: 24,984; sh: 2,585; python: 2,289; perl: 680; pascal: 329; exp: 287; makefile: 222; cpp: 71; xml: 40
file content (41 lines) | stat: -rw-r--r-- 848 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
/* Copyright (c) 2002 Jeff Johnston <jjohnstn@redhat.com> */
/*
FUNCTION
        <<strnlen>>---character string length

INDEX
        strnlen

SYNOPSIS
        #include <string.h>
        size_t strnlen(const char *<[str]>, size_t <[n]>);

DESCRIPTION
        The <<strnlen>> function works out the length of the string
        starting at <<*<[str]>>> by counting chararacters until it
        reaches a NUL character or the maximum: <[n]> number of
        characters have been inspected.

RETURNS
        <<strnlen>> returns the character count or <[n]>.

PORTABILITY
<<strnlen>> is a GNU extension.

<<strnlen>> requires no supporting OS subroutines.

*/

#undef __STRICT_ANSI__
#include <string.h>

size_t
strnlen(const char *str, size_t n)
{
    const char *start = str;

    while (n-- > 0 && *str)
        str++;

    return str - start;
}