1 static char RCSId
[] = "$Id: wine.c,v 1.2 1993/07/04 04:04:21 root Exp root $";
2 static char Copyright
[] = "Copyright Robert J. Amstadt, 1993";
13 #include <linux/unistd.h>
14 #include <linux/head.h>
15 #include <linux/ldt.h>
16 #include <linux/segment.h>
20 #include "prototypes.h"
28 /* #define DEBUG_FIXUP */
30 extern HANDLE
CreateNewTask(HINSTANCE hInst
);
31 extern int CallToInit16(unsigned long csip
, unsigned long sssp
,
33 extern void CallTo32();
35 char *GetDosFileName(char *unixfilename
);
36 char *GetModuleName(struct w_files
* wpnt
, int index
, char *buffer
);
37 extern unsigned char ran_out
;
38 extern char WindowsPath
[256];
39 char *WIN_ProgramName
;
41 unsigned short WIN_StackSize
;
42 unsigned short WIN_HeapSize
;
44 struct w_files
* wine_files
= NULL
;
50 static char *DLL_Extensions
[] = { "dll", NULL
};
51 static char *EXE_Extensions
[] = { "exe", NULL
};
53 /**********************************************************************
57 myerror(const char *s
)
62 fprintf(stderr
, "wine: %s\n", s
);
67 /**********************************************************************
68 * GetFilenameFromInstance
71 GetFilenameFromInstance(unsigned short instance
)
73 register struct w_files
*w
= wine_files
;
75 while (w
&& w
->hinstance
!= instance
)
85 GetFileInfo(unsigned short instance
)
87 register struct w_files
*w
= wine_files
;
89 while (w
&& w
->hinstance
!= instance
)
96 /**********************************************************************
100 void load_mz_header(int fd
, struct mz_header_s
*mz_header
)
102 if (read(fd
, mz_header
, sizeof(struct mz_header_s
)) !=
103 sizeof(struct mz_header_s
))
105 myerror("Unable to read MZ header from file");
110 int IsDLLLoaded(char *name
)
112 struct w_files
*wpnt
;
114 if(FindDLLTable(name
))
117 for(wpnt
= wine_files
; wpnt
; wpnt
= wpnt
->next
)
118 if(strcmp(wpnt
->name
, name
) == 0)
124 /**********************************************************************
126 * Load one executable into memory
128 HINSTANCE
LoadImage(char *module
, int filetype
, int change_dir
)
130 unsigned int read_size
;
132 struct w_files
* wpnt
, *wpnt1
;
134 char buffer
[256], header
[2], modulename
[64], *fullname
;
136 ExtractDLLName(module
, modulename
);
137 printf("%sLoadImage \n", module
);
139 if (FindDLLTable(modulename
)) {
140 return GetModuleHandle(modulename
);
143 /* already loaded ? */
144 for (wpnt
= wine_files
; wpnt
; wpnt
= wpnt
->next
)
145 if (strcasecmp(wpnt
->name
, modulename
) == 0)
146 return wpnt
->hinstance
;
151 fullname
= FindFile(buffer
, sizeof(buffer
), module
,
152 (filetype
== EXE
? EXE_Extensions
: DLL_Extensions
),
154 if (fullname
== NULL
)
156 fprintf(stderr
, "LoadImage: I can't find %s.dll | %s.exe !\n",
161 fullname
= GetDosFileName(fullname
);
162 WIN_ProgramName
= strdup(fullname
);
164 fprintf(stderr
,"LoadImage: loading %s (%s)\n [%s]\n",
165 module
, buffer
, WIN_ProgramName
);
167 if (change_dir
&& fullname
)
172 strcpy(dirname
, fullname
);
173 p
= strrchr(dirname
, '\\');
176 DOS_SetDefaultDrive(dirname
[0] - 'A');
177 DOS_ChangeDir(dirname
[0] - 'A', dirname
+ 2);
180 /* First allocate a spot to store the info we collect, and add it to
184 wpnt
= (struct w_files
*) malloc(sizeof(struct w_files
));
185 if(wine_files
== NULL
)
189 while(wpnt1
->next
) wpnt1
= wpnt1
->next
;
193 wpnt
->resnamtab
= (RESNAMTAB
*) -1;
196 * Open file for reading.
198 wpnt
->fd
= open(buffer
, O_RDONLY
);
203 * Establish header pointers.
205 wpnt
->filename
= strdup(buffer
);
206 wpnt
->name
= strdup(modulename
);
209 wpnt->name = strdup(module);
214 wpnt
->mz_header
= (struct mz_header_s
*) malloc(sizeof(struct mz_header_s
));;
215 status
= lseek(wpnt
->fd
, 0, SEEK_SET
);
216 load_mz_header (wpnt
->fd
, wpnt
->mz_header
);
217 if (wpnt
->mz_header
->must_be_0x40
!= 0x40 &&
218 wpnt
->mz_header
->must_be_0x40
!= 0x1e)
219 myerror("This is not a Windows program");
221 /* read first two bytes to determine filetype */
222 status
= lseek(wpnt
->fd
, wpnt
->mz_header
->ne_offset
, SEEK_SET
);
223 read(wpnt
->fd
, &header
, sizeof(header
));
225 if (header
[0] == 'N' && header
[1] == 'E')
226 return (LoadNEImage(wpnt
));
228 if (header
[0] == 'P' && header
[1] == 'E') {
229 printf("win32 applications are not supported");
233 fprintf(stderr
, "wine: (%s) unknown fileformat !\n", wpnt
->filename
);
239 /**********************************************************************
242 int _WinMain(int argc
, char **argv
)
253 struct w_files
* wpnt
;
254 int cs_reg
, ds_reg
, ss_reg
, ip_reg
, sp_reg
;
260 if (strchr(Argv
[0], '\\') || strchr(Argv
[0],'/')) {
261 for (p
= Argv
[0] + strlen(Argv
[0]); *p
!= '\\' && *p
!='/'; p
--)
264 strncpy(filename
, Argv
[0], p
- Argv
[0]);
265 filename
[p
- Argv
[0]] = '\0';
266 strcat(WindowsPath
, ";");
267 if (strchr(filename
, '/'))
268 strcat(WindowsPath
, GetDosFileName(filename
));
270 strcat(WindowsPath
, filename
);
273 if ((hInstMain
= LoadImage(Argv
[0], EXE
, 1)) < 32) {
274 fprintf(stderr
, "wine: can't load %s!.\n", Argv
[0]);
277 hTaskMain
= CreateNewTask(hInstMain
);
278 printf("_WinMain // hTaskMain=%04X hInstMain=%04X !\n", hTaskMain
, hInstMain
);
280 GetPrivateProfileString("wine", "SystemResources", "sysres.dll",
281 filename
, sizeof(filename
), WINE_INI
);
283 hSysRes
= LoadImage(filename
, DLL
, 0);
285 fprintf(stderr
, "wine: can't load %s!.\n", filename
);
288 printf("System Resources Loaded // hSysRes='%04X'\n", hSysRes
);
293 /* wpnt = wine_files;
294 for(wpnt = wine_files; wpnt; wpnt = wpnt->next)
295 for (segment = 0; segment < wpnt->ne_header->n_segment_tab; segment++)
296 if (FixupSegment(wpnt, segment) < 0)
297 myerror("fixup failed.");
301 cp
= strrchr(argv
[0], '/');
302 if(!cp
) cp
= argv
[0];
304 if(strcmp(cp
,"winestat") == 0) {
311 * Initialize signal handling.
316 * Fixup stack and jump to start.
318 WIN_StackSize
= wine_files
->ne_header
->stack_length
;
319 WIN_HeapSize
= wine_files
->ne_header
->local_heap_length
;
321 ds_reg
= (wine_files
->
322 selector_table
[wine_files
->ne_header
->auto_data_seg
-1].selector
);
323 cs_reg
= wine_files
->selector_table
[wine_files
->ne_header
->cs
-1].selector
;
324 ip_reg
= wine_files
->ne_header
->ip
;
325 ss_reg
= wine_files
->selector_table
[wine_files
->ne_header
->ss
-1].selector
;
326 sp_reg
= wine_files
->ne_header
->sp
;
328 if (Options
.debug
) wine_debug(0, NULL
);
330 rv
= CallToInit16(cs_reg
<< 16 | ip_reg
, ss_reg
<< 16 | sp_reg
, ds_reg
);
331 printf ("rv = %x\n", rv
);
334 void InitDLL(struct w_files
*wpnt
)
336 int cs_reg
, ds_reg
, ip_reg
, rv
;
340 if (wpnt
->ne_header
->format_flags
& 0x8000)
342 if (!(wpnt
->ne_header
->format_flags
& 0x0001))
345 fprintf(stderr
, "Library is not marked SINGLEDATA\n");
349 ds_reg
= wpnt
->selector_table
[wpnt
->
350 ne_header
->auto_data_seg
-1].selector
;
351 cs_reg
= wpnt
->selector_table
[wpnt
->ne_header
->cs
-1].selector
;
352 ip_reg
= wpnt
->ne_header
->ip
;
355 fprintf(stderr
, "Initializing %s, cs:ip %04x:%04x, ds %04x\n",
356 wpnt
->name
, cs_reg
, ip_reg
, ds_reg
);
358 rv
= CallTo16(cs_reg
<< 16 | ip_reg
, ds_reg
);
359 printf ("rv = %x\n", rv
);
361 printf("%s skipped\n", wpnt
->name
);
365 void InitializeLoadedDLLs(struct w_files
*wpnt
)
367 static flagReadyToRun
= 0;
368 struct w_files
*final_wpnt
;
370 printf("InitializeLoadedDLLs %08X\n", wpnt
);
375 fprintf(stderr
, "Initializing DLLs\n");
383 fprintf(stderr
, "Initializing %s\n", wpnt
->name
);
387 * Initialize libraries
396 final_wpnt
= wpnt
->next
;
399 for( ; wpnt
!= final_wpnt
; wpnt
= wpnt
->next
)
402 #else /* #ifndef WINELIB */
403 void InitDLL(struct w_files
*wpnt
)
407 #endif /* #ifndef WINELIB */