]> git.lizzy.rs Git - plan9front.git/blobdiff - sys/src/cmd/file.c
make bind(2) error handling consistent
[plan9front.git] / sys / src / cmd / file.c
index ee6db10b4695de24e63a74a8da85cc27b3f42947..bd9d1afb5b492146277b6c53411a34177dbc2026 100644 (file)
@@ -152,6 +152,7 @@ int ismbox(void);
 int    islimbo(void);
 int    istga(void);
 int    ismp3(void);
+int    ismp4(void);
 int    ismung(void);
 int    isp9bit(void);
 int    isp9font(void);
@@ -167,7 +168,8 @@ int longoff(void);
 int    istar(void);
 int    isface(void);
 int    isexec(void);
-int    p9bitnum(uchar*);
+int    isudiff(void);
+int    p9bitnum(char*, int*);
 int    p9subfont(uchar*);
 void   print_utf(void);
 void   type(char*, int);
@@ -183,11 +185,12 @@ int       (*call[])(void) =
        iff,            /* interchange file format (strings) */
        longoff,        /* recognizable by 4 bytes at some offset */
        isoffstr,       /* recognizable by string at some offset */
+       isudiff,        /* unified diff output */
        isrfc822,       /* email file */
        ismbox,         /* mail box */
        istar,          /* recognizable by tar checksum */
-       ishtml,         /* html keywords */
        iscint,         /* compiler/assembler intermediate */
+       ishtml,         /* html keywords */
        islimbo,        /* limbo source */
        isc,            /* c & alef compiler key words */
        isas,           /* assembler key words */
@@ -198,6 +201,7 @@ int (*call[])(void) =
        isicocur,               /* windows icon or cursor file */
        isface,         /* ascii face file */
        istga,
+       ismp4,
        ismp3,
 
        /* last resorts */
@@ -266,7 +270,7 @@ type(char *file, int nlen)
        }
        fname = file;
        if ((fd = open(file, OREAD)) < 0) {
-               print("cannot open: %r\n");
+               fprint(2, "cannot open: %r\n");
                return;
        }
        filetype(fd);
