| 1 | // Licensed to the .NET Foundation under one or more agreements. |
| 2 | // The .NET Foundation licenses this file to you under the MIT license. |
| 3 | // See the LICENSE file in the project root for more information. |
| 4 | |
| 5 | /*** |
| 6 | *strlen_s.c - contains strnlen() routine |
| 7 | * |
| 8 | |
| 9 | * |
| 10 | *Purpose: |
| 11 | * strnlen returns the length of a null-terminated string, |
| 12 | * not including the null byte itself, up to the specified max size |
| 13 | * |
| 14 | *******************************************************************************/ |
| 15 | |
| 16 | |
| 17 | #include <string.h> |
| 18 | #include <errno.h> |
| 19 | #include <limits.h> |
| 20 | #include "internal_securecrt.h" |
| 21 | |
| 22 | #include "mbusafecrt_internal.h" |
| 23 | |
| 24 | /*** |
| 25 | *strnlen - return the length of a null-terminated string |
| 26 | * |
| 27 | *Purpose: |
| 28 | * Finds the length in bytes of the given string, not including |
| 29 | * the final null character. Only the first maxsize characters |
| 30 | * are inspected: if the null character is not found, maxsize is |
| 31 | * returned. |
| 32 | * |
| 33 | *Entry: |
| 34 | * const char * str - string whose length is to be computed |
| 35 | * size_t maxsize |
| 36 | * |
| 37 | *Exit: |
| 38 | * Length of the string "str", exclusive of the final null byte, or |
| 39 | * maxsize if the null character is not found. |
| 40 | * |
| 41 | *Exceptions: |
| 42 | * |
| 43 | *******************************************************************************/ |
| 44 | |
| 45 | size_t __cdecl PAL_strnlen(const char *str, size_t maxsize) |
| 46 | { |
| 47 | size_t n; |
| 48 | |
| 49 | /* Note that we do not check if str == NULL, because we do not |
| 50 | * return errno_t... |
| 51 | */ |
| 52 | |
| 53 | for (n = 0; n < maxsize && *str; n++, str++) |
| 54 | ; |
| 55 | |
| 56 | return n; |
| 57 | } |
| 58 | |
| 59 | |