Module: wine Branch: master Commit: ecb651c01fa7d6b495b82ed516d5e86042f65121 URL: https://gitlab.winehq.org/wine/wine/-/commit/ecb651c01fa7d6b495b82ed516d5e86...
Author: Alexandre Julliard julliard@winehq.org Date: Wed Jan 25 11:14:36 2023 +0100
tools: Add a common helper to get the argv0 directory.
---
tools/tools.h | 26 ++++++++++++++++++++++++++ tools/widl/widl.c | 25 +++++-------------------- tools/winegcc/winegcc.c | 20 +------------------- tools/wmc/wmc.c | 22 +++------------------- tools/wrc/wrc.c | 22 +++------------------- 5 files changed, 38 insertions(+), 77 deletions(-)
diff --git a/tools/tools.h b/tools/tools.h index 456633e4277..4a886999e96 100644 --- a/tools/tools.h +++ b/tools/tools.h @@ -30,6 +30,9 @@ #include <fcntl.h> #include <time.h> #include <errno.h> +#ifdef HAVE_SYS_SYSCTL_H +# include <sys/sysctl.h> +#endif
#ifdef _WIN32 # include <direct.h> @@ -603,6 +606,29 @@ static inline struct target init_argv0_target( const char *argv0 ) }
+static inline char *get_argv0_dir( const char *argv0 ) +{ +#ifndef _WIN32 + char *dir = NULL; + +#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) + dir = realpath( "/proc/self/exe", NULL ); +#elif defined (__FreeBSD__) || defined(__DragonFly__) + static int pathname[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; + size_t path_size = PATH_MAX; + char *path = xmalloc( path_size ); + if (!sysctl( pathname, ARRAY_SIZE(pathname), path, &path_size, NULL, 0 )) + dir = realpath( path, NULL ); + free( path ); +#endif + if (!dir && !(dir = realpath( argv0, NULL ))) return NULL; + return get_dirname( dir ); +#else + return get_dirname( argv0 ); +#endif +} + + /* output buffer management */
extern unsigned char *output_buffer; diff --git a/tools/widl/widl.c b/tools/widl/widl.c index 8d07bf61dba..58ad87b44f0 100644 --- a/tools/widl/widl.c +++ b/tools/widl/widl.c @@ -31,9 +31,6 @@ #include <signal.h> #include <limits.h> #include <sys/types.h> -#ifdef HAVE_SYS_SYSCTL_H -# include <sys/sysctl.h> -#endif
#include "widl.h" #include "utils.h" @@ -482,23 +479,11 @@ void write_id_data(const statement_list_t *stmts)
static void init_argv0_dir( const char *argv0 ) { -#ifndef _WIN32 - char *dir = NULL; - -#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) - dir = realpath( "/proc/self/exe", NULL ); -#elif defined (__FreeBSD__) || defined(__DragonFly__) - static int pathname[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; - size_t path_size = PATH_MAX; - char *path = xmalloc( path_size ); - if (!sysctl( pathname, ARRAY_SIZE(pathname), path, &path_size, NULL, 0 )) - dir = realpath( path, NULL ); - free( path ); -#endif - if (!dir && !(dir = realpath( argv0, NULL ))) return; - includedir = strmake( "%s/%s", get_dirname( dir ), BIN_TO_INCLUDEDIR ); - dlldir = strmake( "%s/%s", get_dirname( dir ), BIN_TO_DLLDIR ); -#endif + char *dir = get_argv0_dir( argv0 ); + + if (!dir) return; + includedir = strmake( "%s/%s", dir, BIN_TO_INCLUDEDIR ); + dlldir = strmake( "%s/%s", dir, BIN_TO_DLLDIR ); }
static void option_callback( int optc, char *optarg ) diff --git a/tools/winegcc/winegcc.c b/tools/winegcc/winegcc.c index 3891974c6c1..a0947a6fa7c 100644 --- a/tools/winegcc/winegcc.c +++ b/tools/winegcc/winegcc.c @@ -98,9 +98,6 @@ #include <ctype.h> #include <limits.h> #include <sys/types.h> -#ifdef HAVE_SYS_SYSCTL_H -# include <sys/sysctl.h> -#endif
#include "utils.h"
@@ -646,24 +643,9 @@ static char *get_lib_dir( struct options *opts )
static void init_argv0_dir( const char *argv0 ) { -#ifndef _WIN32 - char *dir = NULL; - -#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) - dir = realpath( "/proc/self/exe", NULL ); -#elif defined (__FreeBSD__) || defined(__DragonFly__) - static int pathname[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; - size_t path_size = PATH_MAX; - char *path = xmalloc( path_size ); - if (!sysctl( pathname, ARRAY_SIZE(pathname), path, &path_size, NULL, 0 )) - dir = realpath( path, NULL ); - free( path ); -#endif - if (!dir && !(dir = realpath( argv0, NULL ))) return; - bindir = get_dirname( dir ); + if (!(bindir = get_argv0_dir( argv0 ))) return; includedir = strmake( "%s/%s", bindir, BIN_TO_INCLUDEDIR ); libdir = strmake( "%s/%s", bindir, BIN_TO_LIBDIR ); -#endif }
static void compile(struct options* opts, const char* lang) diff --git a/tools/wmc/wmc.c b/tools/wmc/wmc.c index 76251637a4b..c2cf754f590 100644 --- a/tools/wmc/wmc.c +++ b/tools/wmc/wmc.c @@ -26,9 +26,6 @@ #include <signal.h> #include <limits.h> #include <sys/types.h> -#ifdef HAVE_SYS_SYSCTL_H -# include <sys/sysctl.h> -#endif
#include "wmc.h" #include "utils.h" @@ -149,24 +146,11 @@ static void exit_on_signal( int sig )
static void init_argv0_dir( const char *argv0 ) { -#ifndef _WIN32 - char *dir = NULL; - -#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) - dir = realpath( "/proc/self/exe", NULL ); -#elif defined (__FreeBSD__) || defined(__DragonFly__) - static int pathname[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; - size_t path_size = PATH_MAX; - char *path = xmalloc( path_size ); - if (!sysctl( pathname, ARRAY_SIZE(pathname), path, &path_size, NULL, 0 )) - dir = realpath( path, NULL ); - free( path ); -#endif - if (!dir && !(dir = realpath( argv0, NULL ))) return; - dir = get_dirname( dir ); + char *dir = get_argv0_dir( argv0 ); + + if (!dir) return; if (strendswith( dir, "/tools/wmc" )) nlsdirs[0] = strmake( "%s/../../nls", dir ); else nlsdirs[0] = strmake( "%s/%s", dir, BIN_TO_NLSDIR ); -#endif }
static void option_callback( int optc, char *optarg ) diff --git a/tools/wrc/wrc.c b/tools/wrc/wrc.c index fc217afec6c..baf98c3c3bb 100644 --- a/tools/wrc/wrc.c +++ b/tools/wrc/wrc.c @@ -29,9 +29,6 @@ #include <signal.h> #include <limits.h> #include <sys/types.h> -#ifdef HAVE_SYS_SYSCTL_H -# include <sys/sysctl.h> -#endif
#include "../tools.h" #include "wrc.h" @@ -294,25 +291,12 @@ static int load_file( const char *input_name, const char *output_name )
static void init_argv0_dir( const char *argv0 ) { -#ifndef _WIN32 - char *dir = NULL; - -#if defined(__linux__) || defined(__FreeBSD_kernel__) || defined(__NetBSD__) - dir = realpath( "/proc/self/exe", NULL ); -#elif defined (__FreeBSD__) || defined(__DragonFly__) - static int pathname[] = { CTL_KERN, KERN_PROC, KERN_PROC_PATHNAME, -1 }; - size_t path_size = PATH_MAX; - char *path = xmalloc( path_size ); - if (!sysctl( pathname, ARRAY_SIZE(pathname), path, &path_size, NULL, 0 )) - dir = realpath( path, NULL ); - free( path ); -#endif - if (!dir && !(dir = realpath( argv0, NULL ))) return; - dir = get_dirname( dir ); + char *dir = get_argv0_dir( argv0 ); + + if (!dir) return; includedir = strmake( "%s/%s", dir, BIN_TO_INCLUDEDIR ); if (strendswith( dir, "/tools/wrc" )) nlsdirs[0] = strmake( "%s/../../nls", dir ); else nlsdirs[0] = strmake( "%s/%s", dir, BIN_TO_NLSDIR ); -#endif }
static void option_callback( int optc, char *optarg )