2 * Adapted from: apptype.c, Written by Eberhard Mattes and put into the
5 * Notes: 1. Qualify the filename so that DosQueryAppType does not do extraneous
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".
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
20 * amai: Darrel Hankerson did the changes described here.
22 * It remains to check the validity of comments (2.) since it's referred to an
35 FILE_RCSID("@(#)$File: apptype.c,v 1.7 2007/01/12 17:38:27 christos Exp $")
40 #define INCL_DOSSESMGR
41 #define INCL_DOSERRORS
42 #define INCL_DOSFILEMGR
44 typedef ULONG APPTYPE;
47 file_os2_apptype(struct magic_set *ms, const char *fn, const void *buf,
51 char path[_MAX_PATH], drive[_MAX_DRIVE], dir[_MAX_DIR],
52 fname[_MAX_FNAME], ext[_MAX_EXT];
57 filename = strdup(fn);
58 else if ((filename = tempnam("./", "tmp")) == NULL) {
59 file_error(ms, errno, "cannot create tempnam");
62 /* qualify the filename to prevent extraneous searches */
63 _splitpath(filename, drive, dir, fname, ext);
64 (void)sprintf(path, "%s%s%s%s", drive,
65 (*dir == '\0') ? "./" : dir,
67 (*ext == '\0') ? "." : ext);
70 if ((fp = fopen(path, "wb")) == NULL) {
71 file_error(ms, errno, "cannot open tmp file `%s'", path);
74 if (fwrite(buf, 1, nb, fp) != nb) {
75 file_error(ms, errno, "cannot write tmp file `%s'",
81 rc = DosQueryAppType(path, &type);
88 if (rc == ERROR_INVALID_EXE_SIGNATURE)
89 printf("%s: not an executable file\n", fname);
90 else if (rc == ERROR_FILE_NOT_FOUND)
91 printf("%s: not found\n", fname);
92 else if (rc == ERROR_ACCESS_DENIED)
93 printf("%s: access denied\n", fname);
95 printf("%s: error code = %lu\n", fname, rc);
100 * for our purpose here it's sufficient to just ignore the error and
101 * return w/o success (=0)
109 if (type & FAPPTYP_32BIT)
110 if (file_printf(ms, "32-bit ") == -1)
112 if (type & FAPPTYP_PHYSDRV) {
113 if (file_printf(ms, "physical device driver") == -1)
115 } else if (type & FAPPTYP_VIRTDRV) {
116 if (file_printf(ms, "virtual device driver") == -1)
118 } else if (type & FAPPTYP_DLL) {
119 if (type & FAPPTYP_PROTDLL)
120 if (file_printf(ms, "protected ") == -1)
122 if (file_printf(ms, "DLL") == -1)
124 } else if (type & (FAPPTYP_WINDOWSREAL | FAPPTYP_WINDOWSPROT)) {
125 if (file_printf(ms, "Windows executable") == -1)
127 } else if (type & FAPPTYP_DOS) {
129 * The API routine is partially broken on filenames ending
132 if (stricmp(ext, ".com") == 0)
133 if (strncmp((const char *)buf, "MZ", 2))
135 if (file_printf(ms, "DOS executable") == -1)
137 /* ---------------------------------------- */
138 /* Might learn more from the magic(4) entry */
139 if (file_printf(ms, ", magic(4)-> ") == -1)
142 /* ---------------------------------------- */
143 } else if (type & FAPPTYP_BOUND) {
144 if (file_printf(ms, "bound executable") == -1)
146 } else if ((type & 7) == FAPPTYP_WINDOWAPI) {
147 if (file_printf(ms, "PM executable") == -1)
149 } else if (file_printf(ms, "OS/2 executable") == -1)
152 switch (type & (FAPPTYP_NOTWINDOWCOMPAT |
153 FAPPTYP_WINDOWCOMPAT |
154 FAPPTYP_WINDOWAPI)) {
155 case FAPPTYP_NOTWINDOWCOMPAT:
156 if (file_printf(ms, " [NOTWINDOWCOMPAT]") == -1)
159 case FAPPTYP_WINDOWCOMPAT:
160 if (file_printf(ms, " [WINDOWCOMPAT]") == -1)
163 case FAPPTYP_WINDOWAPI:
164 if (file_printf(ms, " [WINDOWAPI]") == -1)