? gtags/test_isbinary
? gtags/test_isbinary-1.c
Index: gtags/Makefile.am
===================================================================
RCS file: /sources/global/global/gtags/Makefile.am,v
retrieving revision 1.4
diff -u -p -r1.4 Makefile.am
--- gtags/Makefile.am	5 Jul 2002 07:16:46 -0000	1.4
+++ gtags/Makefile.am	24 Nov 2009 12:48:06 -0000
@@ -10,13 +10,14 @@
 # WITHOUT ANY WARRANTY, to the extent permitted by law; without even the
 # implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 #
-bin_PROGRAMS= gtags
+bin_PROGRAMS= gtags test_isbinary
 
 INCLUDES = @INCLUDES@
 
 LDADD = @LDADD@
 
 gtags_DEPENDENCIES = $(LDADD)
+test_isbinary_DEPENDENCIES = $(LDADD)
 
 man_MANS = gtags.1
 
Index: gtags/test_isbinary.c
===================================================================
RCS file: gtags/test_isbinary.c
diff -N gtags/test_isbinary.c
--- /dev/null	1 Jan 1970 00:00:00 -0000
+++ gtags/test_isbinary.c	24 Nov 2009 12:48:06 -0000
@@ -0,0 +1,61 @@
+#include <sys/types.h>
+#include <sys/stat.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <unistd.h>
+
+#include "global.h"
+
+#define BUFFERSIZE	(8*1024)
+
+static int readsize = 32;
+const char *progname = "test_isbinary";
+
+static int
+is_binary(const char *path)
+{
+	int ip;
+	char buf[BUFFERSIZE];
+	int i, size;
+
+	ip = open(path, O_RDONLY);
+	if (ip < 0)
+		die("cannot open file '%s' in read mode.", path);
+	size = read(ip, buf, readsize);
+	close(ip);
+	if (size < 0)
+		return 1;
+	if (size >= 7 && locatestring(buf, "!<arch>", MATCH_AT_FIRST))
+		return 1;
+	for (i = 0; i < size; i++) {
+		if (isbinarychar(buf[i])) {
+//			printf("%04x:%02x ",  i,(unsigned char)buf[i]);
+			return 1;
+		}
+	}
+	return 0;
+}
+
+int
+main(int argc, char **argv)
+{
+	const char *path;
+	STRBUF *sb = strbuf_open(0);
+
+	if (argc > 1)
+		readsize = atoi(argv[1]);
+	if (readsize < 32)
+		die("readsize is too small.");
+	if (readsize > BUFFERSIZE)
+		die("readsize is too large.");
+
+	while ((path = strbuf_fgets(sb, stdin, STRBUF_NOCRLF)) != NULL) {
+//		if (test("b", path))
+		if (is_binary(path))
+			puts(path);
+	}
+
+	strbuf_close(sb);
+	return 0;
+}
Index: libutil/char.c
===================================================================
RCS file: /sources/global/global/libutil/char.c,v
retrieving revision 1.15
diff -u -p -r1.15 char.c
--- libutil/char.c	5 Jul 2007 06:52:02 -0000	1.15
+++ libutil/char.c	24 Nov 2009 12:48:06 -0000
@@ -25,6 +25,7 @@
 #include "char.h"
 #include "strbuf.h"
 
+#define B	BINARYCHAR
 #define R	REGEXCHAR
 #define U	URLCHAR
 #define RU	REGEXCHAR | URLCHAR
@@ -32,8 +33,8 @@ const unsigned char chartype[256] = {
 #if '\n' == 0x0a && ' ' == 0x20 && '0' == 0x30 \
   && 'A' == 0x41 && 'a' == 0x61 && '!' == 0x21
 	/* ASCII */
-	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
-	0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
+	B, B, B, B, B, B, B, B, 0, 0, 0, 0, 0, 0, B, B,
+	B, B, B, B, B, B, B, B, B, B, B, 0, B, B, B, B,
 	0, U, 0, 0, R, 0, 0, U,RU,RU,RU, R, 0, U,RU, U,	/*  !"#$%&'()*+,-./ */
 	U, U, U, U, U, U, U, U, U, U, 0, 0, 0, 0, 0, R,	/* 0123456789:;<=>? */
 	0, U, U, U, U, U, U, U, U, U, U, U, U, U, U, U,	/* @ABCDEFGHIJKLMNO */
Index: libutil/char.h
===================================================================
RCS file: /sources/global/global/libutil/char.h,v
retrieving revision 1.8
diff -u -p -r1.8 char.h
--- libutil/char.h	5 Jul 2007 06:52:02 -0000	1.8
+++ libutil/char.h	24 Nov 2009 12:48:06 -0000
@@ -24,6 +24,7 @@ extern const unsigned char chartype[256]
 
 #define REGEXCHAR		1
 #define URLCHAR			2
+#define BINARYCHAR		4
 #define test_chartype(c, t)	(chartype[(unsigned char)(c)] & (t))
 
 /* test whether or not regular expression char. */
@@ -32,6 +33,9 @@ extern const unsigned char chartype[256]
 /* test whether can be included in URL without escaping. */
 #define isurlchar(c)		test_chartype(c, URLCHAR)
 
+/* test whether or not cahr included in binary file. */
+#define isbinarychar(c)		test_chartype(c, BINARYCHAR)
+
 int isregex(const char *);
 const char *quote_string(const char *);
 
Index: libutil/test.c
===================================================================
RCS file: /sources/global/global/libutil/test.c,v
retrieving revision 1.15
diff -u -p -r1.15 test.c
--- libutil/test.c	19 Jul 2007 14:08:22 -0000	1.15
+++ libutil/test.c	24 Nov 2009 12:48:06 -0000
@@ -38,6 +38,7 @@
 #endif
 
 #include "locatestring.h"
+#include "char.h"
 #include "die.h"
 #include "test.h"
 
@@ -52,7 +53,7 @@ is_binary(const char *path)
 {
 	int ip;
 	char buf[32];
-	int i, c, size;
+	int i, size;
 
 	ip = open(path, O_RDONLY);
 	if (ip < 0)
@@ -64,8 +65,7 @@ is_binary(const char *path)
 	if (size >= 7 && locatestring(buf, "!<arch>", MATCH_AT_FIRST))
 		return 1;
 	for (i = 0; i < size; i++) {
-		c = (unsigned char)buf[i];
-		if (c == 0 || c > 127)
+		if (isbinarychar(buf[i]))
 			return 1;
 	}
 	return 0;
