1 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
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
11 * Copyright (c) 1994 Eli Zaretskii <eliz@is.elta.co.il>
13 * This software may be used freely so long as this copyright notice is
14 * left intact. There is no warranty on this software.
18 #include <libc/stubs.h>
26 #include <libc/farptrgs.h>
27 #include <libc/dosio.h>
29 extern unsigned short _djstat_flags
;
30 unsigned short _get_magic(const char *, int);
31 int _is_executable(const char *, int, const char *);
34 * Read a MAGIC NUMBER from a given file. These are the first
35 * two bytes of the file, if we look at them as an unsigned short. */
37 #define _STAT_EXEC_EXT 2 /* get execute bits from file extension? */
38 #define _STAT_EXEC_MAGIC 4 /* get execute bits from magic signature? */
41 _get_magic(const char *s
, int fh
)
44 unsigned short retval
;
45 unsigned short fpos_high
= 0, fpos_low
= 0;
48 /* If given a pathname, open the file. */
52 if((handle
= _open(s
,0)) == -1)
56 /* Else file already open. Remember its current file position
57 and move to beginning of file. */
60 regs
.x
.ax
= 0x4201; /* set pointer from current position */
62 regs
.x
.cx
= regs
.x
.dx
= 0; /* move 0 bytes (i.e., stay put) */
63 __dpmi_int(0x21, ®s
);
66 errno
= __doserr_to_errno(regs
.x
.ax
);
69 fpos_high
= regs
.x
.dx
; /* got current position */
72 regs
.x
.ax
= 0x4200; /* set pointer from the beginning of file */
73 regs
.x
.cx
= regs
.x
.dx
= 0; /* move to beginning of file */
74 __dpmi_int(0x21, ®s
);
77 errno
= __doserr_to_errno(regs
.x
.ax
);
81 regs
.x
.ds
= __tb_segment
;
82 regs
.x
.dx
= __tb_offset
;
84 /* Read 2 bytes from the file. */
87 __dpmi_int(0x21, ®s
);
89 /* We can either (1) succeed, (2) read less than 2 bytes,
90 or (3) fail to read at all. */
92 read_fail
= (regs
.x
.flags
& 1) ? regs
.x
.ax
: -1;
94 /* If called with filename, close the file. */
98 __dpmi_int(0x21, ®s
);
100 errno
= __doserr_to_errno(regs
.x
.ax
);
102 /* Else leave file pointer where we found it. */
105 regs
.x
.ax
= 0x4200; /* set pointer from the beginning of file */
107 regs
.x
.cx
= fpos_high
;
108 regs
.x
.dx
= fpos_low
;
109 __dpmi_int(0x21, ®s
);
110 if (regs
.x
.flags
& 1)
112 errno
= __doserr_to_errno(regs
.x
.ax
);
118 retval
= _farpeekw(_dos_ds
, __tb
);
121 /* The file couldn't be read: assume non-executable. If the file
122 *is* executable, but was passed as a file-handle, and the user
123 opened it in write-only mode, they lose... */
126 errno
= __doserr_to_errno(read_fail
);
132 /* A list of extensions which designate executable files. These
133 are NOT tested for the magic number. */
134 static char executables
[] = "|EXE|COM|BAT|BTM|DLL|VXD|";
136 /* A list of extensions which belong to files known to NEVER be
137 executables. These exist to minimize read()'ing files while
138 detecting executables by magic number. You are welcome to
139 add to this list, but remember: only extensions which could
140 NEVER be present in executables should go here. */
141 static char non_executables
[] = "\
142 |A|A01|A02|A03|A04|A05|ADL|ARC|ARJ|ASC|ASM|AUX|AWK\
144 |C|CC|CFG|CGZ|CH3|CHR|CI|CLP|CMF|CPI|CPP|CXX\
145 |DAT|DBF|DIZ|DOC|DVI\
152 |L|LEX|LF|LIB|LOG|LST|LZH\
153 |M|MAK|MAP|MF|MID|MPG\
155 |PAK|PAS|PBM|PCD|PCX|PDS|PIC|PIF|PN3|PRJ|PS\
158 |TAR|TAZ|TEX|TGA|TGZ|TIF|TXH|TXI|TXT\
160 |WAV|WK1|WK3|WKB|WQ1|WQ3|WQ4|WQ5|WQ6|WQ!\
166 _is_executable(const char *filename
, int fhandle
, const char *extension
)
168 if (!extension
&& filename
)
170 const char *cp
, *ep
=0;
171 for (cp
=filename
; *cp
; cp
++)
175 if (*cp
== '/' || *cp
== '\\' || *cp
== ':')
180 if ((_djstat_flags
& _STAT_EXEC_EXT
) == 0
183 && strlen(extension
) <= ((extension
[0]=='.') ? 4 : 3))
185 /* Search the list of extensions in executables[]. */
186 char tmp_buf
[6], *tp
= tmp_buf
;
189 if (*extension
== '.')
192 *tp
++ = toupper (*extension
++);
195 if (strstr(non_executables
, tmp_buf
))
197 else if (strstr(executables
, tmp_buf
))
201 /* No extension, or extension doesn't define execute
202 bits unambiguously. We are in for some dirty work.
203 Read the first two bytes of the file and see if they
204 are any of the known magic numbers which designate
206 Unix-like shells, which have executable shell scripts
207 without extensions and DON'T have "#!" as their FIRST
208 TWO CHARACTERS, lose here. Sorry, folks. */
209 if ( (_djstat_flags
& _STAT_EXEC_MAGIC
) == 0 )
211 switch (_get_magic(filename
, fhandle
))
213 case 0x5a4d: /* "MZ" */
216 case 0x2123: /* "#!" */
224 /* arch-tag: b0965811-8c3e-4bc4-8d81-4447a3594785
225 (do not change this comment) */