apptype.c 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  1. /*
  2. * Adapted from: apptype.c, Written by Eberhard Mattes and put into the
  3. * public domain
  4. *
  5. * Notes: 1. Qualify the filename so that DosQueryAppType does not do extraneous
  6. * 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: Moreover, it reports the type of the (new and very
  11. * bug ridden) Win Emacs as "OS/2 executable".
  12. *
  13. * 3. apptype() uses the filename if given, otherwise a tmp file is created with
  14. * the contents of buf. If buf is not the complete file, apptype can
  15. * incorrectly identify the exe type. The "-z" option of "file" is the reason
  16. * for this ugly code.
  17. */
  18. /*
  19. * amai: Darrel Hankerson did the changes described here.
  20. *
  21. * It remains to check the validity of comments (2.) since it's referred to an
  22. * "old" OS/2 version.
  23. *
  24. */
  25. #include "file.h"
  26. #include <stdio.h>
  27. #include <stdlib.h>
  28. #include <string.h>
  29. #ifndef lint
  30. FILE_RCSID("@(#)$Id: apptype.c,v 1.6 2003/11/11 20:01:45 christos Exp $")
  31. #endif /* lint */
  32. #ifdef __EMX__
  33. #include <io.h>
  34. #define INCL_DOSSESMGR
  35. #define INCL_DOSERRORS
  36. #define INCL_DOSFILEMGR
  37. #include <os2.h>
  38. typedef ULONG APPTYPE;
  39. protected int
  40. file_os2_apptype(struct magic_set *ms, const char *fn, const void *buf,
  41. size_t nb)
  42. {
  43. APPTYPE rc, type;
  44. char path[_MAX_PATH], drive[_MAX_DRIVE], dir[_MAX_DIR],
  45. fname[_MAX_FNAME], ext[_MAX_EXT];
  46. char *filename;
  47. FILE *fp;
  48. if (fn)
  49. filename = strdup(fn);
  50. else if ((filename = tempnam("./", "tmp")) == NULL) {
  51. file_error(ms, errno, "cannot create tempnam");
  52. return -1;
  53. }
  54. /* qualify the filename to prevent extraneous searches */
  55. _splitpath(filename, drive, dir, fname, ext);
  56. (void)sprintf(path, "%s%s%s%s", drive,
  57. (*dir == '\0') ? "./" : dir,
  58. fname,
  59. (*ext == '\0') ? "." : ext);
  60. if (fn == NULL) {
  61. if ((fp = fopen(path, "wb")) == NULL) {
  62. file_error(ms, errno, "cannot open tmp file `%s'", path);
  63. return -1;
  64. }
  65. if (fwrite(buf, 1, nb, fp) != nb) {
  66. file_error(ms, errno, "cannot write tmp file `%s'",
  67. path);
  68. return -1;
  69. }
  70. (void)fclose(fp);
  71. }
  72. rc = DosQueryAppType(path, &type);
  73. if (fn == NULL) {
  74. unlink(path);
  75. free(filename);
  76. }
  77. #if 0
  78. if (rc == ERROR_INVALID_EXE_SIGNATURE)
  79. printf("%s: not an executable file\n", fname);
  80. else if (rc == ERROR_FILE_NOT_FOUND)
  81. printf("%s: not found\n", fname);
  82. else if (rc == ERROR_ACCESS_DENIED)
  83. printf("%s: access denied\n", fname);
  84. else if (rc != 0)
  85. printf("%s: error code = %lu\n", fname, rc);
  86. else
  87. #else
  88. /*
  89. * for our purpose here it's sufficient to just ignore the error and
  90. * return w/o success (=0)
  91. */
  92. if (rc)
  93. return (0);
  94. #endif
  95. if (type & FAPPTYP_32BIT)
  96. if (file_printf(ms, "32-bit ") == -1)
  97. return -1;
  98. if (type & FAPPTYP_PHYSDRV) {
  99. if (file_printf(ms, "physical device driver") == -1)
  100. return -1;
  101. } else if (type & FAPPTYP_VIRTDRV) {
  102. if (file_printf(ms, "virtual device driver") == -1)
  103. return -1;
  104. } else if (type & FAPPTYP_DLL) {
  105. if (type & FAPPTYP_PROTDLL)
  106. if (file_printf(ms, "protected ") == -1)
  107. return -1;
  108. if (file_printf(ms, "DLL") == -1)
  109. return -1;
  110. } else if (type & (FAPPTYP_WINDOWSREAL | FAPPTYP_WINDOWSPROT)) {
  111. if (file_printf(ms, "Windows executable") == -1)
  112. return -1;
  113. } else if (type & FAPPTYP_DOS) {
  114. /*
  115. * The API routine is partially broken on filenames ending
  116. * ".com".
  117. */
  118. if (stricmp(ext, ".com") == 0)
  119. if (strncmp((const char *)buf, "MZ", 2))
  120. return (0);
  121. if (file_printf(ms, "DOS executable") == -1)
  122. return -1;
  123. /* ---------------------------------------- */
  124. /* Might learn more from the magic(4) entry */
  125. if (file_printf(ms, ", magic(4)-> ") == -1)
  126. return -1;
  127. return (0);
  128. /* ---------------------------------------- */
  129. } else if (type & FAPPTYP_BOUND) {
  130. if (file_printf(ms, "bound executable") == -1)
  131. return -1;
  132. } else if ((type & 7) == FAPPTYP_WINDOWAPI) {
  133. if (file_printf(ms, "PM executable") == -1)
  134. return -1;
  135. } else if (file_printf(ms, "OS/2 executable") == -1)
  136. return -1;
  137. switch (type & (FAPPTYP_NOTWINDOWCOMPAT |
  138. FAPPTYP_WINDOWCOMPAT |
  139. FAPPTYP_WINDOWAPI)) {
  140. case FAPPTYP_NOTWINDOWCOMPAT:
  141. if (file_printf(ms, " [NOTWINDOWCOMPAT]") == -1)
  142. return -1;
  143. break;
  144. case FAPPTYP_WINDOWCOMPAT:
  145. if (file_printf(ms, " [WINDOWCOMPAT]") == -1)
  146. return -1;
  147. break;
  148. case FAPPTYP_WINDOWAPI:
  149. if (file_printf(ms, " [WINDOWAPI]") == -1)
  150. return -1;
  151. break;
  152. }
  153. return 1;
  154. }
  155. #endif