---
 tests/file.test     | 21 +++++++++++++++++++++
 toys/pending/file.c |  5 ++++-
 2 files changed, 25 insertions(+), 1 deletion(-)
 create mode 100644 tests/file.test
From 04562ab2b0969ed8298d04940521875a976f219f Mon Sep 17 00:00:00 2001
From: Elliott Hughes <[email protected]>
Date: Mon, 29 Feb 2016 19:35:13 -0800
Subject: [PATCH] Fix file for Java class files, improve script detection, and
 add tests.

---
 tests/file.test     | 21 +++++++++++++++++++++
 toys/pending/file.c |  5 ++++-
 2 files changed, 25 insertions(+), 1 deletion(-)
 create mode 100644 tests/file.test

diff --git a/tests/file.test b/tests/file.test
new file mode 100644
index 0000000..86ddd9e
--- /dev/null
+++ b/tests/file.test
@@ -0,0 +1,21 @@
+#!/bin/bash
+
+[ -f testing.sh ] && . testing.sh
+
+#testing "name" "command" "result" "infile" "stdin"
+
+touch empty
+echo "#!/bin/bash" > bash.script
+echo "#!  /bin/bash" > bash.script2
+echo "#!  /usr/bin/env python" > env.python.script
+echo "Hello, world!" > ascii
+echo "cafebabe000000310000" | xxd -r -p > java.class
+
+testing "file empty" "file empty" "empty: empty\n" "" ""
+testing "file bash.script" "file bash.script" "bash.script: /bin/bash script\n" "" ""
+testing "file bash.script with spaces" "file bash.script2" "bash.script2: /bin/bash script\n" "" ""
+testing "file env python script" "file env.python.script" "env.python.script: python script\n" "" ""
+testing "file ascii" "file ascii" "ascii: ASCII text\n" "" ""
+testing "file java class" "file java.class" "java.class: Java class file, version 49.0\n" "" ""
+
+rm empty bash.script bash.script2 env.python.script ascii java.class
diff --git a/toys/pending/file.c b/toys/pending/file.c
index 49f987c..071c668 100644
--- a/toys/pending/file.c
+++ b/toys/pending/file.c
@@ -170,7 +170,7 @@ static void do_regular_file(int fd, char *name)
   // https://docs.oracle.com/javase/specs/jvms/se7/html/jvms-4.html
   else if (len>8 && strstart(&s, "\xca\xfe\xba\xbe"))
     xprintf("Java class file, version %d.%d\n",
-      (int)peek_be(s+6, 2), (int)peek_be(s, 2));
+      (int)peek_be(s+2, 2), (int)peek_be(s, 2));
 
   // https://people.freebsd.org/~kientzle/libarchive/man/cpio.5.txt
   // the lengths for cpio are size of header + 9 bytes, since any valid
@@ -204,6 +204,9 @@ static void do_regular_file(int fd, char *name)
 
     // If shell script, report which interpreter
     if (len>3 && strstart(&s, "#!")) {
+      // Whitespace is allowed between the #! and the interpreter
+      while (isspace(*s)) s++;
+      if (strstart(&s, "/usr/bin/env")) while (isspace(*s)) s++;
       for (what = s; (s-toybuf)<len && !isspace(*s); s++);
       strcpy(s, " script");
 
-- 
2.7.0.rc3.207.g0ac5344

_______________________________________________
Toybox mailing list
[email protected]
http://lists.landley.net/listinfo.cgi/toybox-landley.net

Reply via email to