From 8b499aa9cddde937feab4c73f55b67529786c487 Mon Sep 17 00:00:00 2001 From: Joachim Nilsson Date: Sat, 4 Jun 2016 18:51:25 +0200 Subject: [PATCH] Add compat strnlen() implementation for non-GLIBC systems Signed-off-by: Joachim Nilsson --- Makefile.am | 2 +- README.md | 4 ++++ strlite.h | 1 + strnlen.h | 44 ++++++++++++++++++++++++++++++++++++++++++++ 4 files changed, 50 insertions(+), 1 deletion(-) create mode 100644 strnlen.h diff --git a/Makefile.am b/Makefile.am index 2c887eb..09d6fa7 100644 --- a/Makefile.am +++ b/Makefile.am @@ -2,7 +2,7 @@ ACLOCAL_AMFLAGS = -I m4 lib_LTLIBRARIES = libite.la pkgincludedir = $(includedir)/lite -pkginclude_HEADERS = lite.h queue.h conio.h tree.h strlite.h strdupa.h strndupa.h +pkginclude_HEADERS = lite.h queue.h conio.h tree.h strlite.h strdupa.h strndupa.h strnlen.h libite_la_SOURCES = chomp.c copyfile.c dir.c fexist.c fisdir.c fmode.c fparseln.c \ fsendfile.c ifconfig.c lfile.c makepath.c pidfile.c pidfilefn.c \ progress.c reallocarray.c rsync.c strlcpy.c strlcat.c strtonum.c \ diff --git a/README.md b/README.md index 92a783d..3b3b8e6 100644 --- a/README.md +++ b/README.md @@ -296,6 +296,10 @@ some other platforms. http://man7.org/linux/man-pages/man3/strndupa.3.html +- `strnlen(str, lim)` + + http://man7.org/linux/man-pages/man3/strnlen.3.html + OpenBSD/NetBSD/FreeBSD Functions -------------------------------- diff --git a/strlite.h b/strlite.h index dba09af..397c689 100644 --- a/strlite.h +++ b/strlite.h @@ -27,6 +27,7 @@ #include #include "strdupa.h" #include "strndupa.h" +#include "strnlen.h" #ifndef strlcpy size_t strlcpy (char *dst, const char *src, size_t siz); diff --git a/strnlen.h b/strnlen.h new file mode 100644 index 0000000..c993c52 --- /dev/null +++ b/strnlen.h @@ -0,0 +1,44 @@ +/* strnlen.h - Re-implementation of GLIBC strnlen() + * + * Copyright (c) 2016 Joachim Nilsson + * + * Permission to use, copy, modify, and/or distribute this software for any + * purpose with or without fee is hereby granted, provided that the above + * copyright notice and this permission notice appear in all copies. + * + * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES + * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF + * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR + * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES + * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN + * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF + * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. + */ + +#ifndef LITE_STRNLEN_H +#define LITE_STRNLEN_H + +#if !defined(HAVE_STRNLEN) +#if defined(strnlen) +#define HAVE_STRNLEN 1 +#endif +#endif + + +#if !HAVE_STRNLEN +#include /* size_t */ + +#define strnlen(str, lim) xstrnlen(str, lim) + +static inline size_t xstrnlen(const char *str, size_t lim) +{ + size_t i = 0; + + while (str[i] && i < lim) + i++; + + return i; +} +#endif + +#endif /* LITE_STRNLEN_H */