[Libguestfs] [PATCH 1/2] daemon: Add a trim utility function.

Richard W.M. Jones rjones at redhat.com
Thu Mar 18 14:00:00 UTC 2010


-- 
Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones
Read my programming blog: http://rwmj.wordpress.com
Fedora now supports 80 OCaml packages (the OPEN alternative to F#)
http://cocan.org/getting_started_with_ocaml_on_red_hat_and_fedora
-------------- next part --------------
>From c17af108227b2f0104cf5fb81224f9b743843d73 Mon Sep 17 00:00:00 2001
From: Richard Jones <rjones at redhat.com>
Date: Thu, 18 Mar 2010 13:46:26 +0000
Subject: [PATCH 1/2] daemon: Add a trim utility function.

This function trims the whitespace from around a string.  It
does this in-place, so it can be called for malloc'd strings.
---
 daemon/daemon.h   |    2 ++
 daemon/guestfsd.c |   22 ++++++++++++++++++++++
 2 files changed, 24 insertions(+), 0 deletions(-)

diff --git a/daemon/daemon.h b/daemon/daemon.h
index bb1ebb3..777cf33 100644
--- a/daemon/daemon.h
+++ b/daemon/daemon.h
@@ -66,6 +66,8 @@ extern int commandrvf (char **stdoutput, char **stderror, int flags,
 
 extern char **split_lines (char *str);
 
+extern void trim (char *str);
+
 extern int device_name_translation (char *device, const char *func);
 
 extern void udev_settle (void);
diff --git a/daemon/guestfsd.c b/daemon/guestfsd.c
index 0fc0128..ec3db58 100644
--- a/daemon/guestfsd.c
+++ b/daemon/guestfsd.c
@@ -928,6 +928,28 @@ split_lines (char *str)
   return lines;
 }
 
+/* Skip leading and trailing whitespace, updating the original string
+ * in-place.
+ */
+void
+trim (char *str)
+{
+  size_t len = strlen (str);
+
+  while (len > 0 && c_isspace (str[len-1])) {
+    str[len-1] = '\0';
+    len--;
+  }
+
+  char *p = str;
+  while (*p && c_isspace (*p)) {
+    p++;
+    len--;
+  }
+
+  memmove (str, p, len+1);
+}
+
 /* printf helper function so we can use %Q ("quoted") and %R to print
  * shell-quoted strings.  See HACKING file for more details.
  */
-- 
1.6.5.2



More information about the Libguestfs mailing list