"Fossies" - the Fresh Open Source Software Archive 
Member "file-5.35/src/apptype.c" (11 Sep 2018, 4522 Bytes) of package /linux/misc/file-5.35.tar.gz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "apptype.c" see the
Fossies "Dox" file reference documentation and the latest
Fossies "Diffs" side-by-side code changes report:
5.34_vs_5.35.
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 #ifndef lint
30 FILE_RCSID("@(#)$File: apptype.c,v 1.14 2018/09/09 20:33:28 christos Exp $")
31 #endif /* lint */
32
33 #include <stdlib.h>
34 #include <string.h>
35
36 #ifdef __EMX__
37 #include <io.h>
38 #define INCL_DOSSESMGR
39 #define INCL_DOSERRORS
40 #define INCL_DOSFILEMGR
41 #include <os2.h>
42 typedef ULONG APPTYPE;
43
44 protected int
45 file_os2_apptype(struct magic_set *ms, const char *fn, const void *buf,
46 size_t nb)
47 {
48 APPTYPE rc, type;
49 char path[_MAX_PATH], drive[_MAX_DRIVE], dir[_MAX_DIR],
50 fname[_MAX_FNAME], ext[_MAX_EXT];
51 char *filename;
52 FILE *fp;
53
54 if (fn)
55 filename = strdup(fn);
56 else if ((filename = tempnam("./", "tmp")) == NULL) {
57 file_error(ms, errno, "cannot create tempnam");
58 return -1;
59 }
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,
64 fname,
65 (*ext == '\0') ? "." : ext);
66
67 if (fn == NULL) {
68 if ((fp = fopen(path, "wb")) == NULL) {
69 file_error(ms, errno, "cannot open tmp file `%s'", path);
70 return -1;
71 }
72 if (fwrite(buf, 1, nb, fp) != nb) {
73 file_error(ms, errno, "cannot write tmp file `%s'",
74 path);
75 (void)fclose(fp);
76 return -1;
77 }
78 (void)fclose(fp);
79 }
80 rc = DosQueryAppType((unsigned char *)path, &type);
81
82 if (fn == NULL) {
83 unlink(path);
84 free(filename);
85 }
86 #if 0
87 if (rc == ERROR_INVALID_EXE_SIGNATURE)
88 printf("%s: not an executable file\n", fname);
89 else if (rc == ERROR_FILE_NOT_FOUND)
90 printf("%s: not found\n", fname);
91 else if (rc == ERROR_ACCESS_DENIED)
92 printf("%s: access denied\n", fname);
93 else if (rc != 0)
94 printf("%s: error code = %lu\n", fname, rc);
95 else
96 #else
97
98 /*
99 * for our purpose here it's sufficient to just ignore the error and
100 * return w/o success (=0)
101 */
102
103 if (rc)
104 return (0);
105
106 #endif
107
108 if (type & FAPPTYP_32BIT)
109 if (file_printf(ms, "32-bit ") == -1)
110 return -1;
111 if (type & FAPPTYP_PHYSDRV) {
112 if (file_printf(ms, "physical device driver") == -1)
113 return -1;
114 } else if (type & FAPPTYP_VIRTDRV) {
115 if (file_printf(ms, "virtual device driver") == -1)
116 return -1;
117 } else if (type & FAPPTYP_DLL) {
118 if (type & FAPPTYP_PROTDLL)
119 if (file_printf(ms, "protected ") == -1)
120 return -1;
121 if (file_printf(ms, "DLL") == -1)
122 return -1;
123 } else if (type & (FAPPTYP_WINDOWSREAL | FAPPTYP_WINDOWSPROT)) {
124 if (file_printf(ms, "Windows executable") == -1)
125 return -1;
126 } else if (type & FAPPTYP_DOS) {
127 /*
128 * The API routine is partially broken on filenames ending
129 * ".com".
130 */
131 if (stricmp(ext, ".com") == 0)
132 if (strncmp((const char *)buf, "MZ", 2))
133 return (0);
134 if (file_printf(ms, "DOS executable") == -1)
135 return -1;
136 /* ---------------------------------------- */
137 /* Might learn more from the magic(4) entry */
138 if (file_printf(ms, ", magic(4)-> ") == -1)
139 return -1;
140 return (0);
141 /* ---------------------------------------- */
142 } else if (type & FAPPTYP_BOUND) {
143 if (file_printf(ms, "bound executable") == -1)
144 return -1;
145 } else if ((type & 7) == FAPPTYP_WINDOWAPI) {
146 if (file_printf(ms, "PM executable") == -1)
147 return -1;
148 } else if (file_printf(ms, "OS/2 executable") == -1)
149 return -1;
150
151 switch (type & (FAPPTYP_NOTWINDOWCOMPAT |
152 FAPPTYP_WINDOWCOMPAT |
153 FAPPTYP_WINDOWAPI)) {
154 case FAPPTYP_NOTWINDOWCOMPAT:
155 if (file_printf(ms, " [NOTWINDOWCOMPAT]") == -1)
156 return -1;
157 break;
158 case FAPPTYP_WINDOWCOMPAT:
159 if (file_printf(ms, " [WINDOWCOMPAT]") == -1)
160 return -1;
161 break;
162 case FAPPTYP_WINDOWAPI:
163 if (file_printf(ms, " [WINDOWAPI]") == -1)
164 return -1;
165 break;
166 }
167 return 1;
168 }
169 #endif