Browse Source

trimmed_strdup helper function

Luke Dashjr 12 years ago
parent
commit
e11a58cd06
2 changed files with 20 additions and 0 deletions
  1. 18 0
      util.c
  2. 2 0
      util.h

+ 18 - 0
util.c

@@ -2939,6 +2939,24 @@ void _bytes_alloc_failure(size_t sz)
 }
 }
 
 
 
 
+char *trimmed_strdup(const char *s)
+{
+	size_t n;
+	char *c;
+	
+	while (isspace(s[0]))
+		++s;
+	n = strlen(s) - 1;
+	while (isspace(s[n]))
+		--n;
+	++n;
+	c = malloc(n + 1);
+	c[n] = '\0';
+	memcpy(c, s, n);
+	return c;
+}
+
+
 void *cmd_thread(void *cmdp)
 void *cmd_thread(void *cmdp)
 {
 {
 	const char *cmd = cmdp;
 	const char *cmd = cmdp;

+ 2 - 0
util.h

@@ -498,6 +498,8 @@ void maybe_strdup_if_null(const char **p, const char *s)
 		*p = maybe_strdup(s);
 		*p = maybe_strdup(s);
 }
 }
 
 
+extern char *trimmed_strdup(const char *);
+
 
 
 extern void run_cmd(const char *cmd);
 extern void run_cmd(const char *cmd);