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