31b8095e13bae8ead23f1ad5c304ea5d0df9a5a4
[dragonfly.git] / contrib / file / src / apptype.c
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.11 2009/02/04 18:24:32 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                         return -1;
76                 }
77                 (void)fclose(fp);
78         }
79         rc = DosQueryAppType((unsigned char *)path, &type);
80
81         if (fn == NULL) {
82                 unlink(path);
83                 free(filename);
84         }
85 #if 0
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);
92         else if (rc != 0)
93                 printf("%s: error code = %lu\n", fname, rc);
94         else
95 #else
96
97         /*
98          * for our purpose here it's sufficient to just ignore the error and
99          * return w/o success (=0)
100          */
101
102         if (rc)
103                 return (0);
104
105 #endif
106
107         if (type & FAPPTYP_32BIT)
108                 if (file_printf(ms, "32-bit ") == -1)
109                         return -1;
110         if (type & FAPPTYP_PHYSDRV) {
111                 if (file_printf(ms, "physical device driver") == -1)
112                         return -1;
113         } else if (type & FAPPTYP_VIRTDRV) {
114                 if (file_printf(ms, "virtual device driver") == -1)
115                         return -1;
116         } else if (type & FAPPTYP_DLL) {
117                 if (type & FAPPTYP_PROTDLL)
118                         if (file_printf(ms, "protected ") == -1)
119                                 return -1;
120                 if (file_printf(ms, "DLL") == -1)
121                         return -1;
122         } else if (type & (FAPPTYP_WINDOWSREAL | FAPPTYP_WINDOWSPROT)) {
123                 if (file_printf(ms, "Windows executable") == -1)
124                         return -1;
125         } else if (type & FAPPTYP_DOS) {
126                 /*
127                  * The API routine is partially broken on filenames ending
128                  * ".com".
129                  */
130                 if (stricmp(ext, ".com") == 0)
131                         if (strncmp((const char *)buf, "MZ", 2))
132                                 return (0);
133                 if (file_printf(ms, "DOS executable") == -1)
134                         return -1;
135                 /* ---------------------------------------- */
136                 /* Might learn more from the magic(4) entry */
137                 if (file_printf(ms, ", magic(4)-> ") == -1)
138                         return -1;
139                 return (0);
140                 /* ---------------------------------------- */
141         } else if (type & FAPPTYP_BOUND) {
142                 if (file_printf(ms, "bound executable") == -1)
143                         return -1;
144         } else if ((type & 7) == FAPPTYP_WINDOWAPI) {
145                 if (file_printf(ms, "PM executable") == -1)
146                         return -1;
147         } else if (file_printf(ms, "OS/2 executable") == -1)
148                 return -1;
149
150         switch (type & (FAPPTYP_NOTWINDOWCOMPAT |
151                         FAPPTYP_WINDOWCOMPAT |
152                         FAPPTYP_WINDOWAPI)) {
153         case FAPPTYP_NOTWINDOWCOMPAT:
154                 if (file_printf(ms, " [NOTWINDOWCOMPAT]") == -1)
155                         return -1;
156                 break;
157         case FAPPTYP_WINDOWCOMPAT:
158                 if (file_printf(ms, " [WINDOWCOMPAT]") == -1)
159                         return -1;
160                 break;
161         case FAPPTYP_WINDOWAPI:
162                 if (file_printf(ms, " [WINDOWAPI]") == -1)
163                         return -1;
164                 break;
165         }
166         return 1;
167 }
168 #endif