tools: Add a common helper to get the argv0 directory.

This commit is contained in:
Alexandre Julliard 2023-01-25 11:14:36 +01:00
parent a0a3c25e84
commit ecb651c01f
5 changed files with 35 additions and 74 deletions

View file

@ -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;

View file

@ -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;
char *dir = get_argv0_dir( argv0 );
#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
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 )

View file

@ -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)

View file

@ -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;
char *dir = get_argv0_dir( argv0 );
#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 );
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 )

View file

@ -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;
char *dir = get_argv0_dir( argv0 );
#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 );
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 )