Document of c-modernization-kit (funcman) 1.0.0
Loading...
Searching...
No Matches
get_lib_info.c File Reference

共有ライブラリ自身 (.so/.dll) の絶対パスと basename (パスなし・拡張子なし) を取得します。 More...

#include <funcman.h>
#include <dlfcn.h>
#include <limits.h>
#include <stddef.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
Include dependency graph for get_lib_info.c:

Go to the source code of this file.

Typedefs

typedef enum get_lib_info_status_t get_lib_info_status_t
 内部関数の戻り値(ステータス)。

Enumerations

enum  get_lib_info_status_t { MYLIB_OK = 0 , MYLIB_EINVAL = -1 , MYLIB_ENOBUFS = -2 , MYLIB_EFAIL = -3 }
 内部関数の戻り値(ステータス)。 More...

Functions

static get_lib_info_status_t copy_str (char *dst, size_t dst_sz, const char *src)
 文字列を安全にコピーします (UTF-8 想定だが単なる byte 列として扱う)。
static const char * get_ilename_part (const char *path)
 パス文字列からファイル名部分 (最後の区切り文字以降) を返します。
static void strip_extension_inplace (char *s)
 拡張子を取り除きます (その場で書き換え)。
static get_lib_info_status_t get_self_path_posix (char *out_path, size_t out_path_sz, const void *func_addr)
 .so 自身の絶対パスを取得します (Linux/Unix)。
int get_lib_path (char *out_path, const size_t out_path_sz, const void *func_addr)
 指定した関数が所属する共有ライブラリ (.so/.dll) の絶対パスを取得します。
int get_lib_basename (char *out_basename, const size_t out_basename_sz, const void *func_addr)
 指定した関数が所属する共有ライブラリ (.so/.dll) の basename (パスなし・拡張子なし) を取得します。

Detailed Description

共有ライブラリ自身 (.so/.dll) の絶対パスと basename (パスなし・拡張子なし) を取得します。

Author
c-modenization-kit sample team
Date
2026/02/23
Version
1.0.0

Linux(gcc) では dladdr() で共有オブジェクトを特定し、realpath() で可能な限り正規化 (絶対化・symlink 解決) します。 Windows(MSVC) では GetModuleHandleEx() で DLL の HMODULE を得て、GetModuleFileNameW() でパスを取得します。

Note
- 「絶対パス」は OS/ローダの情報とファイルシステム状態に依存します。 Linux でロード後にファイルが移動/削除される等により realpath() が失敗する場合、可能な範囲で絶対化した文字列を返します。
  • Windows は基本的にフルパスが得られますが、古い環境では MAX_PATH 制約が残る場合があります。

Definition in file get_lib_info.c.

Typedef Documentation

◆ get_lib_info_status_t

内部関数の戻り値(ステータス)。

Enumeration Type Documentation

◆ get_lib_info_status_t

内部関数の戻り値(ステータス)。

Enumerator
MYLIB_OK 

成功

MYLIB_EINVAL -1 

引数不正 (NULL、サイズ0など)

MYLIB_ENOBUFS -2 

バッファ不足 (出力が収まらない)

MYLIB_EFAIL -3 

その他の失敗 (取得不能、OS API 失敗など)

Definition at line 47 of file get_lib_info.c.

Function Documentation

◆ copy_str()

get_lib_info_status_t copy_str ( char * dst,
size_t dst_sz,
const char * src )
static

文字列を安全にコピーします (UTF-8 想定だが単なる byte 列として扱う)。

Parameters
[out]dst出力バッファ。
[in]dst_sz出力バッファサイズ[byte]。
[in]src入力文字列 (NULL 終端)。
Returns
get_lib_info_status_t

Definition at line 69 of file get_lib_info.c.

References MYLIB_EINVAL, MYLIB_ENOBUFS, and MYLIB_OK.

Referenced by get_lib_basename(), and get_self_path_posix().

Here is the caller graph for this function:

◆ get_ilename_part()

const char * get_ilename_part ( const char * path)
static

パス文字列からファイル名部分 (最後の区切り文字以降) を返します。

Parameters
[in]pathパス。
Returns
const char* ファイル名部分へのポインタ (元の文字列内)。

Definition at line 94 of file get_lib_info.c.

Referenced by get_lib_basename().

Here is the caller graph for this function:

◆ strip_extension_inplace()

void strip_extension_inplace ( char * s)
static

拡張子を取り除きます (その場で書き換え)。

  • Linux: ".so." が含まれる場合はそこから先を削除 (例: libx.so.1.2.3 -> libx)
    • Linux: 末尾が ".so" の場合は削除 (例: libx.so -> libx)
    • その他: 最後の '.' 以降を削除 (一般的な拡張子扱い)
Parameters
[in,out]s対象文字列 (NULL 終端)。

Definition at line 120 of file get_lib_info.c.

Referenced by get_lib_basename().

Here is the caller graph for this function:

◆ get_self_path_posix()

get_lib_info_status_t get_self_path_posix ( char * out_path,
size_t out_path_sz,
const void * func_addr )
static

.so 自身の絶対パスを取得します (Linux/Unix)。

dladdr() に指定された関数アドレスを渡して所属共有オブジェクトを取得し、
realpath() で可能な限り絶対化・正規化します。

Parameters
[out]out_path出力 (UTF-8、NULL 終端)。
[in]out_path_sz出力バッファサイズ[byte]。
[in]func_addr所属モジュールを特定するための関数アドレス。
Returns
get_lib_info_status_t

Definition at line 258 of file get_lib_info.c.

References copy_str(), MYLIB_EFAIL, and MYLIB_EINVAL.

Referenced by get_lib_path().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_lib_path()

int get_lib_path ( char * out_path,
const size_t out_path_sz,
const void * func_addr )

指定した関数が所属する共有ライブラリ (.so/.dll) の絶対パスを取得します。

Parameters
[out]out_path出力バッファ。
[in]out_path_sz出力バッファサイズ[byte]。
[in]func_addr所属モジュールを特定するための関数アドレス。
Returns
関数が成功した場合、0 を返します。失敗した場合は 0 以外を返します。

Definition at line 313 of file get_lib_info.c.

References get_self_path_posix(), and MYLIB_OK.

Referenced by get_lib_basename().

Here is the call graph for this function:
Here is the caller graph for this function:

◆ get_lib_basename()

int get_lib_basename ( char * out_basename,
const size_t out_basename_sz,
const void * func_addr )

指定した関数が所属する共有ライブラリ (.so/.dll) の basename (パスなし・拡張子なし) を取得します。

Linux の .so.1.2.3 のようなバージョン付きは ".so." より前を basename とみなします。

Parameters
[out]out_basename出力バッファ。
[in]out_basename_sz出力バッファサイズ[byte]。
[in]func_addr所属モジュールを特定するための関数アドレス。
Returns
関数が成功した場合、0 を返します。失敗した場合は 0 以外を返します。

Definition at line 331 of file get_lib_info.c.

References copy_str(), get_ilename_part(), get_lib_path(), MYLIB_EFAIL, MYLIB_EINVAL, MYLIB_OK, and strip_extension_inplace().

Here is the call graph for this function: