winhttp: Use ws.ifelse.io for websocket tests.
[wine.git] / loader / main.c
blob86c2b28263ecd055f98a416c932866b09352fa99
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 #ifdef __i386__
116 #define SO_DIR "i386-unix/"
117 #elif defined(__x86_64__)
118 #define SO_DIR "x86_64-unix/"
119 #elif defined(__arm__)
120 #define SO_DIR "arm-unix/"
121 #elif defined(__aarch64__)
122 #define SO_DIR "aarch64-unix/"
123 #else
124 #define SO_DIR ""
125 #endif
126 const char *self = get_self_exe( argv0 );
127 char *path, *p;
128 void *handle = NULL;
130 if (self && ((path = realpath_dirname( self ))))
132 if ((p = remove_tail( path, "/loader" )))
134 handle = try_dlopen( p, "dlls/ntdll/ntdll.so" );
135 free( p );
137 else handle = try_dlopen( path, BIN_TO_DLLDIR "/" SO_DIR "ntdll.so" );
138 free( path );
141 if (!handle && (path = getenv( "WINEDLLPATH" )))
143 path = strdup( path );
144 for (p = strtok( path, ":" ); p; p = strtok( NULL, ":" ))
146 handle = try_dlopen( p, SO_DIR "ntdll.so" );
147 if (!handle) handle = try_dlopen( p, "ntdll.so" );
148 if (handle) break;
150 free( path );
153 if (!handle && !self) handle = try_dlopen( DLLDIR, SO_DIR "ntdll.so" );
155 return handle;
159 /**********************************************************************
160 * main
162 int main( int argc, char *argv[] )
164 void *handle;
166 if ((handle = load_ntdll( argv[0] )))
168 void (*init_func)(int, char **, char **) = dlsym( handle, "__wine_main" );
169 if (init_func) init_func( argc, argv, environ );
170 fprintf( stderr, "wine: __wine_main function not found in ntdll.so\n" );
171 exit(1);
174 fprintf( stderr, "wine: could not load ntdll.so: %s\n", dlerror() );
175 pthread_detach( pthread_self() ); /* force importing libpthread for OpenGL */
176 exit(1);