home *** CD-ROM | disk | FTP | other *** search
/ PC Extra Super CD 1998 January / PCPLUS131.iso / DJGPP / V2 / DJLSR201.ZIP / src / libc / posix / sys / stat / is_exec.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-10-19  |  6.3 KB  |  224 lines

  1. /* Copyright (C) 1996 DJ Delorie, see COPYING.DJ for details */
  2. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  3. /* IS_EXEC.C
  4.  *
  5.  * Given a filename or a file handle, and the extension of the file,
  6.  * determine if the file is executable.
  7.  * First, the file extension is checked in case it uniquely identifies
  8.  * the file as either an executable or not.  Failing this, the first
  9.  * two bytes of the file are tested for known signatures of executable
  10.  * files.
  11.  *
  12.  * Copyright (c) 1994 Eli Zaretskii <eliz@is.elta.co.il>
  13.  *
  14.  * This software may be used freely so long as this copyright notice is
  15.  * left intact.  There is no warranty on this software.
  16.  *
  17.  */
  18.  
  19. #include <libc/stubs.h>
  20. #include <stdio.h>
  21. #include <string.h>
  22. #include <ctype.h>
  23. #include <errno.h>
  24. #include <dpmi.h>
  25. #include <go32.h>
  26. #include <io.h>
  27. #include <libc/farptrgs.h>
  28. #include <libc/dosio.h>
  29.  
  30. extern unsigned short _djstat_flags;
  31. unsigned short        _get_magic(const char *, int);
  32. int                   _is_executable(const char *, int, const char *);
  33.  
  34. /*
  35.  * Read a MAGIC NUMBER from a given file.  These are the first
  36.  * two bytes of the file, if we look at them as an unsigned short. */
  37.  
  38. #define _STAT_EXEC_EXT      2   /* get execute bits from file extension? */
  39. #define _STAT_EXEC_MAGIC    4   /* get execute bits from magic signature? */
  40.  
  41. unsigned short
  42. _get_magic(const char *s, int fh)
  43. {
  44.   __dpmi_regs          regs;
  45.   unsigned short       retval;
  46.   unsigned short       fpos_high = 0, fpos_low = 0;
  47.   int                  read_fail = 0;
  48.  
  49.   /* If given a pathname, open the file. */
  50.   if (s)
  51.   {
  52.     int handle;
  53.     if((handle = _open(s,0)) == -1)
  54.       return 0;
  55.     regs.x.bx = handle;
  56.   }
  57.   /* Else file already open.  Remember its current file position
  58.      and move to beginning of file. */
  59.   else
  60.   {
  61.     regs.x.ax = 0x4201;        /* set pointer from current position */
  62.     regs.x.bx = fh;
  63.     regs.x.cx = regs.x.dx = 0;    /* move 0 bytes (i.e., stay put) */
  64.     __dpmi_int(0x21, ®s);
  65.     if (regs.x.flags & 1)
  66.     {
  67.       errno = __doserr_to_errno(regs.x.ax);
  68.       return 0;
  69.     }
  70.     fpos_high = regs.x.dx;    /* got current position */
  71.     fpos_low  = regs.x.ax;
  72.  
  73.     regs.x.ax = 0x4200;        /* set pointer from the beginning of file */
  74.     regs.x.cx = regs.x.dx = 0;    /* move to beginning of file */
  75.     __dpmi_int(0x21, ®s);
  76.     if (regs.x.flags & 1)
  77.     {
  78.       errno = __doserr_to_errno(regs.x.ax);
  79.       return 0;
  80.     }
  81.   }
  82.   regs.x.ds = __tb_segment;
  83.   regs.x.dx = __tb_offset;
  84.  
  85.   /* Read 2 bytes from the file. */
  86.   regs.x.ax = 0x3f00;
  87.   regs.x.cx = 2;
  88.   __dpmi_int(0x21, ®s);
  89.  
  90.   /* We can either (1) succeed, (2) read less than 2 bytes,
  91.      or (3) fail to read at all.  */
  92.   if (regs.x.ax != 2)
  93.     read_fail = (regs.x.flags & 1) ? regs.x.ax : -1;
  94.  
  95.   /* If called with filename, close the file. */
  96.   if (s)
  97.   {
  98.     regs.x.ax = 0x3e00;
  99.     __dpmi_int(0x21, ®s);
  100.     if (regs.x.flags & 1)
  101.       errno = __doserr_to_errno(regs.x.ax);
  102.   }
  103.   /* Else leave file pointer where we found it. */
  104.   else
  105.   {
  106.     regs.x.ax = 0x4200;        /* set pointer from the beginning of file */
  107.     regs.x.bx = fh;
  108.     regs.x.cx = fpos_high;
  109.     regs.x.dx = fpos_low;
  110.     __dpmi_int(0x21, ®s);
  111.     if (regs.x.flags & 1)
  112.     {
  113.       errno = __doserr_to_errno(regs.x.ax);
  114.       return 0;
  115.     }
  116.   }
  117.  
  118.   if (read_fail == 0)
  119.     retval = _farpeekw(_dos_ds, __tb);
  120.   else
  121.   {
  122.     /* The file couldn't be read: assume non-executable.  If the file
  123.        *is* executable, but was passed as a file-handle, and the user
  124.        opened it in write-only mode, they lose...  */
  125.     retval = 0;
  126.     if (read_fail != -1)
  127.       errno = __doserr_to_errno(read_fail);
  128.   }
  129.  
  130.   return retval;
  131. }
  132.  
  133. /* A list of extensions which designate executable files.  These
  134.    are NOT tested for the magic number.  */
  135. static char executables[] = "|EXE|COM|BAT|BTM|DLL|VXD|";
  136.  
  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. |GDT|GIF|GPR|GZ\
  150. |H|HLP|HPP|HXX\
  151. |ICO|IN|INC|INF|INI|INZ\
  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|RGD|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.  
  166. int
  167. _is_executable(const char *filename, int fhandle, const char *extension)
  168. {
  169.   if (!extension && filename)
  170.   {
  171.     const char *cp, *ep=0;
  172.     for (cp=filename; *cp; cp++)
  173.     {
  174.       if (*cp == '.')
  175.     ep = cp;
  176.       if (*cp == '/' || *cp == '\\' || *cp == ':')
  177.     ep = 0;
  178.     }
  179.     extension = ep;
  180.   }
  181.   if ((_djstat_flags & _STAT_EXEC_EXT) == 0
  182.       && extension
  183.       && *extension
  184.       && strlen(extension) <= ((extension[0]=='.') ? 4 : 3))
  185.     {
  186.       /* Search the list of extensions in executables[]. */
  187.       char tmp_buf[6], *tp = tmp_buf;
  188.  
  189.       *tp++ = '|';
  190.       if (*extension == '.')
  191.      extension++;
  192.       while (*extension)
  193.      *tp++ = toupper (*extension++);
  194.       *tp++ = '|';
  195.       *tp = '\0';
  196.       if (strstr(non_executables, tmp_buf))
  197.         return 0;
  198.       else if (strstr(executables, tmp_buf))
  199.         return 1;
  200.     }
  201.  
  202.   /* No extension, or extension doesn't define execute
  203.      bits unambiguously.  We are in for some dirty work.
  204.      Read the first two bytes of the file and see if they
  205.      are any of the known magic numbers which designate
  206.      executable files.
  207.      Unix-like shells, which have executable shell scripts
  208.      without extensions and DON'T have "#!" as their FIRST
  209.      TWO CHARACTERS, lose here.  Sorry, folks.  */
  210.   if ( (_djstat_flags & _STAT_EXEC_MAGIC) == 0 )
  211.     {
  212.       switch (_get_magic(filename, fhandle))
  213.         {
  214.           case 0x5a4d:      /* "MZ" */
  215.           case 0x010b:
  216.           case 0x014c:
  217.           case 0x2123:      /* "#!" */
  218.               return 1;
  219.         }
  220.     }
  221.  
  222.   return 0;
  223. }
  224.