- Start rosapps rearrange and cleanup process.
[reactos.git] / rosapps / applications / mc / pc / util_win32.c
1 /* Utilities - Win32 utilities (Windows NT and Windows '95)
2 Copyright (C) 1994, 1995, 1996 the Free Software Foundation.
3
4 Written 1996 by Juan Grigera<grigera@isis.unlp.edu.ar>
5
6 This program is free software; you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation; either version 2 of the License, or
9 (at your option) any later version.
10
11 This program is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 GNU General Public License for more details.
15
16 You should have received a copy of the GNU General Public License
17 along with this program; if not, write to the Free Software
18 Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
19 */
20
21 #include <config.h>
22 #include <windows.h>
23 #include "util_win32.h"
24 #include "trace_nt.h"
25
26 /* int win32_GetPlatform ()
27 Checks in which OS Midnight Commander is running.
28
29 Returns:
30 OS_WinNT - Windows NT 3.x
31 OS_Win95 - Windows 4.x
32
33 Note: GetVersionEx (Win32API) is called only once.
34 */
35 int win32_GetPlatform ()
36 {
37 static int platform = 0;
38
39 return (platform ? platform : (platform = win32_GetVersionEx()) );
40 }
41
42 /* int win32_GetVersionEx ()
43 intended for use by win32_GetPlatform only
44 */
45 int win32_GetVersionEx ()
46 {
47 OSVERSIONINFO ovi;
48
49 ovi.dwOSVersionInfoSize = sizeof(ovi);
50 win32APICALL( GetVersionEx(&ovi) );
51
52 return ovi.dwPlatformId;
53 }
54
55 /* int win32_GetEXEType (const char* filename)
56 Determines whether filename (an Executable) is
57 a Console application (CUI) or a Graphical application(GUI).
58
59 filename - Name of executable file to check
60
61 Returns: EXE_win16 - Windows 3.x archive or OS/2
62 EXE_win32CUI - NT or Chicago Console API, also OS/2
63 EXE_win32GUI - NT or Chicago GUI API
64 EXE_otherCUI - DOS COM, MZ, ZM, Phar Lap
65 EXE_Unknown - Unknown
66 EXE_Error - Couldn't read file/EXE image
67
68 TODO: better management of OS/2 images
69 EXE_CompressedArchive can be easily implemented
70 Notes: This function parses the executable header (the only ugly way
71 to do it). If header is not found or not understood,
72 0 is returned.
73
74 Information on NE, LE, LX and MZ taken from Ralf Brown's interrupt
75 list, under INT 21-function 4B ("EXEC" - LOAD AND/OR EXECUTE PROGRAM),
76 Tables 0806 - 836.
77
78 Parsing of PE header (Win32 signature, "Portable Executable")
79 taken from MSKBase article Number: Q90493.
80 */
81
82 /* ---- Executable Signatures ---- */
83
84 /* Alternative DOS signagure */
85 #define IMAGE_DOS_SIGNATURE_ALTERNATIVE 0x4D5A /* ZM */
86
87 /* Phar Lap .EXP files */
88 #define IMAGE_OLDPHARLAP_SIGNATURE 0x504D /* MP */
89 #define IMAGE_NEWPHARLAP_286_SIGNATURE 0x3250 /* P2 */
90 #define IMAGE_NEWPHARLAP_386_SIGNATURE 0x3350 /* P3 */
91
92 /* New Executables */
93 #define IMAGE_LX_SIGNATURE 0x584C /* LX */
94 #define IMAGE_PE_SIGNATURE 0x4550 /* PE */
95
96
97 int win32_GetEXEType (const char* a_szFileName)
98 {
99 /* FIXME: MinGW cannot compile this code */
100 #ifndef __MINGW32__
101 HANDLE hImage;
102 DWORD dwDumm;
103 DWORD SectionOffset;
104 DWORD CoffHeaderOffset;
105 WORD wSignature;
106 /* DWORD MoreDosHeader[16]; */
107
108 IMAGE_DOS_HEADER image_dos_header;
109 IMAGE_FILE_HEADER image_file_header;
110 IMAGE_OPTIONAL_HEADER image_optional_header;
111 /* IMAGE_SECTION_HEADER image_section_header; */
112
113 /* Open the EXE file - Use Native API for SHARE compatibility */
114 hImage = CreateFile(a_szFileName, GENERIC_READ, FILE_SHARE_READ, NULL,
115 OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL);
116 if (hImage == INVALID_HANDLE_VALUE) {
117 win32Trace (("win32_GetEXEType: Could not open file %s. API Error %d.", a_szFileName, GetLastError()));
118 return EXE_Error;
119 }
120
121 /* Read the MZ (DOS) image header. */
122 win32APICALL( ReadFile (hImage, (LPVOID)&image_dos_header, sizeof(IMAGE_DOS_HEADER), &dwDumm, NULL) );
123
124 switch (image_dos_header.e_magic) {
125 case IMAGE_DOS_SIGNATURE: /* MZ or ZM */
126 case IMAGE_DOS_SIGNATURE_ALTERNATIVE:
127 break;
128
129 case IMAGE_OLDPHARLAP_SIGNATURE: /* MP, P2, P3: Phar Lap executables */
130 case IMAGE_NEWPHARLAP_286_SIGNATURE:
131 case IMAGE_NEWPHARLAP_386_SIGNATURE:
132 return EXE_otherCUI;
133
134 default:
135 return EXE_otherCUI; /* Probably .COM? */
136 }
137
138 /* Read more MS-DOS header. */
139 /* win32APICALL( ReadFile (hImage, MoreDosHeader, sizeof(MoreDosHeader)); */
140
141 /* Get new executable header */
142 CoffHeaderOffset = SetFilePointer(hImage, image_dos_header.e_lfanew, NULL, FILE_BEGIN);
143 /* + sizeof(ULONG); */
144 win32APICALL( ReadFile (hImage, (LPVOID) &wSignature, sizeof(WORD), &dwDumm, NULL) );
145
146 switch (wSignature) {
147 case IMAGE_PE_SIGNATURE: /* PE - Portable Executable */
148 break;
149 case IMAGE_OS2_SIGNATURE: /* NE - New Executable OS/2 and Windows 3.x */
150 case IMAGE_OS2_SIGNATURE_LE: /* LE - Linear Execuable (Windows 3.x) */
151 case IMAGE_LX_SIGNATURE: /* LX - Linear Execuable (OS/2) */
152 return EXE_win16;
153 default:
154 return EXE_Unknown; /* unknown New Executable or bad pointer */
155
156 }
157
158 /* Continue parsing PE (COFF-like) */
159 SectionOffset = CoffHeaderOffset + IMAGE_SIZEOF_FILE_HEADER + IMAGE_SIZEOF_NT_OPTIONAL_HEADER;
160
161 win32APICALL( ReadFile(hImage, (LPVOID) &image_file_header, IMAGE_SIZEOF_FILE_HEADER, &dwDumm, NULL) );
162
163 /* Read optional header. */
164 win32APICALL( ReadFile(hImage, (LPVOID) &image_optional_header, IMAGE_SIZEOF_NT_OPTIONAL_HEADER, &dwDumm, NULL) );
165
166 switch (image_optional_header.Subsystem) {
167 case IMAGE_SUBSYSTEM_WINDOWS_GUI:
168 return EXE_win32GUI;
169
170 case IMAGE_SUBSYSTEM_WINDOWS_CUI:
171 case IMAGE_SUBSYSTEM_OS2_CUI:
172 case IMAGE_SUBSYSTEM_POSIX_CUI:
173 return EXE_win32CUI;
174
175 case IMAGE_SUBSYSTEM_UNKNOWN:
176 case IMAGE_SUBSYSTEM_NATIVE:
177 return EXE_Unknown; /* FIXME: what is "NATIVE??" */
178 default:
179 win32Trace(("Unknown type %u.\n", image_optional_header.Subsystem));
180 return EXE_Unknown;
181 }
182 #else
183 return EXE_Unknown;
184 #endif /* !__MINGW32__ */
185 }
186
187