xref: /dflybsd-src/contrib/file/src/apptype.c (revision 6fca56fb90a257291c53bba3b861b751027c3e3d)
1327e51cbSPeter Avalos /*
2327e51cbSPeter Avalos  * Adapted from: apptype.c, Written by Eberhard Mattes and put into the
3327e51cbSPeter Avalos  * public domain
4327e51cbSPeter Avalos  *
5327e51cbSPeter Avalos  * Notes: 1. Qualify the filename so that DosQueryAppType does not do extraneous
6327e51cbSPeter Avalos  * searches.
7327e51cbSPeter Avalos  *
8327e51cbSPeter Avalos  * 2. DosQueryAppType will return FAPPTYP_DOS on a file ending with ".com"
9327e51cbSPeter Avalos  * (other than an OS/2 exe or Win exe with this name). Eberhard Mattes
10327e51cbSPeter Avalos  * remarks Tue, 6 Apr 93: Moreover, it reports the type of the (new and very
11327e51cbSPeter Avalos  * bug ridden) Win Emacs as "OS/2 executable".
12327e51cbSPeter Avalos  *
13327e51cbSPeter Avalos  * 3. apptype() uses the filename if given, otherwise a tmp file is created with
14327e51cbSPeter Avalos  * the contents of buf. If buf is not the complete file, apptype can
15327e51cbSPeter Avalos  * incorrectly identify the exe type. The "-z" option of "file" is the reason
16327e51cbSPeter Avalos  * for this ugly code.
17327e51cbSPeter Avalos  */
18327e51cbSPeter Avalos 
19327e51cbSPeter Avalos /*
20327e51cbSPeter Avalos  * amai: Darrel Hankerson did the changes described here.
21327e51cbSPeter Avalos  *
22327e51cbSPeter Avalos  * It remains to check the validity of comments (2.) since it's referred to an
23327e51cbSPeter Avalos  * "old" OS/2 version.
24327e51cbSPeter Avalos  *
25327e51cbSPeter Avalos  */
26327e51cbSPeter Avalos 
27327e51cbSPeter Avalos #include "file.h"
28327e51cbSPeter Avalos 
2979343712SPeter Avalos #ifndef	lint
30*6fca56fbSSascha Wildner FILE_RCSID("@(#)$File: apptype.c,v 1.14 2018/09/09 20:33:28 christos Exp $")
3179343712SPeter Avalos #endif /* lint */
3279343712SPeter Avalos 
33327e51cbSPeter Avalos #include <stdlib.h>
34327e51cbSPeter Avalos #include <string.h>
35327e51cbSPeter Avalos 
36327e51cbSPeter Avalos #ifdef __EMX__
37327e51cbSPeter Avalos #include <io.h>
38327e51cbSPeter Avalos #define INCL_DOSSESMGR
39327e51cbSPeter Avalos #define INCL_DOSERRORS
40327e51cbSPeter Avalos #define INCL_DOSFILEMGR
41327e51cbSPeter Avalos #include <os2.h>
42327e51cbSPeter Avalos typedef ULONG   APPTYPE;
43327e51cbSPeter Avalos 
44327e51cbSPeter Avalos protected int
file_os2_apptype(struct magic_set * ms,const char * fn,const void * buf,size_t nb)45327e51cbSPeter Avalos file_os2_apptype(struct magic_set *ms, const char *fn, const void *buf,
46327e51cbSPeter Avalos     size_t nb)
47327e51cbSPeter Avalos {
48327e51cbSPeter Avalos 	APPTYPE         rc, type;
49327e51cbSPeter Avalos 	char            path[_MAX_PATH], drive[_MAX_DRIVE], dir[_MAX_DIR],
50327e51cbSPeter Avalos 			fname[_MAX_FNAME], ext[_MAX_EXT];
51327e51cbSPeter Avalos 	char           *filename;
52327e51cbSPeter Avalos 	FILE           *fp;
53327e51cbSPeter Avalos 
54327e51cbSPeter Avalos 	if (fn)
55327e51cbSPeter Avalos 		filename = strdup(fn);
56327e51cbSPeter Avalos 	else if ((filename = tempnam("./", "tmp")) == NULL) {
57327e51cbSPeter Avalos 		file_error(ms, errno, "cannot create tempnam");
58327e51cbSPeter Avalos 		return -1;
59327e51cbSPeter Avalos 	}
60327e51cbSPeter Avalos 	/* qualify the filename to prevent extraneous searches */
61327e51cbSPeter Avalos 	_splitpath(filename, drive, dir, fname, ext);
62327e51cbSPeter Avalos 	(void)sprintf(path, "%s%s%s%s", drive,
63327e51cbSPeter Avalos 		(*dir == '\0') ? "./" : dir,
64327e51cbSPeter Avalos 		fname,
65327e51cbSPeter Avalos 		(*ext == '\0') ? "." : ext);
66327e51cbSPeter Avalos 
67327e51cbSPeter Avalos 	if (fn == NULL) {
68327e51cbSPeter Avalos 		if ((fp = fopen(path, "wb")) == NULL) {
69327e51cbSPeter Avalos 			file_error(ms, errno, "cannot open tmp file `%s'", path);
70327e51cbSPeter Avalos 			return -1;
71327e51cbSPeter Avalos 		}
72327e51cbSPeter Avalos 		if (fwrite(buf, 1, nb, fp) != nb) {
73327e51cbSPeter Avalos 			file_error(ms, errno, "cannot write tmp file `%s'",
74327e51cbSPeter Avalos 			    path);
75a96e001bSPeter Avalos 			(void)fclose(fp);
76327e51cbSPeter Avalos 			return -1;
77327e51cbSPeter Avalos 		}
78327e51cbSPeter Avalos 		(void)fclose(fp);
79327e51cbSPeter Avalos 	}
8079343712SPeter Avalos 	rc = DosQueryAppType((unsigned char *)path, &type);
81327e51cbSPeter Avalos 
82327e51cbSPeter Avalos 	if (fn == NULL) {
83327e51cbSPeter Avalos 		unlink(path);
84327e51cbSPeter Avalos 		free(filename);
85327e51cbSPeter Avalos 	}
86327e51cbSPeter Avalos #if 0
87327e51cbSPeter Avalos 	if (rc == ERROR_INVALID_EXE_SIGNATURE)
88327e51cbSPeter Avalos 		printf("%s: not an executable file\n", fname);
89327e51cbSPeter Avalos 	else if (rc == ERROR_FILE_NOT_FOUND)
90327e51cbSPeter Avalos 		printf("%s: not found\n", fname);
91327e51cbSPeter Avalos 	else if (rc == ERROR_ACCESS_DENIED)
92327e51cbSPeter Avalos 		printf("%s: access denied\n", fname);
93327e51cbSPeter Avalos 	else if (rc != 0)
94327e51cbSPeter Avalos 		printf("%s: error code = %lu\n", fname, rc);
95327e51cbSPeter Avalos 	else
96327e51cbSPeter Avalos #else
97327e51cbSPeter Avalos 
98327e51cbSPeter Avalos 	/*
99327e51cbSPeter Avalos 	 * for our purpose here it's sufficient to just ignore the error and
100327e51cbSPeter Avalos 	 * return w/o success (=0)
101327e51cbSPeter Avalos 	 */
102327e51cbSPeter Avalos 
103327e51cbSPeter Avalos 	if (rc)
104327e51cbSPeter Avalos 		return (0);
105327e51cbSPeter Avalos 
106327e51cbSPeter Avalos #endif
107327e51cbSPeter Avalos 
108327e51cbSPeter Avalos 	if (type & FAPPTYP_32BIT)
109327e51cbSPeter Avalos 		if (file_printf(ms, "32-bit ") == -1)
110327e51cbSPeter Avalos 			return -1;
111327e51cbSPeter Avalos 	if (type & FAPPTYP_PHYSDRV) {
112327e51cbSPeter Avalos 		if (file_printf(ms, "physical device driver") == -1)
113327e51cbSPeter Avalos 			return -1;
114327e51cbSPeter Avalos 	} else if (type & FAPPTYP_VIRTDRV) {
115327e51cbSPeter Avalos 		if (file_printf(ms, "virtual device driver") == -1)
116327e51cbSPeter Avalos 			return -1;
117327e51cbSPeter Avalos 	} else if (type & FAPPTYP_DLL) {
118327e51cbSPeter Avalos 		if (type & FAPPTYP_PROTDLL)
119327e51cbSPeter Avalos 			if (file_printf(ms, "protected ") == -1)
120327e51cbSPeter Avalos 				return -1;
121327e51cbSPeter Avalos 		if (file_printf(ms, "DLL") == -1)
122327e51cbSPeter Avalos 			return -1;
123327e51cbSPeter Avalos 	} else if (type & (FAPPTYP_WINDOWSREAL | FAPPTYP_WINDOWSPROT)) {
124327e51cbSPeter Avalos 		if (file_printf(ms, "Windows executable") == -1)
125327e51cbSPeter Avalos 			return -1;
126327e51cbSPeter Avalos 	} else if (type & FAPPTYP_DOS) {
127327e51cbSPeter Avalos 		/*
128327e51cbSPeter Avalos 		 * The API routine is partially broken on filenames ending
129327e51cbSPeter Avalos 		 * ".com".
130327e51cbSPeter Avalos 		 */
131327e51cbSPeter Avalos 		if (stricmp(ext, ".com") == 0)
132327e51cbSPeter Avalos 			if (strncmp((const char *)buf, "MZ", 2))
133327e51cbSPeter Avalos 				return (0);
134327e51cbSPeter Avalos 		if (file_printf(ms, "DOS executable") == -1)
135327e51cbSPeter Avalos 			return -1;
136327e51cbSPeter Avalos 		/* ---------------------------------------- */
137327e51cbSPeter Avalos 		/* Might learn more from the magic(4) entry */
138327e51cbSPeter Avalos 		if (file_printf(ms, ", magic(4)-> ") == -1)
139327e51cbSPeter Avalos 			return -1;
140327e51cbSPeter Avalos 		return (0);
141327e51cbSPeter Avalos 		/* ---------------------------------------- */
142327e51cbSPeter Avalos 	} else if (type & FAPPTYP_BOUND) {
143327e51cbSPeter Avalos 		if (file_printf(ms, "bound executable") == -1)
144327e51cbSPeter Avalos 			return -1;
145327e51cbSPeter Avalos 	} else if ((type & 7) == FAPPTYP_WINDOWAPI) {
146327e51cbSPeter Avalos 		if (file_printf(ms, "PM executable") == -1)
147327e51cbSPeter Avalos 			return -1;
148327e51cbSPeter Avalos 	} else if (file_printf(ms, "OS/2 executable") == -1)
149327e51cbSPeter Avalos 		return -1;
150327e51cbSPeter Avalos 
151327e51cbSPeter Avalos 	switch (type & (FAPPTYP_NOTWINDOWCOMPAT |
152327e51cbSPeter Avalos 			FAPPTYP_WINDOWCOMPAT |
153327e51cbSPeter Avalos 			FAPPTYP_WINDOWAPI)) {
154327e51cbSPeter Avalos 	case FAPPTYP_NOTWINDOWCOMPAT:
155327e51cbSPeter Avalos 		if (file_printf(ms, " [NOTWINDOWCOMPAT]") == -1)
156327e51cbSPeter Avalos 			return -1;
157327e51cbSPeter Avalos 		break;
158327e51cbSPeter Avalos 	case FAPPTYP_WINDOWCOMPAT:
159327e51cbSPeter Avalos 		if (file_printf(ms, " [WINDOWCOMPAT]") == -1)
160327e51cbSPeter Avalos 			return -1;
161327e51cbSPeter Avalos 		break;
162327e51cbSPeter Avalos 	case FAPPTYP_WINDOWAPI:
163327e51cbSPeter Avalos 		if (file_printf(ms, " [WINDOWAPI]") == -1)
164327e51cbSPeter Avalos 			return -1;
165327e51cbSPeter Avalos 		break;
166327e51cbSPeter Avalos 	}
167327e51cbSPeter Avalos 	return 1;
168327e51cbSPeter Avalos }
169327e51cbSPeter Avalos #endif
170