home *** CD-ROM | disk | FTP | other *** search
/ OS/2 Shareware BBS: 6 File / 06-File.zip / FILE39.ZIP / apptype.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-04-07  |  3.8 KB  |  147 lines

  1. /* Adapted from:
  2.    apptype.c, Written by Eberhard Mattes and put into the public domain
  3.  
  4. Notes:
  5. 1. Qualify the filename so that DosQueryAppType does not do
  6.    extraneous searches.
  7.  
  8. 2. DosQueryAppType will return FAPPTYP_DOS on a file ending with ".com"
  9.    (other than an OS/2 exe or Win exe with this name). Eberhard Mattes
  10.     remarks Tue, 6 Apr 93:
  11.       Moreover, it reports the type of the (new and very bug ridden) Win
  12.       Emacs as "OS/2 executable".
  13.  
  14. 3. apptype uses the filename if given, otherwise a tmp file is created
  15.     with the contents of buf. If buf is not the complete file, apptype
  16.     can incorrectly identify the exe type. The "-z" option of "file" is
  17.     the reason for this ugly code.
  18. */
  19.  
  20. #define INCL_DOSSESMGR
  21. #define INCL_DOSERRORS
  22. #include <os2.h>
  23. #if defined (MSC)
  24. #include "fapptyp.h"
  25. typedef     USHORT    APPTYPE;
  26. #else
  27. typedef    ULONG        APPTYPE;
  28. #endif
  29.  
  30. #include <stdio.h>
  31. #include <stdlib.h>
  32. #include <string.h>
  33.  
  34. extern void  ckfputs        (const char *, FILE *);
  35.  
  36. APPTYPE
  37. apptype (char *fn, char *buf, int nb)
  38. {
  39.     APPTYPE    rc, type;
  40.     char    path[_MAX_PATH], drive[_MAX_DRIVE], dir[_MAX_DIR],
  41.             fname[_MAX_FNAME], ext[_MAX_EXT], *filename;
  42.     FILE    *fp;
  43.  
  44. #if defined (MSDOS)
  45.     return(0);             /* No special checks under MSDOS */
  46. #else
  47.     if (fn)
  48.         filename = fn;
  49.     else 
  50.         if ((filename = tempnam("./", "tmp")) == NULL) {
  51.             error("can't create tempnam (%s).\n", strerror(errno));
  52.             /*NOTREACHED*/
  53.         }
  54.  
  55.     /* qualify the filename to prevent extraneous searches */
  56.     _splitpath(filename, drive, dir, fname, ext);
  57.     sprintf(path, "%s%s%s%s", drive,
  58.             (*dir == '\0') ? "./" : dir,
  59.             fname,
  60.             (*ext == '\0') ? "." : ext);
  61.  
  62.     if (fn == NULL) {
  63.         if ((fp = fopen(path, "wb")) == NULL) {
  64.             error("can't open tmp file '%s' (%s).\n", path, strerror(errno));
  65.             /*NOTREACHED*/
  66.         }
  67.         if (fwrite(buf, 1, nb, fp) != nb) {
  68.             error("can't write tmp file '%s' (%s).\n", path, strerror(errno));
  69.             /*NOTREACHED*/
  70.         }
  71.         fclose(fp);
  72.     }
  73.  
  74. #if defined (MSC)
  75.     rc = DosQAppType (path, &type);
  76. #else
  77.       rc = DosQueryAppType (path, &type);
  78. #endif
  79.     
  80.     if (fn == NULL) {
  81.         unlink(path);
  82.         free(filename);
  83.     }
  84.  
  85. /*
  86.       if (rc == ERROR_INVALID_EXE_SIGNATURE) 
  87.         printf ("%s: not an executable file\n", fname); 
  88.       else if (rc == ERROR_FILE_NOT_FOUND) 
  89.         printf ("%s: not found\n", fname); 
  90.       else if (rc == ERROR_ACCESS_DENIED)
  91.         printf ("%s: access denied\n", fname);
  92.       else if (rc != 0)
  93.         printf ("%s: error code = %lu\n", fname, rc);
  94.       else
  95. */
  96.  
  97.     if (rc)
  98.         return(type);
  99.    if (type & FAPPTYP_32BIT)
  100.        ckfputs("32-bit ", stdout); 
  101.    if (type & FAPPTYP_PHYSDRV) {
  102.         ckfputs("physical device driver", stdout);
  103.     }
  104.    else if (type & FAPPTYP_VIRTDRV) {
  105.         ckfputs("virtual device driver", stdout);
  106.     }
  107.    else if (type & FAPPTYP_DLL) {
  108.         if (type & FAPPTYP_PROTDLL)
  109.             ckfputs("protected ", stdout);
  110.             ckfputs("DLL", stdout);
  111.    }
  112.    else if (type & (FAPPTYP_WINDOWSREAL|FAPPTYP_WINDOWSPROT)) {
  113.        ckfputs("Windows executable", stdout);
  114.     }
  115.    else if (type & FAPPTYP_DOS) {
  116.         /* The API routine is partially broken on filenames ending ".com". */
  117.         if (stricmp(ext, ".com") == 0)
  118.             if (strncmp(buf, "MZ", 2)) type = 0;
  119.         if (type)
  120.             ckfputs("DOS executable", stdout);
  121.     }
  122.    else if (type & FAPPTYP_BOUND) {
  123.         ckfputs("bound executable", stdout);
  124.     }
  125.    else if ((type & 7) == FAPPTYP_WINDOWAPI) {
  126.         ckfputs("PM executable", stdout);
  127.     }
  128.    else
  129.         ckfputs("OS/2 executable", stdout);
  130.  
  131.     switch(type & (FAPPTYP_NOTWINDOWCOMPAT |
  132.                             FAPPTYP_WINDOWCOMPAT |
  133.                             FAPPTYP_WINDOWAPI)) {
  134.         case FAPPTYP_NOTWINDOWCOMPAT:
  135.             ckfputs(" [NOTWINDOWCOMPAT]", stdout);
  136.             break;
  137.         case FAPPTYP_WINDOWCOMPAT:
  138.             ckfputs(" [WINDOWCOMPAT]", stdout);
  139.             break;
  140.         case FAPPTYP_WINDOWAPI:
  141.             ckfputs(" [WINDOWAPI]", stdout);
  142.             break;
  143.         }
  144.   return (type);
  145. #endif
  146. }
  147.