@@ -345,7 +349,7 @@ filetype(int fd)
        free(mbuf);
        mbuf = dirfstat(fd);
        if(mbuf == nil){
-               print("cannot stat: %r\n");
+               fprint(2, "cannot stat: %r\n");
                return;
        }
        if(mbuf->mode & DMDIR) {
@@ -362,7 +366,7 @@ filetype(int fd)
        /* may be reading a pipe on standard input */
        nbuf = readn(fd, buf, sizeof(buf)-1);
        if(nbuf < 0) {
-               print("cannot read: %r\n");
+               fprint(2, "cannot read: %r\n");
                return;
        }
        if(nbuf == 0) {
@@ -610,6 +614,7 @@ Filemagic long0tab[] = {
        070707,         0xFFFF,         "cpio archive", "application/x-cpio",
        0x2F7,          0xFFFF,         "tex dvi", "application/dvi",
        0xfaff,         0xfeff,         "mp3 audio",    "audio/mpeg",
+       0xf0ff,         0xf6ff,         "aac audio",    "audio/mpeg",
        /* 0xfeedface: this could alternately be a Next Plan 9 boot image */
        0xcefaedfe,     0xFFFFFFFF,     "32-bit power Mach-O executable", OCTET,
        /* 0xfeedfacf */
@@ -626,6 +631,12 @@ Filemagic long0tab[] = {
         */
        0xad4e5cd1,     0xFFFFFFFF,     "venti arena", OCTET,
        0x2bb19a52,     0xFFFFFFFF,     "paq archive", OCTET,
+       0x1a53454e,     0xFFFFFFFF,     "NES ROM", OCTET,
+       /* tcpdump pcap file */
+       0xa1b2c3d4,     0xFFFFFFFF,     "pcap file",    "application/vnd.tcpdump.pcap",
+       0xd4c3b2a1,     0xFFFFFFFF,     "pcap file",    "application/vnd.tcpdump.pcap",
+       0xa1b23c4d,     0xFFFFFFFF,     "pcap file",    "application/vnd.tcpdump.pcap",
+       0x4d3cb2a1,     0xFFFFFFFF,     "pcap file",    "application/vnd.tcpdump.pcap",
 };
 
 int
@@ -666,6 +677,8 @@ Fileoffmag longofftab[] = {
        256*1024, 0xc75e5cd1, 0xFFFFFFFF, "venti index section", OCTET,
        128*1024, 0x89ae7637, 0xFFFFFFFF, "fossil write buffer", OCTET,
        4,        0x31647542, 0xFFFFFFFF, "OS X finder properties", OCTET,
+       0x100,    0x41474553, 0xFFFFFFFF, "SEGA ROM", OCTET,
+       0x1fc,    0xAA550000, 0xFFFF0000, "bootable disk image", OCTET,
 };
 
 int
@@ -789,11 +802,11 @@ struct    FILE_STRING
        "\x1f\x9d",             "compressed",                   2,      "application/x-compress",
        "\x1f\x8b",             "gzip compressed",              2,      "application/x-gzip",
        "BZh",                  "bzip2 compressed",             3,      "application/x-bzip2",
-       "!<arch>\n__.SYMDEF",   "archive random library",       16,     "application/octet-stream",
-       "!<arch>\n",            "archive",                      8,      "application/octet-stream",
-       "070707",               "cpio archive - ascii header",  6,      "application/octet-stream",
-       "#!/bin/rc",            "rc executable file",           9,      "text/plain",
-       "#!/bin/sh",            "sh executable file",           9,      "text/plain",
+       "!<arch>\n__.SYMDEF",   "archive random library",       16,     OCTET,
+       "!<arch>\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,20 +833,20 @@ struct    FILE_STRING
        "BM",                   "bmp",                          2,      "image/bmp", 
        "\xD0\xCF\x11\xE0\xA1\xB1\x1A\xE1",     "microsoft office document",    8,      "application/doc",
        "<MakerFile ",          "FrameMaker file",              11,     "application/framemaker",
-       "\033E\033",    "HP PCL printer data",          3,      OCTET,
-       "\033&",        "HP PCL printer data",          2,      OCTET,
-       "\033%-12345X", "HPJCL file",           9,      "application/hpjcl",
+       "\033E\033",            "HP PCL printer data",          3,      OCTET,
+       "\033&",                "HP PCL printer data",          2,      OCTET,
+       "\033%-12345X",         "HPJCL file",           9,      "application/hpjcl",
        "\033Lua",              "Lua bytecode",         4,      OCTET,
        "ID3",                  "mp3 audio with id3",   3,      "audio/mpeg",
        "OggS",                 "ogg audio",            4,      "audio/ogg",
        ".snd",                 "sun audio",            4,      "audio/basic",
        "\211PNG",              "PNG image",            4,      "image/png",
-       "P1\n",                 "ppm",                          3,      "image/ppm",
-       "P2\n",                 "ppm",                          3,      "image/ppm",
-       "P3\n",                 "ppm",                          3,      "image/ppm",
-       "P4\n",                 "ppm",                          3,      "image/ppm",
-       "P5\n",                 "ppm",                          3,      "image/ppm",
-       "P6\n",                 "ppm",                          3,      "image/ppm",
+       "P1\n",                 "ppm",                  3,      "image/ppm",
+       "P2\n",                 "ppm",                  3,      "image/ppm",
+       "P3\n",                 "ppm",                  3,      "image/ppm",
+       "P4\n",                 "ppm",                  3,      "image/ppm",
+       "P5\n",                 "ppm",                  3,      "image/ppm",
+       "P6\n",                 "ppm",                  3,      "image/ppm",
        "/* XPM */\n",  "xbm",                          10,     "image/xbm",
        ".HTML ",               "troff -ms input",      6,      "text/troff",
        ".LP",                  "troff -ms input",      3,      "text/troff",
@@ -847,20 +860,32 @@ struct    FILE_STRING
        ".if",                  "troff input",          3,      "text/troff",
        ".nr",                  "troff input",          3,      "text/troff",
        ".tr",                  "troff input",          3,      "text/troff",
-       "vac:",                 "venti score",          4,      "text/plain",
+       "vac:",                 "venti score",          4,      PLAIN,
        "-----BEGIN CERTIFICATE-----\n",
-                               "pem certificate",      -1,     "text/plain",
+                               "pem certificate",      -1,     PLAIN,
        "-----BEGIN TRUSTED CERTIFICATE-----\n",
-                               "pem trusted certificate", -1,  "text/plain",
+                               "pem trusted certificate", -1,  PLAIN,
        "-----BEGIN X509 CERTIFICATE-----\n",
-                               "pem x.509 certificate", -1,    "text/plain",
-       "subject=/C=",          "pem certificate with header", -1, "text/plain",
+                               "pem x.509 certificate", -1,    PLAIN,
+       "subject=/C=",          "pem certificate with header", -1, PLAIN,
        "process snapshot ",    "process snapshot",     -1,     "application/snapfs",
        "d8:announce",          "torrent file",         11,     "application/x-bittorrent",
        "[playlist]",           "playlist",             10,     "application/x-scpls",
        "#EXTM3U",              "playlist",             7,      "audio/x-mpegurl",
        "BEGIN:VCARD\r\n",      "vCard",                13,     "text/directory;profile=vcard",
        "BEGIN:VCARD\n",        "vCard",                12,     "text/directory;profile=vcard",
+       "AT&T",                 "DjVu document",        4,      "image/vnd.djvu",
+       "Extended module: ",    "XM audio",             17,     "audio/xm",
+       "MThd",                 "midi audio",           4,      "audio/midi",
+       "MUS\x1a",              "mus audio",            4,      "audio/mus",
+       "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
+       "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
+       "\x00\x00\x00\xbb\x11\x22\x00\x44\xff\xff\xff\xff\xff\xff\xff\xff"
+       "\xaa\x99\x55\x66", "Xilinx bitstream (not byteswappped)", 52, OCTET,
+       "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
+       "\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff\xff"
+       "\xbb\x00\x00\x00\x44\x00\x22\x11\xff\xff\xff\xff\xff\xff\xff\xff"
+       "\x66\x55\x99\xaa", "Xilinx bitstream (byteswappped)", 52, OCTET,
        0,0,0,0
 };
 
@@ -898,6 +923,7 @@ struct offstr
        struct FILE_STRING;
 } offstrs[] = {
        32*1024, "\001CD001\001",       "ISO9660 CD image",     7,      "application/x-iso9660-image",
+       32*4, "DICM",   "DICOM medical imaging data",   4,      "application/dicom",
        0, 0, 0, 0, 0
 };
 
@@ -937,7 +963,7 @@ iff(void)
                else if (strncmp((char*)buf+8, "AVI ", 4) == 0)
                        print("%s\n", mime? "video/avi": "avi video");
                else
-                       print("%s\n", mime? "application/octet-stream": "riff file");
+                       print("%s\n", mime? OCTET : "riff file");
                return 1;
        }
        return 0;
