]> git.lizzy.rs Git - plan9front.git/blob - sys/src/cmd/mothra/snoop.c
exec(2): fix prototypes
[plan9front.git] / sys / src / cmd / mothra / snoop.c
1 #include <u.h>
2 #include <libc.h>
3 #include <draw.h>
4 #include <event.h>
5 #include <panel.h>
6 #include <ctype.h>
7 #include "mothra.h"
8
9 int
10 filetype(int fd, char *typ, int ntyp)
11 {
12         int ifd[2], ofd[2], xfd[2], n;
13         char *argv[3], buf[4096];
14
15         typ[0] = 0;
16         if((n = readn(fd, buf, sizeof(buf))) < 0)
17                 return -1;
18         if(n == 0)
19                 return 0;
20         if(pipe(ifd) < 0)
21                 return -1;
22         if(pipe(ofd) < 0){
23 Err1:
24                 close(ifd[0]);
25                 close(ifd[1]);
26                 return -1;
27         }
28         switch(rfork(RFFDG|RFPROC|RFNOWAIT)){
29         case -1:
30                 close(ofd[0]);
31                 close(ofd[1]);
32                 goto Err1;      
33         case 0:
34                 dup(ifd[1], 0);
35                 dup(ofd[1], 1);
36
37                 close(ifd[1]);
38                 close(ifd[0]);
39                 close(ofd[1]);
40                 close(ofd[0]);
41                 close(fd);
42
43                 argv[0] = "file";
44                 argv[1] = "-m";
45                 argv[2] = 0;
46                 exec("/bin/file", argv);
47         }
48         close(ifd[1]);
49         close(ofd[1]);
50
51         if(rfork(RFFDG|RFPROC|RFNOWAIT) == 0){
52                 close(fd);
53                 close(ofd[0]);
54                 write(ifd[0], buf, n);
55                 exits(nil);
56         }
57         close(ifd[0]);
58
59         if(pipe(xfd) < 0){
60                 close(ofd[0]);
61                 return -1;
62         }
63         switch(rfork(RFFDG|RFPROC|RFNOWAIT)){
64         case -1:
65                 break;
66         case 0:
67                 close(ofd[0]);
68                 close(xfd[0]);
69                 do {
70                         if(write(xfd[1], buf, n) != n)
71                                 break;
72                 } while((n = read(fd, buf, sizeof(buf))) > 0);
73                 exits(nil);
74         default:
75                 dup(xfd[0], fd);
76         }
77         close(xfd[0]);
78         close(xfd[1]);
79
80         if((n = readn(ofd[0], typ, ntyp-1)) < 0)
81                 n = 0;
82         close(ofd[0]);
83         while(n > 0 && typ[n-1] == '\n')
84                 n--;
85         typ[n] = 0;
86         return 0;
87 }
88
89 int
90 snooptype(int fd)
91 {
92         static struct {
93                 char    *typ;
94                 int     val;
95         } tab[] = {
96         "text/plain",                   PLAIN,
97         "text/html",                    HTML,
98
99         "image/jpeg",                   JPEG,
100         "image/gif",                    GIF,
101         "image/png",                    PNG,
102         "image/bmp",                    BMP,
103         "image/x-icon",                 ICO,
104
105         "application/pdf",              PAGE,
106         "application/postscript",       PAGE,
107         "application/ghostscript",      PAGE,
108         "application/troff",            PAGE,
109
110         "image/",                       PAGE,
111         "text/",                        PLAIN,
112         "message/rfc822",               PLAIN,
113         };
114         char buf[128];
115         int i;
116         if(filetype(fd, buf, sizeof(buf)) < 0)
117                 return -1;
118         for(i=0; i<nelem(tab); i++)
119                 if(strncmp(buf, tab[i].typ, strlen(tab[i].typ)) == 0)
120                         return tab[i].val;
121         return -1;
122 }