From 2cdc8075f8109b69895bc56bd4d3f59deb6da360 Mon Sep 17 00:00:00 2001 From: kemal Date: Tue, 20 Apr 2021 01:40:31 +0200 Subject: [PATCH] file: recognize executable scripts, etc. The patch does the following: 1. Adds recognition of executable script (shebang) files. 2. Returns correct MIME type for mbox files (RFC 4155). 3. Returns XML instead of HTML type in some cases. --- sys/src/cmd/file.c | 45 ++++++++++++++++++++++++++++++++++++++++----- 1 file changed, 40 insertions(+), 5 deletions(-) diff --git a/sys/src/cmd/file.c b/sys/src/cmd/file.c index ba34eb009..99a693e12 100644 --- a/sys/src/cmd/file.c +++ b/sys/src/cmd/file.c @@ -169,6 +169,7 @@ int istar(void); int isface(void); int isexec(void); int isudiff(void); +int isexecscript(void); int p9bitnum(char*, int*); int p9subfont(uchar*); void print_utf(void); @@ -182,6 +183,7 @@ int (*call[])(void) = istring, /* recognizable by first string */ iself, /* ELF (foreign) executable */ isexec, /* native executables */ + isexecscript, /* executable scripts */ iff, /* interchange file format (strings) */ longoff, /* recognizable by 4 bytes at some offset */ isoffstr, /* recognizable by string at some offset */ @@ -722,6 +724,41 @@ isexec(void) return 0; } +/* executable scripts */ +int +isexecscript(void) +{ + char tmp[128+1], *p; + + if (memcmp("#!", buf, 2) != 0) + return 0; + memmove(tmp, buf+2, sizeof(tmp) - 1); + tmp[sizeof(tmp) - 1] = 0; + if ((p = strchr(tmp, '\n')) != nil) + *p = 0; + if ((p = strpbrk(tmp, " \t")) != nil) + *p = 0; + if ((p = strrchr(tmp, '/')) != nil) + p++; + else + p = tmp; + + if (strcmp("rc", p) == 0) + print("%s\n", mime ? PLAIN : "rc executable file"); + else if (strcmp("sh", p) == 0) + print("%s\n", mime ? "application/x-sh" : "sh executable file"); + else if (strcmp("bash", p) == 0) + print("%s\n", mime ? "application/x-sh" : "bash executable file"); + else if (strcmp("awk", p) == 0) + print("%s\n", mime ? PLAIN : "awk executable file"); + else if (strcmp("sed", p) == 0) + print("%s\n", mime ? PLAIN : "sed executable file"); + else if (strcmp("perl", p) == 0) + print("%s\n", mime ? PLAIN : "perl executable file"); + else + print("%s\n", mime ? PLAIN : "unknown executable file"); + return 1; +} /* from tar.c */ enum { NAMSIZ = 100, TBLOCK = 512 }; @@ -805,8 +842,6 @@ struct FILE_STRING "!\n__.SYMDEF", "archive random library", 16, OCTET, "!\n", "archive", 8, OCTET, "070707", "cpio archive - ascii header", 6, OCTET, - "#!/bin/rc", "rc executable file", 9, PLAIN, - "#!/bin/sh", "sh executable file", 9, PLAIN, "%!", "postscript", 2, "application/postscript", "\004%!", "postscript", 3, "application/postscript", "x T post", "troff output for post", 8, "application/troff", @@ -820,10 +855,10 @@ struct FILE_STRING "%PDF", "PDF", 4, "application/pdf", "", "HTML file", 6, "text/html", "", "HTML file", 6, "text/html", - "