@@ -960,6 +986,25 @@ char*      html_string[] = {
        0,
 };
 
+int
+isudiff(void)
+{
+       char *p;
+
+       p = (char*)buf;
+       if((p = strstr(p, "diff")) != nil)
+       if((p = strchr(p, '\n')) != nil)
+       if(strncmp(++p, "--- ", 4) == 0)
+       if((p = strchr(p, '\n')) != nil)
+       if(strncmp(++p, "+++ ", 4) == 0)
+       if((p = strchr(p, '\n')) != nil)
+       if(strncmp(++p, "@@ ", 3) == 0){
+               print("%s\n", mime ? "text/plain" : "unified diff output");
+               return 1;
+       }
+       return 0;
+}
+
 int
 ishtml(void)
 {
@@ -1061,7 +1106,7 @@ ismbox(void)
                return 0;
        *q = 0;
        if(strncmp(p, "From ", 5) == 0 && strstr(p, " remote from ") == nil){
-               print("%s\n", mime ? "text/plain" : "mail box");
+               print("%s\n", mime ? PLAIN : "mail box");
                return 1;
        }
        *q = '\n';
@@ -1199,6 +1244,24 @@ ismp3(void)
        return 0;
 }
 
+int
+ismp4(void)
+{
+       if(nbuf <= 12)
+               return 0;
+       if(memcmp(&buf[4], "ftyp", 4) != 0)
+               return 0;
+       if(memcmp(&buf[8], "isom", 4) == 0){
+               print("%s\n", mime ? "video/mp4" : "mp4 video");
+               return 1;
+       }
+       if(memcmp(&buf[8], "M4A ", 4) == 0){
+               print("%s\n", mime ? "audio/m4a" : "m4a audio");
+               return 1;
+       }
+       return 0;
+}
+
 /*
  * low entropy means encrypted
  */
