libite
strnlen.h
Go to the documentation of this file.
1 /* strnlen.h - Re-implementation of GLIBC strnlen()
2  *
3  * Copyright (c) 2016-2021 Joachim Wiberg <troglobit@gmail.com>
4  *
5  * Permission to use, copy, modify, and/or distribute this software for any
6  * purpose with or without fee is hereby granted, provided that the above
7  * copyright notice and this permission notice appear in all copies.
8  *
9  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
10  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
11  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
12  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
13  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
14  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
15  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
16  */
17 
25 #ifdef __cplusplus
26 extern "C"
27 {
28 #endif
29 
30 #ifndef LIBITE_STRNLEN_H_
31 #define LIBITE_STRNLEN_H_
32 
33 #if !defined(HAVE_STRNLEN)
34 #if defined(strnlen)
35 #define HAVE_STRNLEN 1
36 #endif
37 #endif
38 
39 #if !HAVE_STRNLEN
40 #include <stddef.h> /* size_t */
41 
42 #define strnlen(str, lim) xstrnlen(str, lim)
50 static inline size_t xstrnlen(const char *str, size_t lim)
51 {
52  size_t i = 0;
53 
54  while (i < lim && str[i])
55  i++;
56 
57  return i;
58 }
59 #endif
60 
61 #endif /* LIBITE_STRNLEN_H_ */
62 
63 #ifdef __cplusplus
64 }
65 #endif