From 6a1b9cf10ab5141fc8ad953b69ed2e8e459693c0 Mon Sep 17 00:00:00 2001 From: Thomas Richter Date: Mon, 19 Feb 2018 13:52:57 +0000 Subject: [PATCH] s390-tools: Add function util_strstrip MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Function strstrip strips leading and trailung spaces from a given string. During review it was decided to move this function to the libutil library to make it available for other tools. Signed-off-by: Thomas Richter Signed-off-by: Jan Höppner --- include/lib/util_str.h | 25 +++++++++++++++++++++++++ libutil/Makefile | 3 ++- libutil/util_str.c | 39 +++++++++++++++++++++++++++++++++++++++ 3 files changed, 66 insertions(+), 1 deletion(-) create mode 100644 include/lib/util_str.h create mode 100644 libutil/util_str.c diff --git a/include/lib/util_str.h b/include/lib/util_str.h new file mode 100644 index 00000000..abe14a8c --- /dev/null +++ b/include/lib/util_str.h @@ -0,0 +1,25 @@ +/** + * @defgroup util_str + * @{ + * @brief Strip leading and trailing blanks + * + * Copyright IBM Corp. 2018 + * + * s390-tools is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#ifndef LIB_UTIL_STR_H +#define LIB_UTIL_STR_H + +#ifdef __cplusplus +extern "C" { +#endif + +char *util_strstrip(char *s); + +#ifdef __cplusplus +} +#endif + +#endif /** LIB_UTIL_STRSTRIP_H @} */ diff --git a/libutil/Makefile b/libutil/Makefile index fd5e2138..29b5ee76 100644 --- a/libutil/Makefile +++ b/libutil/Makefile @@ -26,7 +26,8 @@ objects = util_base.o \ util_part.o \ util_prg.o \ util_proc.o \ - util_rec.o + util_rec.o \ + util_str.o util_base_example: util_base_example.o $(lib) util_panic_example: util_panic_example.o $(lib) diff --git a/libutil/util_str.c b/libutil/util_str.c new file mode 100644 index 00000000..4bc44ec3 --- /dev/null +++ b/libutil/util_str.c @@ -0,0 +1,39 @@ +/* + * util - Utility function library + * + * Strip leading and trailing blanks. + * + * Copyright IBM Corp. 2018 + * + * s390-tools is free software; you can redistribute it and/or modify + * it under the terms of the MIT license. See LICENSE for details. + */ + +#include +#include + +#include "lib/util_str.h" + +/* + * strip leading and trailing blanks + */ +char *util_strstrip(char *s) +{ + size_t size; + char *end; + + size = strlen(s); + + if (!size) + return s; + + end = s + size - 1; + while (end >= s && isspace(*end)) + end--; + *(end + 1) = '\0'; + + while (*s && isspace(*s)) + s++; + + return s; +}