@@ -1298,28 +1361,18 @@ isenglish(void)
  */
 #define        P9BITLEN        12
 int
-p9bitnum(uchar *bp)
+p9bitnum(char *s, int *v)
 {
-       int n, c, len;
+       char *es;
 
-       len = P9BITLEN;
-       while(*bp == ' ') {
-               bp++;
-               len--;
-               if(len <= 0)
-                       return -1;
-       }
-       n = 0;
-       while(len > 1) {
-               c = *bp++;
-               if(!isdigit(c))
-                       return -1;
-               n = n*10 + c-'0';
-               len--;
-       }
-       if(*bp != ' ')
+       if(s[P9BITLEN-1] != ' ')
+               return -1;
+       s[P9BITLEN-1] = '\0';
+       *v = strtol(s, &es, 10);
+       s[P9BITLEN-1] = ' ';
+       if(es != &s[P9BITLEN-1])
                return -1;
-       return n;
+       return 0;
 }
 
 int
@@ -1335,13 +1388,18 @@ depthof(char *s, int *newp)
        if(s == es)
                return -1;
        if('0'<=*s && *s<='9')
-               return 1<<strtol(s, 0, 0);
+               return 1<<strtol(s, nil, 0);
 
        *newp = 1;
        d = 0;
        while(s<es && *s!=' '){
-               s++;                    /* skip letter */
-               d += strtoul(s, &s, 10);
+               if(strchr("rgbkamx", *s) == nil)
+                       return -1;
+               s++;
+               if('0'<=*s && *s<='9')
+                       d += strtoul(s, &s, 10);
+               else
+                       return -1;
        }
 
        if(d % 8 == 0 || 8 % d == 0)
