From 8abcf2900297c6d53ead867c42f7c1688e8d52ca Mon Sep 17 00:00:00 2001 From: Davidlohr Bueso Date: Tue, 16 Nov 2010 10:47:35 -0300 Subject: lib: [strutils] general purpose string handling functions This patch replaces a few functions used throughout the source: * Renames getnum (from schedutils) to strtol_or_err * Moves strtosize (from lib/strtosize.c) * Moves xstrncpy (from include/xstrncpy.h) * Adds strnlen, strnchr and strndup if not available (remove it from libmount utils) A few Makefile.am files were modified to compile accordingly along with trivial renaming in schedutils source code. Signed-off-by: Davidlohr Bueso --- include/strutils.h | 26 ++++++++++++++++++++++++++ 1 file changed, 26 insertions(+) create mode 100644 include/strutils.h (limited to 'include/strutils.h') diff --git a/include/strutils.h b/include/strutils.h new file mode 100644 index 000000000..e24496d3c --- /dev/null +++ b/include/strutils.h @@ -0,0 +1,26 @@ +#ifndef UTIL_LINUX_STRUTILS +#define UTIL_LINUX_STRUTILS + +#include +#include + +extern int strtosize(const char *str, uintmax_t *res); +extern long strtol_or_err(const char *str, const char *errmesg); + +#ifndef HAVE_STRNLEN +extern size_t strnlen(const char *s, size_t maxlen); +#endif +#ifndef HAVE_STRNDUP +extern char *strndup(const char *s, size_t n); +#endif +#ifndef HAVE_STRNCHR +extern char *strnchr(const char *s, size_t maxlen, int c); +#endif + +/* caller guarantees n > 0 */ +static inline void xstrncpy(char *dest, const char *src, size_t n) +{ + strncpy(dest, src, n-1); + dest[n-1] = 0; +} +#endif -- cgit v1.2.3-55-g7522