is_exec.c 6.5 KB

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