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
30 FILE_RCSID("@(#)$File: apptype.c,v 1.11 2009/02/04 18:24:32 christos Exp $")
38 #define INCL_DOSSESMGR
39 #define INCL_DOSERRORS
40 #define INCL_DOSFILEMGR
42 typedef ULONG APPTYPE;
45 file_os2_apptype(struct magic_set *ms, const char *fn, const void *buf,
49 char path[_MAX_PATH], drive[_MAX_DRIVE], dir[_MAX_DIR],
50 fname[_MAX_FNAME], ext[_MAX_EXT];
55 filename = strdup(fn);
56 else if ((filename = tempnam("./", "tmp")) == NULL) {
57 file_error(ms, errno, "cannot create tempnam");
60 /* qualify the filename to prevent extraneous searches */
61 _splitpath(filename, drive, dir, fname, ext);
62 (void)sprintf(path, "%s%s%s%s", drive,
63 (*dir == '\0') ? "./" : dir,
65 (*ext == '\0') ? "." : ext);
68 if ((fp = fopen(path, "wb")) == NULL) {
69 file_error(ms, errno, "cannot open tmp file `%s'", path);
72 if (fwrite(buf, 1, nb, fp) != nb) {
73 file_error(ms, errno, "cannot write tmp file `%s'",
79 rc = DosQueryAppType((unsigned char *)path, &type);
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);
93 printf("%s: error code = %lu\n", fname, rc);
98 * for our purpose here it's sufficient to just ignore the error and
99 * return w/o success (=0)
107 if (type & FAPPTYP_32BIT)
108 if (file_printf(ms, "32-bit ") == -1)
110 if (type & FAPPTYP_PHYSDRV) {
111 if (file_printf(ms, "physical device driver") == -1)
113 } else if (type & FAPPTYP_VIRTDRV) {
114 if (file_printf(ms, "virtual device driver") == -1)
116 } else if (type & FAPPTYP_DLL) {
117 if (type & FAPPTYP_PROTDLL)
118 if (file_printf(ms, "protected ") == -1)
120 if (file_printf(ms, "DLL") == -1)
122 } else if (type & (FAPPTYP_WINDOWSREAL | FAPPTYP_WINDOWSPROT)) {
123 if (file_printf(ms, "Windows executable") == -1)
125 } else if (type & FAPPTYP_DOS) {
127 * The API routine is partially broken on filenames ending
130 if (stricmp(ext, ".com") == 0)
131 if (strncmp((const char *)buf, "MZ", 2))
133 if (file_printf(ms, "DOS executable") == -1)
135 /* ---------------------------------------- */
136 /* Might learn more from the magic(4) entry */
137 if (file_printf(ms, ", magic(4)-> ") == -1)
140 /* ---------------------------------------- */
141 } else if (type & FAPPTYP_BOUND) {
142 if (file_printf(ms, "bound executable") == -1)
144 } else if ((type & 7) == FAPPTYP_WINDOWAPI) {
145 if (file_printf(ms, "PM executable") == -1)
147 } else if (file_printf(ms, "OS/2 executable") == -1)
150 switch (type & (FAPPTYP_NOTWINDOWCOMPAT |
151 FAPPTYP_WINDOWCOMPAT |
152 FAPPTYP_WINDOWAPI)) {
153 case FAPPTYP_NOTWINDOWCOMPAT:
154 if (file_printf(ms, " [NOTWINDOWCOMPAT]") == -1)
157 case FAPPTYP_WINDOWCOMPAT:
158 if (file_printf(ms, " [WINDOWCOMPAT]") == -1)
161 case FAPPTYP_WINDOWAPI:
162 if (file_printf(ms, " [WINDOWAPI]") == -1)