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
22 #include "wine/port.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
);
42 p
= strrchr( fullpath
, '/' );
43 if (p
== fullpath
) p
++;
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
);
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;
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
);
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";
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
))
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
);
113 static void *load_ntdll( char *argv0
)
115 const char *self
= get_self_exe( argv0
);
119 if (self
&& ((path
= realpath_dirname( self
))))
121 if ((p
= remove_tail( path
, "/loader" )))
123 handle
= try_dlopen( p
, "dlls/ntdll/ntdll.so" );
126 else handle
= try_dlopen( path
, BIN_TO_DLLDIR
"/ntdll.so" );
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" );
141 if (!handle
&& !self
) handle
= try_dlopen( DLLDIR
, "ntdll.so" );
147 /**********************************************************************
150 int main( int argc
, char *argv
[] )
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" );
162 fprintf( stderr
, "wine: could not load ntdll.so: %s\n", dlerror() );
163 pthread_detach( pthread_self() ); /* force importing libpthread for OpenGL */