Update to file-4.19.
[dragonfly.git] / contrib / file-4 / src / apptype.c
CommitLineData
ab0b56cc
JS
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/*
20 * amai: Darrel Hankerson did the changes described here.
21 *
22 * It remains to check the validity of comments (2.) since it's referred to an
23 * "old" OS/2 version.
24 *
25 */
26
27#include "file.h"
28
29#include <stdio.h>
30#include <stdlib.h>
31#include <string.h>
32
33
34#ifndef lint
35FILE_RCSID("@(#)$Id: apptype.c,v 1.6 2003/11/11 20:01:45 christos Exp $")
36#endif /* lint */
37
38#ifdef __EMX__
39#include <io.h>
40#define INCL_DOSSESMGR
41#define INCL_DOSERRORS
42#define INCL_DOSFILEMGR
43#include <os2.h>
44typedef ULONG APPTYPE;
45
46protected int
47file_os2_apptype(struct magic_set *ms, const char *fn, const void *buf,
48 size_t nb)
49{
50 APPTYPE rc, type;
51 char path[_MAX_PATH], drive[_MAX_DRIVE], dir[_MAX_DIR],
52 fname[_MAX_FNAME], ext[_MAX_EXT];
53 char *filename;
54 FILE *fp;
55
56 if (fn)
57 filename = strdup(fn);
58 else if ((filename = tempnam("./", "tmp")) == NULL) {
59 file_error(ms, errno, "cannot create tempnam");
60 return -1;
61 }
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,
66 fname,
67 (*ext == '\0') ? "." : ext);
68
69 if (fn == NULL) {
70 if ((fp = fopen(path, "wb")) == NULL) {
71 file_error(ms, errno, "cannot open tmp file `%s'", path);
72 return -1;
73 }
74 if (fwrite(buf, 1, nb, fp) != nb) {
75 file_error(ms, errno, "cannot write tmp file `%s'",
76 path);
77 return -1;
78 }
79 (void)fclose(fp);
80 }
81 rc = DosQueryAppType(path, &type);
82
83 if (fn == NULL) {
84 unlink(path);
85 free(filename);
86 }
87#if 0
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);
94 else if (rc != 0)
95 printf("%s: error code = %lu\n", fname, rc);
96 else
97#else
98
99 /*
100 * for our purpose here it's sufficient to just ignore the error and
101 * return w/o success (=0)
102 */
103
104 if (rc)
105 return (0);
106
107#endif
108
109 if (type & FAPPTYP_32BIT)
110 if (file_printf(ms, "32-bit ") == -1)
111 return -1;
112 if (type & FAPPTYP_PHYSDRV) {
113 if (file_printf(ms, "physical device driver") == -1)
114 return -1;
115 } else if (type & FAPPTYP_VIRTDRV) {
116 if (file_printf(ms, "virtual device driver") == -1)
117 return -1;
118 } else if (type & FAPPTYP_DLL) {
119 if (type & FAPPTYP_PROTDLL)
120 if (file_printf(ms, "protected ") == -1)
121 return -1;
122 if (file_printf(ms, "DLL") == -1)
123 return -1;
124 } else if (type & (FAPPTYP_WINDOWSREAL | FAPPTYP_WINDOWSPROT)) {
125 if (file_printf(ms, "Windows executable") == -1)
126 return -1;
127 } else if (type & FAPPTYP_DOS) {
128 /*
129 * The API routine is partially broken on filenames ending
130 * ".com".
131 */
132 if (stricmp(ext, ".com") == 0)
133 if (strncmp((const char *)buf, "MZ", 2))
134 return (0);
135 if (file_printf(ms, "DOS executable") == -1)
136 return -1;
137 /* ---------------------------------------- */
138 /* Might learn more from the magic(4) entry */
139 if (file_printf(ms, ", magic(4)-> ") == -1)
140 return -1;
141 return (0);
142 /* ---------------------------------------- */
143 } else if (type & FAPPTYP_BOUND) {
144 if (file_printf(ms, "bound executable") == -1)
145 return -1;
146 } else if ((type & 7) == FAPPTYP_WINDOWAPI) {
147 if (file_printf(ms, "PM executable") == -1)
148 return -1;
149 } else if (file_printf(ms, "OS/2 executable") == -1)
150 return -1;
151
152 switch (type & (FAPPTYP_NOTWINDOWCOMPAT |
153 FAPPTYP_WINDOWCOMPAT |
154 FAPPTYP_WINDOWAPI)) {
155 case FAPPTYP_NOTWINDOWCOMPAT:
156 if (file_printf(ms, " [NOTWINDOWCOMPAT]") == -1)
157 return -1;
158 break;
159 case FAPPTYP_WINDOWCOMPAT:
160 if (file_printf(ms, " [WINDOWCOMPAT]") == -1)
161 return -1;
162 break;
163 case FAPPTYP_WINDOWAPI:
164 if (file_printf(ms, " [WINDOWAPI]") == -1)
165 return -1;
166 break;
167 }
168 return 1;
169}
170#endif