| 1 |
'\" t |
| 2 |
.\" Copyright, Bruno Haible <[email protected]> |
| 3 |
.\" Copyright, the authors of the Linux man-pages project |
| 4 |
.\" |
| 5 |
.\" SPDX-License-Identifier: GPL-2.0-or-later |
| 6 |
.\" |
| 7 |
.TH strnlen 3 2026-02-25 "Linux man-pages 6.18" |
| 8 |
.SH NAME |
| 9 |
strnlen \- determine the length of a fixed-size string |
| 10 |
.SH LIBRARY |
| 11 |
Standard C library |
| 12 |
.RI ( libc ,\~ \-lc ) |
| 13 |
.SH SYNOPSIS |
| 14 |
.nf |
| 15 |
.B #include <string.h> |
| 16 |
.P |
| 17 |
.BR "size_t strnlen(const char " s "[], size_t " maxlen ); |
| 18 |
.fi |
| 19 |
.P |
| 20 |
.RS -4 |
| 21 |
Feature Test Macro Requirements for glibc (see |
| 22 |
.BR feature_test_macros (7)): |
| 23 |
.RE |
| 24 |
.P |
| 25 |
.BR strnlen (): |
| 26 |
.nf |
| 27 |
Since glibc 2.10: |
| 28 |
_POSIX_C_SOURCE >= 200809L |
| 29 |
Before glibc 2.10: |
| 30 |
_GNU_SOURCE |
| 31 |
.fi |
| 32 |
.SH DESCRIPTION |
| 33 |
The |
| 34 |
.BR strnlen () |
| 35 |
function returns the number of bytes in the string |
| 36 |
pointed to by |
| 37 |
.IR s , |
| 38 |
excluding the terminating null byte (\[aq]\[rs]0\[aq]), |
| 39 |
but at most |
| 40 |
.IR maxlen . |
| 41 |
In doing this, |
| 42 |
.BR strnlen () |
| 43 |
looks only at the first |
| 44 |
.I maxlen |
| 45 |
characters in the string pointed to by |
| 46 |
.I s |
| 47 |
and never beyond |
| 48 |
.IR s[maxlen\-1] . |
| 49 |
.P |
| 50 |
It is equivalent to |
| 51 |
.P |
| 52 |
.in +4n |
| 53 |
.EX |
| 54 |
(memchr(s, \[aq]\[rs]0\[aq], maxlen) ?: s + maxlen) - s |
| 55 |
.EE |
| 56 |
.in |
| 57 |
.SH RETURN VALUE |
| 58 |
The |
| 59 |
.BR strnlen () |
| 60 |
function returns |
| 61 |
.IR strlen(s) , |
| 62 |
if that is less than |
| 63 |
.IR maxlen , |
| 64 |
or |
| 65 |
.I maxlen |
| 66 |
if there is no null terminating (\[aq]\[rs]0\[aq]) among the first |
| 67 |
.I maxlen |
| 68 |
characters pointed to by |
| 69 |
.IR s . |
| 70 |
.SH ATTRIBUTES |
| 71 |
For an explanation of the terms used in this section, see |
| 72 |
.BR attributes (7). |
| 73 |
.TS |
| 74 |
allbox; |
| 75 |
lbx lb lb |
| 76 |
l l l. |
| 77 |
Interface Attribute Value |
| 78 |
T{ |
| 79 |
.na |
| 80 |
.nh |
| 81 |
.BR strnlen () |
| 82 |
T} Thread safety MT-Safe |
| 83 |
.TE |
| 84 |
.SH STANDARDS |
| 85 |
POSIX.1-2008. |
| 86 |
.SH HISTORY |
| 87 |
POSIX.1-2008. |
| 88 |
.SH SEE ALSO |
| 89 |
.BR strlen (3) |