Util-Linux Archive on lore.kernel.org
 help / color / Atom feed
* [PATCH] more: avoid libmagic telling an empty file is binary
@ 2020-05-25  7:30 Sami Kerola
  2020-05-25 12:18 ` Karel Zak
  0 siblings, 1 reply; 2+ messages in thread
From: Sami Kerola @ 2020-05-25  7:30 UTC (permalink / raw)
  To: util-linux; +Cc: Sami Kerola

My earlier change that took libmagic in use to identify mime-type of an input
file caused empty files to be marked binary.  Before the change empty files
were simply displayed as empty.  This change will restore that behavior.

Addresses: 09070e1a658e70ec203150e4fa5f486b32771858
Signed-off-by: Sami Kerola <kerolasa@iki.fi>
---
 text-utils/more.c | 18 +++++++++++++++++-
 1 file changed, 17 insertions(+), 1 deletion(-)

diff --git a/text-utils/more.c b/text-utils/more.c
index b69fa5c5b..3855d8549 100644
--- a/text-utils/more.c
+++ b/text-utils/more.c
@@ -395,7 +395,23 @@ static void print_separator(const int c, int n)
 static int check_magic(struct more_control *ctl, char *fs)
 {
 #ifdef HAVE_MAGIC
-	const char *mime_encoding = magic_descriptor(ctl->magic, fileno(ctl->current_file));
+	const int fd = fileno(ctl->current_file);
+	const char *mime_encoding = magic_descriptor(ctl->magic, fd);
+	const char *magic_error_msg = magic_error(ctl->magic);
+	struct stat st;
+
+	if (magic_error_msg) {
+		printf(_("magic failed: %s\n"), magic_error_msg);
+		return 0;
+	}
+	if (fstat(fd, &st)) {
+		warn(_("cannot stat %s"), fs);
+		return 1;
+	}
+	if (st.st_size == 0) {
+		/* libmagic tells an empty file has binary encoding */
+		return 0;
+	}
 
 	if (!mime_encoding || !(strcmp("binary", mime_encoding))) {
 		printf(_("\n******** %s: Not a text file ********\n\n"), fs);
-- 
2.26.2


^ permalink raw reply	[flat|nested] 2+ messages in thread

* Re: [PATCH] more: avoid libmagic telling an empty file is binary
  2020-05-25  7:30 [PATCH] more: avoid libmagic telling an empty file is binary Sami Kerola
@ 2020-05-25 12:18 ` Karel Zak
  0 siblings, 0 replies; 2+ messages in thread
From: Karel Zak @ 2020-05-25 12:18 UTC (permalink / raw)
  To: Sami Kerola; +Cc: util-linux

On Mon, May 25, 2020 at 08:30:24AM +0100, Sami Kerola wrote:
>  text-utils/more.c | 18 +++++++++++++++++-
>  1 file changed, 17 insertions(+), 1 deletion(-)

Applied, thanks.

-- 
 Karel Zak  <kzak@redhat.com>
 http://karelzak.blogspot.com


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, back to index

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2020-05-25  7:30 [PATCH] more: avoid libmagic telling an empty file is binary Sami Kerola
2020-05-25 12:18 ` Karel Zak

Util-Linux Archive on lore.kernel.org

Archives are clonable:
	git clone --mirror https://lore.kernel.org/util-linux/0 util-linux/git/0.git

	# If you have public-inbox 1.1+ installed, you may
	# initialize and index your mirror using the following commands:
	public-inbox-init -V2 util-linux util-linux/ https://lore.kernel.org/util-linux \
		util-linux@vger.kernel.org
	public-inbox-index util-linux

Example config snippet for mirrors

Newsgroup available over NNTP:
	nntp://nntp.lore.kernel.org/org.kernel.vger.util-linux


AGPL code for this site: git clone https://public-inbox.org/public-inbox.git