(sh-mode-map): Don't remap
[emacs.git] / msdos / is_exec.c
blob899c574af0d557e67990643272dd279b55674d87
1 /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
2 /* IS_EXEC.C
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.
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>
19 #include <stdio.h>
20 #include <string.h>
21 #include <ctype.h>
22 #include <errno.h>
23 #include <dpmi.h>
24 #include <go32.h>
25 #include <io.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? */
40 unsigned short
41 _get_magic(const char *s, int fh)
43 __dpmi_regs regs;
44 unsigned short retval;
45 unsigned short fpos_high = 0, fpos_low = 0;
46 int read_fail = 0;
48 /* If given a pathname, open the file. */
49 if (s)
51 int handle;
52 if((handle = _open(s,0)) == -1)
53 return 0;
54 regs.x.bx = handle;
56 /* Else file already open. Remember its current file position
57 and move to beginning of file. */
58 else
60 regs.x.ax = 0x4201; /* set pointer from current position */
61 regs.x.bx = fh;
62 regs.x.cx = regs.x.dx = 0; /* move 0 bytes (i.e., stay put) */
63 __dpmi_int(0x21, &regs);
64 if (regs.x.flags & 1)
66 errno = __doserr_to_errno(regs.x.ax);
67 return 0;
69 fpos_high = regs.x.dx; /* got current position */
70 fpos_low = regs.x.ax;
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, &regs);
75 if (regs.x.flags & 1)
77 errno = __doserr_to_errno(regs.x.ax);
78 return 0;
81 regs.x.ds = __tb_segment;
82 regs.x.dx = __tb_offset;
84 /* Read 2 bytes from the file. */
85 regs.x.ax = 0x3f00;
86 regs.x.cx = 2;
87 __dpmi_int(0x21, &regs);
89 /* We can either (1) succeed, (2) read less than 2 bytes,
90 or (3) fail to read at all. */
91 if (regs.x.ax != 2)
92 read_fail = (regs.x.flags & 1) ? regs.x.ax : -1;
94 /* If called with filename, close the file. */
95 if (s)
97 regs.x.ax = 0x3e00;
98 __dpmi_int(0x21, &regs);
99 if (regs.x.flags & 1)
100 errno = __doserr_to_errno(regs.x.ax);
102 /* Else leave file pointer where we found it. */
103 else
105 regs.x.ax = 0x4200; /* set pointer from the beginning of file */
106 regs.x.bx = fh;
107 regs.x.cx = fpos_high;
108 regs.x.dx = fpos_low;
109 __dpmi_int(0x21, &regs);
110 if (regs.x.flags & 1)
112 errno = __doserr_to_errno(regs.x.ax);
113 return 0;
117 if (read_fail == 0)
118 retval = _farpeekw(_dos_ds, __tb);
119 else
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... */
124 retval = 0;
125 if (read_fail != -1)
126 errno = __doserr_to_errno(read_fail);
129 return retval;
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\
143 |BAS|BIB|BGI|BMP\
144 |C|CC|CFG|CGZ|CH3|CHR|CI|CLP|CMF|CPI|CPP|CXX\
145 |DAT|DBF|DIZ|DOC|DVI\
146 |E|EL|ELC\
147 |F77|FN3\
148 |GIF|GZ\
149 |H|HLP|HPP|HXX\
150 |ICO|IN|INC|INF|INI\
151 |JPG\
152 |L|LEX|LF|LIB|LOG|LST|LZH\
153 |M|MAK|MAP|MF|MID|MPG\
154 |O|OBJ\
155 |PAK|PAS|PBM|PCD|PCX|PDS|PIC|PIF|PN3|PRJ|PS\
156 |RAS|RGB|RLE\
157 |S|SND|SY3\
158 |TAR|TAZ|TEX|TGA|TGZ|TIF|TXH|TXI|TXT\
159 |VOC\
160 |WAV|WK1|WK3|WKB|WQ1|WQ3|WQ4|WQ5|WQ6|WQ!\
161 |XBM\
163 |ZIP|ZOO|";
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++)
173 if (*cp == '.')
174 ep = cp;
175 if (*cp == '/' || *cp == '\\' || *cp == ':')
176 ep = 0;
178 extension = ep;
180 if ((_djstat_flags & _STAT_EXEC_EXT) == 0
181 && extension
182 && *extension
183 && strlen(extension) <= ((extension[0]=='.') ? 4 : 3))
185 /* Search the list of extensions in executables[]. */
186 char tmp_buf[6], *tp = tmp_buf;
188 *tp++ = '|';
189 if (*extension == '.')
190 extension++;
191 while (*extension)
192 *tp++ = toupper (*extension++);
193 *tp++ = '|';
194 *tp = '\0';
195 if (strstr(non_executables, tmp_buf))
196 return 0;
197 else if (strstr(executables, tmp_buf))
198 return 1;
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
205 executable files.
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" */
214 case 0x010b:
215 case 0x014c:
216 case 0x2123: /* "#!" */
217 return 1;
221 return 0;