Path: csiph.com!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!eternal-september.org!.POSTED!not-for-mail From: Keith Thompson Newsgroups: comp.lang.c Subject: Re: is_binary_file() Date: Mon, 08 Dec 2025 14:43:58 -0800 Organization: None to speak of Lines: 33 Message-ID: <871pl4fvbl.fsf@example.invalid> References: <10gvvh8$1vv6e$1@dont-email.me> MIME-Version: 1.0 Content-Type: text/plain Injection-Date: Mon, 08 Dec 2025 22:43:59 +0000 (UTC) Injection-Info: dont-email.me; posting-host="47bb24738c82d1d1e25e7d99eaef8eeb"; logging-data="495109"; mail-complaints-to="abuse@eternal-september.org"; posting-account="U2FsdGVkX1969ybe8gqHN8LDjtT7ERoS" User-Agent: Gnus/5.13 (Gnus v5.13) Cancel-Lock: sha1:RE605cf4QmrXnffLeNq+xpONsRc= sha1:32eyVCbpbcbyxOQjurGhfggyIWk= Xref: csiph.com comp.lang.c:395730 Michael Sanders writes: [...] For yet another set of unreliable hueristics for guessing whether a file is text or binary, you can take a look at Perl's built-in "-T" and "-B" operators. The "-T" and "-B" tests work as follows. The first block or so of the file is examined to see if it is valid UTF-8 that includes non-ASCII characters. If so, it's a "-T" file. Otherwise, that same portion of the file is examined for odd characters such as strange control codes or characters with the high bit set. If more than a third of the characters are strange, it's a "-B" file; otherwise it's a "-T" file. Also, any file containing a zero byte in the examined portion is considered a binary file. (If executed within the scope of a use locale which includes "LC_CTYPE", odd characters are anything that isn't a printable nor space in the current locale.) If "-T" or "-B" is used on a filehandle, the current IO buffer is examined rather than the first block. Both "-T" and "-B" return true on an empty file, or a file at EOF when testing a filehandle. Because you have to read a file to do the "-T" test, on most occasions you want to use a "-f" against the file first, as in "next unless -f $file && -T $file". It's not clear how big a "block" is. For an empty file, both -T and -B are true. I don't know whether there are other cases where both are true, or where both are false. -- Keith Thompson (The_Other_Keith) Keith.S.Thompson+u@gmail.com void Void(void) { Void(); } /* The recursive call of the void */