3 * Copyright (C) 1995 DJ Delorie
4 * Copyright (C) 1994 Eli Zaretskii <eliz@is.elta.co.il>
6 * (See the README file in this directory for the copyright and license
7 * history of this file.)
9 * This file is free software: you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation, either version 3 of the License, or
12 * (at your option) any later version.
14 * This file is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this file. If not, see <http://www.gnu.org/licenses/>.
24 * Given a filename or a file handle, and the extension of the file,
25 * determine if the file is executable.
26 * First, the file extension is checked in case it uniquely identifies
27 * the file as either an executable or not. Failing this, the first
28 * two bytes of the file are tested for known signatures of executable
33 #include <libc/stubs.h>
41 #include <libc/farptrgs.h>
42 #include <libc/dosio.h>
44 extern unsigned short _djstat_flags
;
45 unsigned short _get_magic(const char *, int);
46 int _is_executable(const char *, int, const char *);
49 * Read a MAGIC NUMBER from a given file. These are the first
50 * two bytes of the file, if we look at them as an unsigned short. */
52 #define _STAT_EXEC_EXT 2 /* get execute bits from file extension? */
53 #define _STAT_EXEC_MAGIC 4 /* get execute bits from magic signature? */
56 _get_magic(const char *s
, int fh
)
59 unsigned short retval
;
60 unsigned short fpos_high
= 0, fpos_low
= 0;
63 /* If given a pathname, open the file. */
67 if((handle
= _open(s
,0)) == -1)
71 /* Else file already open. Remember its current file position
72 and move to beginning of file. */
75 regs
.x
.ax
= 0x4201; /* set pointer from current position */
77 regs
.x
.cx
= regs
.x
.dx
= 0; /* move 0 bytes (i.e., stay put) */
78 __dpmi_int(0x21, ®s
);
81 errno
= __doserr_to_errno(regs
.x
.ax
);
84 fpos_high
= regs
.x
.dx
; /* got current position */
87 regs
.x
.ax
= 0x4200; /* set pointer from the beginning of file */
88 regs
.x
.cx
= regs
.x
.dx
= 0; /* move to beginning of file */
89 __dpmi_int(0x21, ®s
);
92 errno
= __doserr_to_errno(regs
.x
.ax
);
96 regs
.x
.ds
= __tb_segment
;
97 regs
.x
.dx
= __tb_offset
;
99 /* Read 2 bytes from the file. */
102 __dpmi_int(0x21, ®s
);
104 /* We can either (1) succeed, (2) read less than 2 bytes,
105 or (3) fail to read at all. */
107 read_fail
= (regs
.x
.flags
& 1) ? regs
.x
.ax
: -1;
109 /* If called with filename, close the file. */
113 __dpmi_int(0x21, ®s
);
114 if (regs
.x
.flags
& 1)
115 errno
= __doserr_to_errno(regs
.x
.ax
);
117 /* Else leave file pointer where we found it. */
120 regs
.x
.ax
= 0x4200; /* set pointer from the beginning of file */
122 regs
.x
.cx
= fpos_high
;
123 regs
.x
.dx
= fpos_low
;
124 __dpmi_int(0x21, ®s
);
125 if (regs
.x
.flags
& 1)
127 errno
= __doserr_to_errno(regs
.x
.ax
);
133 retval
= _farpeekw(_dos_ds
, __tb
);
136 /* The file couldn't be read: assume non-executable. If the file
137 *is* executable, but was passed as a file-handle, and the user
138 opened it in write-only mode, they lose... */
141 errno
= __doserr_to_errno(read_fail
);
147 /* A list of extensions which designate executable files. These
148 are NOT tested for the magic number. */
149 static char executables
[] = "|EXE|COM|BAT|BTM|DLL|VXD|";
151 /* A list of extensions which belong to files known to NEVER be
152 executables. These exist to minimize read()'ing files while
153 detecting executables by magic number. You are welcome to
154 add to this list, but remember: only extensions which could
155 NEVER be present in executables should go here. */
156 static char non_executables
[] = "\
157 |A|A01|A02|A03|A04|A05|ADL|ARC|ARJ|ASC|ASM|AUX|AWK\
159 |C|CC|CFG|CGZ|CH3|CHR|CI|CLP|CMF|CPI|CPP|CXX\
160 |DAT|DBF|DIZ|DOC|DVI\
167 |L|LEX|LF|LIB|LOG|LST|LZH\
168 |M|MAK|MAP|MF|MID|MPG\
170 |PAK|PAS|PBM|PCD|PCX|PDS|PIC|PIF|PN3|PRJ|PS\
173 |TAR|TAZ|TEX|TGA|TGZ|TIF|TXH|TXI|TXT\
175 |WAV|WK1|WK3|WKB|WQ1|WQ3|WQ4|WQ5|WQ6|WQ!\
181 _is_executable(const char *filename
, int fhandle
, const char *extension
)
183 if (!extension
&& filename
)
185 const char *cp
, *ep
=0;
186 for (cp
=filename
; *cp
; cp
++)
190 if (*cp
== '/' || *cp
== '\\' || *cp
== ':')
195 if ((_djstat_flags
& _STAT_EXEC_EXT
) == 0
198 && strlen(extension
) <= ((extension
[0]=='.') ? 4 : 3))
200 /* Search the list of extensions in executables[]. */
201 char tmp_buf
[6], *tp
= tmp_buf
;
204 if (*extension
== '.')
207 *tp
++ = toupper (*extension
++);
210 if (strstr(non_executables
, tmp_buf
))
212 else if (strstr(executables
, tmp_buf
))
216 /* No extension, or extension doesn't define execute
217 bits unambiguously. We are in for some dirty work.
218 Read the first two bytes of the file and see if they
219 are any of the known magic numbers which designate
221 Unix-like shells, which have executable shell scripts
222 without extensions and DON'T have "#!" as their FIRST
223 TWO CHARACTERS, lose here. Sorry, folks. */
224 if ( (_djstat_flags
& _STAT_EXEC_MAGIC
) == 0 )
226 switch (_get_magic(filename
, fhandle
))
228 case 0x5a4d: /* "MZ" */
231 case 0x2123: /* "#!" */