Subversion Repositories Kolibri OS

Rev

Go to most recent revision | Blame | Last modification | View Log | Download | RSS feed

  1. /* Copyright (C) 1995 DJ Delorie, see COPYING.DJ for details */
  2. /* IS_EXEC.C
  3.  *
  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.
  10.  *
  11.  * Copyright (c) 1994 Eli Zaretskii <eliz@is.elta.co.il>
  12.  *
  13.  * This software may be used freely so long as this copyright notice is
  14.  * left intact.  There is no warranty on this software.
  15.  *
  16.  */
  17.  
  18. #include <libc/stubs.h>
  19. #include <stdio.h>
  20. #include <string.h>
  21. #include <errno.h>
  22. #include <libc/farptrgs.h>
  23. #include <libc/dosio.h>
  24.  
  25. extern unsigned short _djstat_flags;
  26. unsigned short        _get_magic(const char *, int);
  27. int                   _is_executable(const char *, int, const char *);
  28.  
  29. /*
  30.  * Read a MAGIC NUMBER from a given file.  These are the first
  31.  * two bytes of the file, if we look at them as an unsigned short. */
  32.  
  33. #define _STAT_EXEC_EXT      2   /* get execute bits from file extension? */
  34. #define _STAT_EXEC_MAGIC    4   /* get execute bits from magic signature? */
  35.  
  36. unsigned short _get_magic(const char *s, int fh)
  37. {
  38. }
  39.  
  40. /* A list of extensions which designate executable files.  These
  41.    are NOT tested for the magic number.  */
  42. static char executables[] = "|EXE|COM|BAT|BTM|DLL|VXD|";
  43.  
  44. /* A list of extensions which belong to files known to NEVER be
  45.    executables.  These exist to minimize read()'ing files while
  46.    detecting executables by magic number.  You are welcome to
  47.    add to this list, but remember: only extensions which could
  48.    NEVER be present in executables should go here.  */
  49. static char non_executables[] = "\
  50. |A|A01|A02|A03|A04|A05|ADL|ARC|ARJ|ASC|ASM|AUX|AWK\
  51. |BAS|BIB|BGI|BMP\
  52. |C|CC|CFG|CGZ|CH3|CHR|CI|CLP|CMF|CPI|CPP|CXX\
  53. |DAT|DBF|DIZ|DOC|DVI\
  54. |E|EL|ELC\
  55. |F77|FN3\
  56. |GIF|GZ\
  57. |H|HLP|HPP|HXX\
  58. |ICO|IN|INC|INF|INI\
  59. |JPG\
  60. |L|LEX|LF|LIB|LOG|LST|LZH\
  61. |M|MAK|MAP|MF|MID|MPG\
  62. |O|OBJ\
  63. |PAK|PAS|PBM|PCD|PCX|PDS|PIC|PIF|PN3|PRJ|PS\
  64. |RAS|RGB|RLE\
  65. |S|SND|SY3\
  66. |TAR|TAZ|TEX|TGA|TGZ|TIF|TXH|TXI|TXT\
  67. |VOC\
  68. |WAV|WK1|WK3|WKB|WQ1|WQ3|WQ4|WQ5|WQ6|WQ!\
  69. |XBM\
  70. |Y\
  71. |ZIP|ZOO|";
  72.  
  73. int _is_executable(const char *filename, int fhandle, const char *extension)
  74. {
  75.   if (!extension && filename)
  76.   {
  77.     const char *cp, *ep=0;
  78.     for (cp=filename; *cp; cp++)
  79.     {
  80.       if (*cp == '.')
  81.         ep = cp;
  82.       if (*cp == '/' || *cp == '\\' || *cp == ':')
  83.         ep = 0;
  84.     }
  85.     extension = ep;
  86.   }
  87.   if ((_djstat_flags & _STAT_EXEC_EXT) == 0
  88.       && extension
  89.       && *extension
  90.       && strlen(extension) <= ((extension[0]=='.') ? 4 : 3))
  91.     {
  92.       /* Search the list of extensions in executables[]. */
  93.       char tmp_buf[6];
  94.  
  95.       tmp_buf[0] = '|';
  96.       strcpy(tmp_buf+1, *extension == '.' ? extension + 1 : extension);
  97.       strcat(tmp_buf, "|");
  98.       if (strstr(non_executables, tmp_buf))
  99.         return 0;
  100.       else if (strstr(executables, tmp_buf))
  101.         return 1;
  102.     }
  103.  
  104.   /* No extension, or extension doesn't define execute
  105.      bits unambiguously.  We are in for some dirty work.
  106.      Read the first two bytes of the file and see if they
  107.      are any of the known magic numbers which designate
  108.      executable files.
  109.      Unix-like shells, which have executable shell scripts
  110.      without extensions and DON'T have "#!" as their FIRST
  111.      TWO CHARACTERS, lose here.  Sorry, folks.  */
  112.   if ( (_djstat_flags & _STAT_EXEC_MAGIC) == 0 )
  113.     {
  114.       switch (_get_magic(filename, fhandle))
  115.         {
  116.           case 0x5a4d:      /* "MZ" */
  117.           case 0x010b:
  118.           case 0x014c:
  119.           case 0x2123:      /* "#!" */
  120.               return 1;
  121.         }
  122.     }
  123.  
  124.   return 0;
  125. }
  126.