threads / patch / 3671

patchls-files: Don't require exclude files to end with a newline.

Subject: [PATCH] ls-files: Don't require exclude files to end with a newline.

## tl;dr

3 messages between Mar 18, 2006 and Mar 20, 2006. Diffs are folded; open one to read it.

replies: 2people: 2as markdown or json

Alexandre Julliard· Mar 18, 2006, 10:27 UTC · lore

Without this patch, the last line of an exclude file is silently ignored if it doesn't end with a newline.

Signed-off-by: Alexandre Julliard <julliard@winehq.org>
---
 ls-files.c |    3 ++-
 1 files changed, 2 insertions(+), 1 deletions(-)
9c5a78851bcf3b541364f818f6b397c29a8f4592
Show changes to ls-files.c +2 −1
diff --git a/ls-files.c b/ls-files.c
index df25c8c..e42119c 100644
--- a/ls-files.c
+++ b/ls-files.c
@@ -92,11 +92,12 @@ static int add_excludes_from_file_1(cons
 		close(fd);
 		return 0;
 	}
-	buf = xmalloc(size);
+	buf = xmalloc(size+1);
 	if (read(fd, buf, size) != size)
 		goto err;
 	close(fd);
 
+	buf[size++] = '\n';
 	entry = buf;
 	for (i = 0; i < size; i++) {
 		if (buf[i] == '\n') {
-- 
1.2.4.g5a1f-dirty

-- 
Alexandre Julliard
julliard@winehq.org
Petr Baudis· Mar 19, 2006, 21:29 UTC · re: Alexandre Julliard · lore

Re: [PATCH] ls-files: Don't require exclude files to end with a newline.

Dear diary, on Sat, Mar 18, 2006 at 11:27:45AM CET, I got a letter where Alexandre Julliard <julliard@winehq.org> said that...

> Without this patch, the last line of an exclude file is silently
> ignored if it doesn't end with a newline.
> 
> Signed-off-by: Alexandre Julliard <julliard@winehq.org>

$ echo -en "a\nb" | wc -l 1

In UNIX, a line is a string terminated by a newline, therefore the blob past the last newline character is not really a line at all. ;-)

Perhaps a warning might be in order. Why don't you just add the trailing newline to the file?

-- 
				Petr "Pasky" Baudis
Stuff: http://pasky.or.cz/
Right now I am having amnesia and deja-vu at the same time.  I think
I have forgotten this before.
Alexandre Julliard· Mar 20, 2006, 09:14 UTC · re: Petr Baudis · lore

Re: [PATCH] ls-files: Don't require exclude files to end with a newline.

Petr Baudis <pasky@suse.cz> writes:
Show 15 quoted lines
> Dear diary, on Sat, Mar 18, 2006 at 11:27:45AM CET, I got a letter
> where Alexandre Julliard <julliard@winehq.org> said that...
>> Without this patch, the last line of an exclude file is silently
>> ignored if it doesn't end with a newline.
>> 
>> Signed-off-by: Alexandre Julliard <julliard@winehq.org>
>
> $ echo -en "a\nb" | wc -l
> 1
>
> In UNIX, a line is a string terminated by a newline, therefore the blob
> past the last newline character is not really a line at all. ;-)
>
> Perhaps a warning might be in order. Why don't you just add the trailing
> newline to the file?

Of course I can do that, but I think that if a user entered something on the last line it's a safe bet they didn't mean for it to be ignored; and since it's trivial to DTRT here, I don't see why we shouldn't.

-- 
Alexandre Julliard
julliard@winehq.org

← back to recent threads