@@ -1354,43 +1412,41 @@ int
 isp9bit(void)
 {
        int dep, lox, loy, hix, hiy, px, new, cmpr;
-       ulong t;
        long len;
        char *newlabel;
        uchar *cp;
 
        cp = buf;
        cmpr = 0;
-       newlabel = "old ";
-
        if(memcmp(cp, "compressed\n", 11) == 0) {
                cmpr = 1;
                cp = buf + 11;
        }
 
-       dep = depthof((char*)cp + 0*P9BITLEN, &new);
-       if(new)
-               newlabel = "";
-       lox = p9bitnum(cp + 1*P9BITLEN);
-       loy = p9bitnum(cp + 2*P9BITLEN);
-       hix = p9bitnum(cp + 3*P9BITLEN);
-       hiy = p9bitnum(cp + 4*P9BITLEN);
-       if(dep < 0 || lox < 0 || loy < 0 || hix < 0 || hiy < 0)
+       if((dep = depthof((char*)cp + 0*P9BITLEN, &new)) < 0)
+               return 0;
+       newlabel = new ? "" : "old ";
+       if(p9bitnum((char*)cp + 1*P9BITLEN, &lox) < 0)
+               return 0;
+       if(p9bitnum((char*)cp + 2*P9BITLEN, &loy) < 0)
+               return 0;
+       if(p9bitnum((char*)cp + 3*P9BITLEN, &hix) < 0)
+               return 0;
+       if(p9bitnum((char*)cp + 4*P9BITLEN, &hiy) < 0)
+               return 0;
+
+       hix -= lox;
+       hiy -= loy;
+       if(hix <= 0 || hiy <= 0)
                return 0;
 
        if(dep < 8){
                px = 8/dep;             /* pixels per byte */
                /* set l to number of bytes of data per scan line */
-               if(lox >= 0)
-                       len = (hix+px-1)/px - lox/px;
-               else{                   /* make positive before divide */
-                       t = (-lox)+px-1;
-                       t = (t/px)*px;
-                       len = (t+hix+px-1)/px;
-               }
+               len = (hix+px-1)/px;
        }else
-               len = (hix-lox)*dep/8;
-       len *= hiy - loy;               /* col length */
+               len = hix*dep/8;
+       len *= hiy;                     /* col length */
        len += 5 * P9BITLEN;            /* size of initial ascii */
 
        /*
@@ -1400,8 +1456,8 @@ isp9bit(void)
         * for subfont, the subfont header should follow immediately.
         */
        if (cmpr) {
-               print(mime ? "image/p9bit\n" : "Compressed %splan 9 image or subfont, depth %d\n",
-                       newlabel, dep);
+               print(mime ? "image/p9bit\n" : "Compressed %splan 9 image or subfont, depth %d, size %dx%d\n",
+                       newlabel, dep, hix, hiy);
                return 1;
        }
        /*
@@ -1410,11 +1466,13 @@ isp9bit(void)
         */
        if (len != 0 && (mbuf->length == 0 || mbuf->length == len ||
            mbuf->length > len && mbuf->length < len+P9BITLEN)) {
-               print(mime ? "image/p9bit\n" : "%splan 9 image, depth %d\n", newlabel, dep);
+               print(mime ? "image/p9bit\n" : "%splan 9 image, depth %d, size %dx%d\n",
+                       newlabel, dep, hix, hiy);
                return 1;
        }
        if (p9subfont(buf+len)) {
-               print(mime ? "image/p9bit\n" : "%ssubfont file, depth %d\n", newlabel, dep);
+               print(mime ? "image/p9bit\n" : "%ssubfont file, depth %d, size %dx%d\n",
+                       newlabel, dep, hix, hiy);
                return 1;
        }
        return 0;
@@ -1429,16 +1487,15 @@ p9subfont(uchar *p)
        if (p+3*P9BITLEN > buf+sizeof(buf))
                return 1;
 
-       n = p9bitnum(p + 0*P9BITLEN);   /* char count */
-       if (n < 0)
+       if (p9bitnum((char*)p + 0*P9BITLEN, &n) < 0)    /* char count */
                return 0;
-       h = p9bitnum(p + 1*P9BITLEN);   /* height */
-       if (h < 0)
+       if (p9bitnum((char*)p + 1*P9BITLEN, &h) < 0)    /* height */
                return 0;
-       a = p9bitnum(p + 2*P9BITLEN);   /* ascent */
-       if (a < 0)
+       if (p9bitnum((char*)p + 2*P9BITLEN, &a) < 0)    /* ascent */
                return 0;
-       return 1;
+       if(n > 0 && h > 0 && a >= 0)
+               return 1;
+       return 0;
 }
 
 #define        WHITESPACE(c)           ((c) == ' ' || (c) == '\t' || (c) == '\n')
@@ -1485,7 +1542,7 @@ isp9font(void)
                }
        }
        if (i) {
-               print(mime ? "text/plain\n" : "font file\n");
+               print("%s\n", mime ? PLAIN : "font file");
                return 1;
        }
        return 0;
@@ -1647,4 +1704,3 @@ isface(void)
                print("face image depth %d\n", ldepth);
        return 1;
 }
-