Release 950430
[wine/multimedia.git] / loader / selector.c
blobf54605af0e3af98ecefe51c4ffeef5222c38ab5c
1 /*
2 * Selector manipulation functions
4 * Copyright 1993 Robert J. Amstadt
5 * Copyright 1995 Alexandre Julliard
6 */
8 #include <stdio.h>
9 #include <stdlib.h>
10 #include <string.h>
11 #include <sys/types.h>
12 #include <sys/stat.h>
13 #include <fcntl.h>
14 #include <unistd.h>
16 #ifndef WINELIB
18 #ifdef __linux__
19 #include <sys/mman.h>
20 #include <linux/unistd.h>
21 #include <linux/head.h>
22 #include <linux/mman.h>
23 #include <linux/a.out.h>
24 #include <linux/ldt.h>
25 #endif
26 #if defined(__NetBSD__) || defined(__FreeBSD__)
27 #include <sys/mman.h>
28 #include <machine/segments.h>
29 #endif
31 #include "windows.h"
32 #include "ldt.h"
33 #include "wine.h"
34 #include "global.h"
35 #include "dlls.h"
36 #include "neexe.h"
37 #include "if1632.h"
38 #include "prototypes.h"
39 #include "module.h"
40 #include "stddebug.h"
41 /* #define DEBUG_SELECTORS */
42 #include "debug.h"
45 #define MAX_ENV_SIZE 16384 /* Max. environment size (ought to be dynamic) */
47 static HANDLE EnvironmentHandle = 0;
50 extern char WindowsPath[256];
52 extern char **Argv;
53 extern int Argc;
54 extern char **environ;
57 /**********************************************************************
58 * Check whether pseudo-functions like __0040H for direct memory
59 * access are referenced and return 1 if so.
60 * FIXME: Reading and writing to the returned selectors has no effect
61 * (e.g. reading from the Bios data segment (esp. clock!) )
64 unsigned int GetMemoryReference( char *dll_name, char *function,
65 WORD *sel, WORD *offset )
67 static HANDLE memory_handles[ 10 ] = { 0,0,0,0,0,0,0,0,0,0 };
68 static char *memory_names[ 10 ] = { "segment 0xA000",
69 "segment 0xB000",
70 "segment 0xB800",
71 "Bios-Rom",
72 "segment 0xD000",
73 "segment 0x0000",
74 "segment 0xE000",
75 "segment 0xF000",
76 "segment 0xC000",
77 "Bios data segment" };
78 short nr;
80 if( strcasecmp( dll_name, "KERNEL" ) )
81 return 0;
83 if( HIWORD( function ) ) {
84 if( ( *function != '_' ) || ( *(function+1) != '_' ) )
85 return 0;
86 if( !strcasecmp( function, "__A000H" ) ) nr = 0;
87 else if( !strcasecmp( function, "__B000H" ) ) nr = 1;
88 else if( !strcasecmp( function, "__B800H" ) ) nr = 2;
89 else if( !strcasecmp( function, "__ROMBIOS" ) ) nr = 3;
90 else if( !strcasecmp( function, "__D000H" ) ) nr = 4;
91 else if( !strcasecmp( function, "__0000H" ) ) nr = 5;
92 else if( !strcasecmp( function, "__E000H" ) ) nr = 6;
93 else if( !strcasecmp( function, "__F000H" ) ) nr = 7;
94 else if( !strcasecmp( function, "__C000H" ) ) nr = 8;
95 else if( !strcasecmp( function, "__0040H" ) ) nr = 9;
96 else
97 return 0;
99 else {
100 switch( LOWORD( function ) ) {
101 case 174: nr = 0; break;
102 case 181: nr = 1; break;
103 case 182: nr = 2; break;
104 case 173: nr = 3; break;
105 case 179: nr = 4; break;
106 case 183: nr = 5; break;
107 case 190: nr = 6; break;
108 case 194: nr = 7; break;
109 case 195: nr = 8; break;
110 case 193: nr = 9; break;
111 default: return 0;
115 if( !memory_handles[ nr ] ) {
116 fprintf( stderr, "Warning: Direct access to %s!\n", memory_names[ nr ] );
117 memory_handles[ nr ] = GlobalAlloc( GMEM_FIXED, 65535 );
119 *sel = *offset = memory_handles[ nr ];
120 return 1;
125 unsigned int GetEntryDLLName( char * dll_name, char * function,
126 WORD* sel, WORD *offset )
128 HMODULE hModule;
129 struct dll_table_s *dll_table;
130 int ordinal, addr;
132 if( GetMemoryReference( dll_name, function, sel, offset ) )
133 return 0;
135 hModule = GetModuleHandle( dll_name );
136 ordinal = MODULE_GetOrdinal( hModule, function );
137 if (!ordinal) return 1;
138 addr = MODULE_GetEntryPoint( hModule, ordinal );
139 if (!addr) return 1;
140 #ifdef WINESTAT
141 if ((dll_table = FindDLLTable(dll_name)) != NULL)
143 dll_table->dll_table[ordinal].used++;
145 #endif
146 *offset = LOWORD(addr);
147 *sel = HIWORD(addr);
148 return 0;
152 unsigned int GetEntryDLLOrdinal( char * dll_name, int ordinal,
153 WORD *sel, WORD *offset )
155 HMODULE hModule;
156 struct dll_table_s *dll_table;
157 int addr;
159 if( GetMemoryReference( dll_name, (char*)ordinal, sel, offset ) )
160 return 0;
162 hModule = GetModuleHandle( dll_name );
163 addr = MODULE_GetEntryPoint( hModule, ordinal );
164 if (!addr) return 1;
165 #ifdef WINESTAT
166 if ((dll_table = FindDLLTable(dll_name)) != NULL)
167 dll_table->dll_table[ordinal].used++;
168 #endif
169 *offset = LOWORD(addr);
170 *sel = HIWORD(addr);
171 return 0;
175 WNDPROC GetWndProcEntry16( char *name )
177 WORD sel, offset;
179 GetEntryDLLName( "WINPROCS", name, &sel, &offset );
180 return (WNDPROC) MAKELONG( offset, sel );
184 /***********************************************************************
185 * GetDOSEnvironment (KERNEL.131)
187 SEGPTR GetDOSEnvironment(void)
189 return WIN16_GlobalLock( EnvironmentHandle );
193 /**********************************************************************
194 * CreateEnvironment
196 static HANDLE CreateEnvironment(void)
198 HANDLE handle;
199 char **e;
200 char *p;
202 handle = GlobalAlloc( GMEM_MOVEABLE, MAX_ENV_SIZE );
203 if (!handle) return 0;
204 p = (char *) GlobalLock( handle );
207 * Fill environment with Windows path, the Unix environment,
208 * and program name.
210 strcpy(p, "PATH=");
211 strcat(p, WindowsPath);
212 p += strlen(p) + 1;
214 for (e = environ; *e; e++)
216 if (strncasecmp(*e, "path", 4))
218 strcpy(p, *e);
219 p += strlen(p) + 1;
223 *p++ = '\0';
226 * Display environment
228 p = (char *) GlobalLock( handle );
229 dprintf_selectors(stddeb, "Environment at %p\n", p);
230 for (; *p; p += strlen(p) + 1) dprintf_selectors(stddeb, " %s\n", p);
232 return handle;
237 /**********************************************************************
238 * CreateSelectors
240 void CreateSelectors(void)
242 if(!EnvironmentHandle) EnvironmentHandle = CreateEnvironment();
246 #endif /* ifndef WINELIB */