summaryrefslogtreecommitdiffstats
path: root/src/common/util-common.c
diff options
context:
space:
mode:
authorAndrew Gregory <andrew.gregory.8@gmail.com>2015-09-15 00:37:38 +0200
committerAllan McRae <allan@archlinux.org>2015-09-20 09:36:27 +0200
commitedbe6c2bdc6a3011e5aea208c44be5eee02f120b (patch)
treebee3474adf643e425f5482813b50fec8db19fc28 /src/common/util-common.c
parent75fe6ef104b1071bf763354177f3c4339c1c7854 (diff)
downloadpacman-edbe6c2bdc6a3011e5aea208c44be5eee02f120b.tar.gz
pacman-edbe6c2bdc6a3011e5aea208c44be5eee02f120b.tar.xz
move strtrim to util-common
Signed-off-by: Andrew Gregory <andrew.gregory.8@gmail.com> Signed-off-by: Allan McRae <allan@archlinux.org>
Diffstat (limited to 'src/common/util-common.c')
-rw-r--r--src/common/util-common.c39
1 files changed, 39 insertions, 0 deletions
diff --git a/src/common/util-common.c b/src/common/util-common.c
index e8341688..542dcfde 100644
--- a/src/common/util-common.c
+++ b/src/common/util-common.c
@@ -17,6 +17,7 @@
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
+#include <ctype.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>
@@ -127,6 +128,44 @@ char *safe_fgets(char *s, int size, FILE *stream)
return ret;
}
+/* Trim whitespace and newlines from a string
+ */
+size_t strtrim(char *str)
+{
+ char *end, *pch = str;
+
+ if(str == NULL || *str == '\0') {
+ /* string is empty, so we're done. */
+ return 0;
+ }
+
+ while(isspace((unsigned char)*pch)) {
+ pch++;
+ }
+ if(pch != str) {
+ size_t len = strlen(pch);
+ if(len) {
+ memmove(str, pch, len + 1);
+ pch = str;
+ } else {
+ *str = '\0';
+ }
+ }
+
+ /* check if there wasn't anything but whitespace in the string. */
+ if(*str == '\0') {
+ return 0;
+ }
+
+ end = (str + strlen(str) - 1);
+ while(isspace((unsigned char)*end)) {
+ end--;
+ }
+ *++end = '\0';
+
+ return end - pch;
+}
+
#ifndef HAVE_STRNLEN
/* A quick and dirty implementation derived from glibc */
/** Determines the length of a fixed-size string.