[augeas-devel] [PATCH] transform: append missing newlines

David Lutterkort lutter at redhat.com
Fri Sep 5 17:02:36 UTC 2008


1 file changed, 14 insertions(+)
src/transform.c |   14 ++++++++++++++


# HG changeset patch
# User David Lutterkort <dlutter at redhat.com>
# Date 1220634134 25200
# Node ID e02c42a6d9391e6b61e4ed966790a98ac88f8f23
# Parent  33e975529c26c2ceaeb04d913a71d1b782ef6baa
transform: append missing newlines

If a file does not end with a newline, append one automatically. Most
lenses require that a file ends with a newline, and files that are missing
them are quite common.

diff -r 33e975529c26 -r e02c42a6d939 src/transform.c
--- a/src/transform.c	Fri Sep 05 09:41:44 2008 -0700
+++ b/src/transform.c	Fri Sep 05 10:02:14 2008 -0700
@@ -316,6 +316,19 @@
     return result;
 }
 
+static char *append_newline(char *text, size_t len) {
+    /* Try to append a newline; this is a big hack to work */
+    /* around the fact that lenses generally break if the  */
+    /* file does not end with a newline. */
+    if (len == 0 || text[len-1] != '\n') {
+        if (REALLOC_N(text, len+2) == 0) {
+            text[len] = '\n';
+            text[len+1] = '\0';
+        }
+    }
+    return text;
+}
+
 static int load_file(struct augeas *aug, struct lens *lens, char *filename) {
     char *text = NULL;
     const char *err_status = NULL;
@@ -336,6 +349,7 @@
         err_status = "read_failed";
         goto done;
     }
+    text = append_newline(text, strlen(text));
 
     struct info *info;
     make_ref(info);




More information about the augeas-devel mailing list