aboutsummaryrefslogtreecommitdiff
path: root/src/utils.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/utils.c')
-rw-r--r--src/utils.c24
1 files changed, 24 insertions, 0 deletions
diff --git a/src/utils.c b/src/utils.c
index 5fba4dc..56cca93 100644
--- a/src/utils.c
+++ b/src/utils.c
@@ -1,4 +1,5 @@
#include "../include/cfetch.h"
+#include <string.h>
/* function: char *string_format(char* string)
* input: pointer to a null-terminated string (must not be NULL)
@@ -109,3 +110,26 @@ char *concat(const char *s1, const char *s2) {
memcpy(result + len1, s2, len2 + 1); // +1 for null-terminator
return result; //caller needs to free memory
}
+
+
+char *trim_whitespace(char* str)
+{
+ char *end;
+
+ // Trim leading spaces
+ while (*str == ' ' || *str == '\t' || *str == '\n' || *str == '\r') {
+ str++;
+ }
+
+ if (*str == '\0') return str; // all spaces
+
+ // Trim trailing space
+ end = str + strlen(str) - 1;
+
+ while (end > str && (*end == ' ' || *end == '\t' || *end == '\r' || *end == '\n')) end--;
+
+ // Write new null terminator character
+ end[1] = '\0';
+
+ return str;
+}