ntdll: Call FLS callbacks.
[wine/zf.git] / loader / main.c
blobfb40e90f977dbcad533b698b34bacd0d385b852b
1 /*
2 * Emulator initialisation code
4 * Copyright 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library 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 GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
21 #include "config.h"
22 #include "wine/port.h"
24 #include <pthread.h>
25 #include <stdio.h>
26 #include <stdlib.h>
28 #include "main.h"
30 extern char **environ;
32 /* the preloader will set this variable */
33 const struct wine_preload_info *wine_main_preload_info = NULL;
35 /* canonicalize path and return its directory name */
36 static char *realpath_dirname( const char *name )
38 char *p, *fullpath = realpath( name, NULL );
40 if (fullpath)
42 p = strrchr( fullpath, '/' );
43 if (p == fullpath) p++;
44 if (p) *p = 0;
46 return fullpath;
49 /* if string ends with tail, remove it */
50 static char *remove_tail( const char *str, const char *tail )
52 size_t len = strlen( str );
53 size_t tail_len = strlen( tail );
54 char *ret;
56 if (len < tail_len) return NULL;
57 if (strcmp( str + len - tail_len, tail )) return NULL;
58 ret = malloc( len - tail_len + 1 );
59 memcpy( ret, str, len - tail_len );
60 ret[len - tail_len] = 0;
61 return ret;
64 /* build a path from the specified dir and name */
65 static char *build_path( const char *dir, const char *name )
67 size_t len = strlen( dir );
68 char *ret = malloc( len + strlen( name ) + 2 );
70 memcpy( ret, dir, len );
71 if (len && ret[len - 1] != '/') ret[len++] = '/';
72 strcpy( ret + len, name );
73 return ret;
76 static const char *get_self_exe( char *argv0 )
78 #if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__)
79 return "/proc/self/exe";
80 #elif defined (__FreeBSD__) || defined(__DragonFly__)
81 return "/proc/curproc/file";
82 #else
83 if (!strchr( argv0, '/' )) /* search in PATH */
85 char *p, *path = getenv( "PATH" );
87 if (!path || !(path = strdup(path))) return NULL;
88 for (p = strtok( path, ":" ); p; p = strtok( NULL, ":" ))
90 char *name = build_path( p, argv0 );
91 if (!access( name, X_OK ))
93 free( path );
94 return name;
96 free( name );
98 free( path );
99 return NULL;
101 return argv0;
102 #endif
105 static void *try_dlopen( const char *dir, const char *name )
107 char *path = build_path( dir, name );
108 void *handle = dlopen( path, RTLD_NOW );
109 free( path );
110 return handle;
113 static void *load_ntdll( char *argv0 )
115 const char *self = get_self_exe( argv0 );
116 char *path, *p;
117 void *handle = NULL;
119 if (self && ((path = realpath_dirname( self ))))
121 if ((p = remove_tail( path, "/loader" )))
123 handle = try_dlopen( p, "dlls/ntdll/ntdll.so" );
124 free( p );
126 else handle = try_dlopen( path, BIN_TO_DLLDIR "/ntdll.so" );
127 free( path );
130 if (!handle && (path = getenv( "WINEDLLPATH" )))
132 path = strdup( path );
133 for (p = strtok( path, ":" ); p; p = strtok( NULL, ":" ))
135 handle = try_dlopen( p, "ntdll.so" );
136 if (handle) break;
138 free( path );
141 if (!handle && !self) handle = try_dlopen( DLLDIR, "ntdll.so" );
143 return handle;
147 /**********************************************************************
148 * main
150 int main( int argc, char *argv[] )
152 void *handle;
154 if ((handle = load_ntdll( argv[0] )))
156 void (*init_func)(int, char **, char **) = dlsym( handle, "__wine_main" );
157 if (init_func) init_func( argc, argv, environ );
158 fprintf( stderr, "wine: __wine_main function not found in ntdll.so\n" );
159 exit(1);
162 fprintf( stderr, "wine: could not load ntdll.so: %s\n", dlerror() );
163 pthread_detach( pthread_self() ); /* force importing libpthread for OpenGL */
164 exit(1);