[PATCH 1/2] kconfig: add chomp like helper function

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Add a helper function that strips trailing new lines and carriage
returns from strings. Call it chomp, after the perl function that
inspired it.

Signed-off-by: Paul Bolle <pebolle@xxxxxxxxxx>
---
 scripts/kconfig/confdata.c | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/scripts/kconfig/confdata.c b/scripts/kconfig/confdata.c
index 0b7dc2fd7bac..51904c423411 100644
--- a/scripts/kconfig/confdata.c
+++ b/scripts/kconfig/confdata.c
@@ -248,6 +248,28 @@ e_out:
 	return -1;
 }
 
+/*
+ * Return newly allocated copy of string "in" with all trailing new lines and
+ * carriage returns removed.
+ */
+static char *chomp(char *in)
+{
+	size_t last = strlen(in);
+	char *copy;
+
+	copy = malloc(last + 1);
+	if (!copy)
+		return NULL;
+
+	strcpy(copy, in);
+	if (last)
+		last--;
+	while (last  && (copy[last] == '\r' || copy[last] == '\n'))
+		copy[last--] = '\0';
+
+	return copy;
+}
+
 int conf_read_simple(const char *name, int def)
 {
 	FILE *in = NULL;
-- 
2.4.3

--
To unsubscribe from this list: send the line "unsubscribe linux-kbuild" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Linux&nblp;USB Development]     [Linux Media]     [Video for Linux]     [Linux Audio Users]     [Yosemite Secrets]     [Linux Kernel]     [Linux SCSI]

  Powered by Linux