25856
|
1 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
|
|
2 /* IS_EXEC.C
|
|
3 *
|
|
4 * Given a filename or a file handle, and the extension of the file,
|
|
5 * determine if the file is executable.
|
|
6 * First, the file extension is checked in case it uniquely identifies
|
|
7 * the file as either an executable or not. Failing this, the first
|
|
8 * two bytes of the file are tested for known signatures of executable
|
|
9 * files.
|
|
10 *
|
|
11 */
|
|
12
|
|
13 #include <libc/stubs.h>
|
|
14 #include <stdio.h>
|
|
15 #include <string.h>
|
|
16 #include <ctype.h>
|
|
17 #include <errno.h>
|
|
18 #include <dpmi.h>
|
|
19 #include <go32.h>
|
|
20 #include <io.h>
|
|
21 #include <libc/farptrgs.h>
|
|
22 #include <libc/dosio.h>
|
|
23
|
|
24 extern unsigned short _djstat_flags;
|
|
25 unsigned short _get_magic(const char *, int);
|
|
26 int _is_executable(const char *, int, const char *);
|
|
27
|
|
28 /*
|
|
29 * Read a MAGIC NUMBER from a given file. These are the first
|
|
30 * two bytes of the file, if we look at them as an unsigned short. */
|
|
31
|
|
32 #define _STAT_EXEC_EXT 2 /* get execute bits from file extension? */
|
|
33 #define _STAT_EXEC_MAGIC 4 /* get execute bits from magic signature? */
|
|
34
|
|
35 unsigned short
|
|
36 _get_magic(const char *s, int fh)
|
|
37 {
|
|
38 __dpmi_regs regs;
|
|
39 unsigned short retval;
|
|
40 unsigned short fpos_high = 0, fpos_low = 0;
|
|
41 int read_fail = 0;
|
|
42
|
|
43 /* If given a pathname, open the file. */
|
|
44 if (s)
|
|
45 {
|
|
46 int handle;
|
|
47 if((handle = _open(s,0)) == -1)
|
|
48 return 0;
|
|
49 regs.x.bx = handle;
|
|
50 }
|
|
51 /* Else file already open. Remember its current file position
|
|
52 and move to beginning of file. */
|
|
53 else
|
|
54 {
|
|
55 regs.x.ax = 0x4201; /* set pointer from current position */
|
|
56 regs.x.bx = fh;
|
|
57 regs.x.cx = regs.x.dx = 0; /* move 0 bytes (i.e., stay put) */
|
|
58 __dpmi_int(0x21, ®s);
|
|
59 if (regs.x.flags & 1)
|
|
60 {
|
|
61 errno = __doserr_to_errno(regs.x.ax);
|
|
62 return 0;
|
|
63 }
|
|
64 fpos_high = regs.x.dx; /* got current position */
|
|
65 fpos_low = regs.x.ax;
|
|
66
|
|
67 regs.x.ax = 0x4200; /* set pointer from the beginning of file */
|
|
68 regs.x.cx = regs.x.dx = 0; /* move to beginning of file */
|
|
69 __dpmi_int(0x21, ®s);
|
|
70 if (regs.x.flags & 1)
|
|
71 {
|
|
72 errno = __doserr_to_errno(regs.x.ax);
|
|
73 return 0;
|
|
74 }
|
|
75 }
|
|
76 regs.x.ds = __tb_segment;
|
|
77 regs.x.dx = __tb_offset;
|
|
78
|
|
79 /* Read 2 bytes from the file. */
|
|
80 regs.x.ax = 0x3f00;
|
|
81 regs.x.cx = 2;
|
|
82 __dpmi_int(0x21, ®s);
|
|
83
|
|
84 /* We can either (1) succeed, (2) read less than 2 bytes,
|
|
85 or (3) fail to read at all. */
|
|
86 if (regs.x.ax != 2)
|
|
87 read_fail = (regs.x.flags & 1) ? regs.x.ax : -1;
|
|
88
|
|
89 /* If called with filename, close the file. */
|
|
90 if (s)
|
|
91 {
|
|
92 regs.x.ax = 0x3e00;
|
|
93 __dpmi_int(0x21, ®s);
|
|
94 if (regs.x.flags & 1)
|
|
95 errno = __doserr_to_errno(regs.x.ax);
|
|
96 }
|
|
97 /* Else leave file pointer where we found it. */
|
|
98 else
|
|
99 {
|
|
100 regs.x.ax = 0x4200; /* set pointer from the beginning of file */
|
|
101 regs.x.bx = fh;
|
|
102 regs.x.cx = fpos_high;
|
|
103 regs.x.dx = fpos_low;
|
|
104 __dpmi_int(0x21, ®s);
|
|
105 if (regs.x.flags & 1)
|
|
106 {
|
|
107 errno = __doserr_to_errno(regs.x.ax);
|
|
108 return 0;
|
|
109 }
|
|
110 }
|
|
111
|
|
112 if (read_fail == 0)
|
|
113 retval = _farpeekw(_dos_ds, __tb);
|
|
114 else
|
|
115 {
|
|
116 /* The file couldn't be read: assume non-executable. If the file
|
|
117 *is* executable, but was passed as a file-handle, and the user
|
|
118 opened it in write-only mode, they lose... */
|
|
119 retval = 0;
|
|
120 if (read_fail != -1)
|
|
121 errno = __doserr_to_errno(read_fail);
|
|
122 }
|
|
123
|
|
124 return retval;
|
|
125 }
|
|
126
|
|
127 /* A list of extensions which designate executable files. These
|
|
128 are NOT tested for the magic number. */
|
|
129 static char executables[] = "|EXE|COM|BAT|BTM|DLL|VXD|";
|
|
130
|
|
131 /* A list of extensions which belong to files known to NEVER be
|
|
132 executables. These exist to minimize read()'ing files while
|
|
133 detecting executables by magic number. You are welcome to
|
|
134 add to this list, but remember: only extensions which could
|
|
135 NEVER be present in executables should go here. */
|
|
136 static char non_executables[] = "\
|
|
137 |A|A01|A02|A03|A04|A05|ADL|ARC|ARJ|ASC|ASM|AUX|AWK\
|
|
138 |BAS|BIB|BGI|BMP\
|
|
139 |C|CC|CFG|CGZ|CH3|CHR|CI|CLP|CMF|CPI|CPP|CXX\
|
|
140 |DAT|DBF|DIZ|DOC|DVI\
|
|
141 |E|EL|ELC\
|
|
142 |F77|FN3\
|
|
143 |GIF|GZ\
|
|
144 |H|HLP|HPP|HXX\
|
|
145 |ICO|IN|INC|INF|INI\
|
|
146 |JPG\
|
|
147 |L|LEX|LF|LIB|LOG|LST|LZH\
|
|
148 |M|MAK|MAP|MF|MID|MPG\
|
|
149 |O|OBJ\
|
|
150 |PAK|PAS|PBM|PCD|PCX|PDS|PIC|PIF|PN3|PRJ|PS\
|
|
151 |RAS|RGB|RLE\
|
|
152 |S|SND|SY3\
|
|
153 |TAR|TAZ|TEX|TGA|TGZ|TIF|TXH|TXI|TXT\
|
|
154 |VOC\
|
|
155 |WAV|WK1|WK3|WKB|WQ1|WQ3|WQ4|WQ5|WQ6|WQ!\
|
|
156 |XBM\
|
|
157 |Y\
|
|
158 |ZIP|ZOO|";
|
|
159
|
|
160 int
|
|
161 _is_executable(const char *filename, int fhandle, const char *extension)
|
|
162 {
|
|
163 if (!extension && filename)
|
|
164 {
|
|
165 const char *cp, *ep=0;
|
|
166 for (cp=filename; *cp; cp++)
|
|
167 {
|
|
168 if (*cp == '.')
|
|
169 ep = cp;
|
|
170 if (*cp == '/' || *cp == '\\' || *cp == ':')
|
|
171 ep = 0;
|
|
172 }
|
|
173 extension = ep;
|
|
174 }
|
|
175 if ((_djstat_flags & _STAT_EXEC_EXT) == 0
|
|
176 && extension
|
|
177 && *extension
|
|
178 && strlen(extension) <= ((extension[0]=='.') ? 4 : 3))
|
|
179 {
|
|
180 /* Search the list of extensions in executables[]. */
|
|
181 char tmp_buf[6], *tp = tmp_buf;
|
|
182
|
|
183 *tp++ = '|';
|
|
184 if (*extension == '.')
|
|
185 extension++;
|
|
186 while (*extension)
|
|
187 *tp++ = toupper (*extension++);
|
|
188 *tp++ = '|';
|
|
189 *tp = '\0';
|
|
190 if (strstr(non_executables, tmp_buf))
|
|
191 return 0;
|
|
192 else if (strstr(executables, tmp_buf))
|
|
193 return 1;
|
|
194 }
|
|
195
|
|
196 /* No extension, or extension doesn't define execute
|
|
197 bits unambiguously. We are in for some dirty work.
|
|
198 Read the first two bytes of the file and see if they
|
|
199 are any of the known magic numbers which designate
|
|
200 executable files.
|
|
201 Unix-like shells, which have executable shell scripts
|
|
202 without extensions and DON'T have "#!" as their FIRST
|
|
203 TWO CHARACTERS, lose here. Sorry, folks. */
|
|
204 if ( (_djstat_flags & _STAT_EXEC_MAGIC) == 0 )
|
|
205 {
|
|
206 switch (_get_magic(filename, fhandle))
|
|
207 {
|
|
208 case 0x5a4d: /* "MZ" */
|
|
209 case 0x010b:
|
|
210 case 0x014c:
|
|
211 case 0x2123: /* "#!" */
|
|
212 return 1;
|
|
213 }
|
|
214 }
|
|
215
|
|
216 return 0;
|
|
217 }
|
52401
|
218
|
|
219 /* arch-tag: b0965811-8c3e-4bc4-8d81-4447a3594785
|
|
220 (do not change this comment) */
|