Rizin
unix-like reverse engineering framework and cli tools
rz_path.h File Reference
#include <rz_types.h>

Go to the source code of this file.

Functions

RZ_API void rz_path_set_prefix (RZ_NONNULL const char *path)
 Return path prefixed by the Rizin install prefix. More...
 
RZ_API RZ_OWN char * rz_path_prefix (RZ_NULLABLE const char *path)
 Return path prefixed by the Rizin install prefix. More...
 
RZ_API RZ_OWN char * rz_path_incdir (void)
 Return the directory where include files are placed. More...
 
RZ_API RZ_OWN char * rz_path_bindir (void)
 Return the directory where the Rizin binaries are placed. More...
 
RZ_API RZ_OWN char * rz_path_libdir (void)
 Return the directory where the Rizin libraries are placed. More...
 
RZ_API RZ_OWN char * rz_path_system (RZ_NULLABLE const char *path)
 Return the full system path of the given subpath path. More...
 
RZ_API RZ_OWN char * rz_path_home_prefix (RZ_NULLABLE const char *path)
 Return path prefixed by the home prefix. More...
 
RZ_API RZ_OWN char * rz_path_home (RZ_NULLABLE const char *path)
 Return a new path relative to the home directory. More...
 
RZ_API RZ_OWN char * rz_path_system_rc (void)
 Return the system path of the global rizinrc file. More...
 
RZ_API RZ_OWN char * rz_path_home_rc (void)
 Return the path of the rizinrc file in the home directory. More...
 
RZ_API RZ_OWN char * rz_path_home_config_rc (void)
 Return the path of the rizinrc file in the home config directory. More...
 
RZ_API RZ_OWN char * rz_path_home_config_rcdir (void)
 Return the home directory of config rizinrc.d. More...
 
RZ_API RZ_OWN char * rz_path_home_config (void)
 Return the home directory for config files (e.g. ~/.config/rizin) More...
 
RZ_API RZ_OWN char * rz_path_home_cache (void)
 Return the home directory for cache files (e.g. ~/.cache/rizin) More...
 
RZ_API RZ_OWN char * rz_path_home_history (void)
 Return the path for the command history file. More...
 
RZ_API RZ_OWN char * rz_path_home_expand (RZ_NULLABLE const char *path)
 Return a new path with the ~ char expanded to the home directory. More...
 
RZ_API RZ_OWN char * rz_path_realpath (RZ_NULLABLE const char *path)
 Return a canonicalized absolute path. Expands all symbolic links and resolves references to /./, /../ and extra '/' characters. More...
 

Function Documentation

◆ rz_path_bindir()

RZ_API RZ_OWN char* rz_path_bindir ( void  )

Return the directory where the Rizin binaries are placed.

Definition at line 148 of file path.c.

148  {
149  return rz_path_prefix(RZ_BINDIR);
150 }
#define RZ_BINDIR
Definition: rz_userconf.h:66
RZ_API RZ_OWN char * rz_path_prefix(RZ_NULLABLE const char *path)
Return path prefixed by the Rizin install prefix.
Definition: path.c:121

References RZ_BINDIR, and rz_path_prefix().

Referenced by rz_str_version().

◆ rz_path_home()

RZ_API RZ_OWN char* rz_path_home ( RZ_NULLABLE const char *  path)

Return a new path relative to the home directory.

Parameters
pathSub-path relative to the home directory
Returns
New path prefixed by the home directory

Definition at line 243 of file path.c.

243  {
244  char *home = rz_sys_getenv(RZ_SYS_HOME);
245  if (!home) {
246  home = rz_file_tmpdir();
247  if (!home) {
248  return NULL;
249  }
250  }
251  char *res;
252  if (path) {
253  res = rz_file_path_join(home, path);
254  free(home);
255  } else {
256  res = home;
257  }
258  return res;
259 }
#define NULL
Definition: cris-opc.c:27
static static fork const void static count static fd const char const char static newpath const char static path const char path
Definition: sflib.h:35
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
RZ_API char * rz_file_tmpdir(void)
Definition: file.c:1132
RZ_API RZ_OWN char * rz_file_path_join(RZ_NONNULL const char *s1, RZ_NULLABLE const char *s2)
Concatenate two paths to create a new one with s1+s2 with the correct path separator.
Definition: file.c:1312
RZ_API char * rz_sys_getenv(const char *key)
Get the value of an environment variable named key or NULL if none exists.
Definition: sys.c:483
#define RZ_SYS_HOME
Definition: rz_types.h:220

References free(), NULL, path, rz_file_path_join(), rz_file_tmpdir(), rz_sys_getenv(), and RZ_SYS_HOME.

Referenced by rz_path_home_cache(), rz_path_home_config(), rz_path_home_config_rc(), rz_path_home_config_rcdir(), rz_path_home_expand(), rz_path_home_history(), and rz_path_home_rc().

◆ rz_path_home_cache()

RZ_API RZ_OWN char* rz_path_home_cache ( void  )

Return the home directory for cache files (e.g. ~/.cache/rizin)

Definition at line 205 of file path.c.

205  {
207 }
#define RZ_HOME_CACHEDIR
Definition: rz_userconf.h:94
RZ_API RZ_OWN char * rz_path_home(RZ_NULLABLE const char *path)
Return a new path relative to the home directory.
Definition: path.c:243

References RZ_HOME_CACHEDIR, and rz_path_home().

Referenced by main_print_var().

◆ rz_path_home_config()

RZ_API RZ_OWN char* rz_path_home_config ( void  )

Return the home directory for config files (e.g. ~/.config/rizin)

Definition at line 198 of file path.c.

198  {
200 }
#define RZ_HOME_CONFIGDIR
Definition: rz_userconf.h:93

References RZ_HOME_CONFIGDIR, and rz_path_home().

Referenced by main_print_var().

◆ rz_path_home_config_rc()

RZ_API RZ_OWN char* rz_path_home_config_rc ( void  )

Return the path of the rizinrc file in the home config directory.

Definition at line 226 of file path.c.

226  {
228 }
#define RZ_HOME_CONFIG_RC
Definition: rz_userconf.h:102

References RZ_HOME_CONFIG_RC, and rz_path_home().

Referenced by main_help(), and rz_core_parse_rizinrc().

◆ rz_path_home_config_rcdir()

RZ_API RZ_OWN char* rz_path_home_config_rcdir ( void  )

Return the home directory of config rizinrc.d.

Definition at line 233 of file path.c.

233  {
235 }
#define RZ_HOME_CONFIG_RC_DIR
Definition: rz_userconf.h:103

References RZ_HOME_CONFIG_RC_DIR, and rz_path_home().

Referenced by main_help(), and rz_core_parse_rizinrc().

◆ rz_path_home_expand()

RZ_API RZ_OWN char* rz_path_home_expand ( RZ_NULLABLE const char *  path)

Return a new path with the ~ char expanded to the home directory.

Parameters
pathOriginal path that may or may not contain the ~ prefix to refer to the home directory
Returns
New path with the ~ character replaced with the full path of the home directory

Definition at line 268 of file path.c.

268  {
269  // if the path does not start with `~`, there is nothing to expand
270  if (path && path[0] != '~') {
271  return strdup(path);
272  }
273 
274  // if the path starts with `~` but it is not `~/` or just `~`, then it is a
275  // valid name (e.g. `~hello`)
276  if (path && path[0] && path[1] && path[1] != '/') {
277  return strdup(path);
278  }
279 
280  return rz_path_home(path + 1);
281 }
return strdup("=SP r13\n" "=LR r14\n" "=PC r15\n" "=A0 r0\n" "=A1 r1\n" "=A2 r2\n" "=A3 r3\n" "=ZF zf\n" "=SF nf\n" "=OF vf\n" "=CF cf\n" "=SN or0\n" "gpr lr .32 56 0\n" "gpr pc .32 60 0\n" "gpr cpsr .32 64 0 ____tfiae_________________qvczn\n" "gpr or0 .32 68 0\n" "gpr tf .1 64.5 0 thumb\n" "gpr ef .1 64.9 0 endian\n" "gpr jf .1 64.24 0 java\n" "gpr qf .1 64.27 0 sticky_overflow\n" "gpr vf .1 64.28 0 overflow\n" "gpr cf .1 64.29 0 carry\n" "gpr zf .1 64.30 0 zero\n" "gpr nf .1 64.31 0 negative\n" "gpr itc .4 64.10 0 if_then_count\n" "gpr gef .4 64.16 0 great_or_equal\n" "gpr r0 .32 0 0\n" "gpr r1 .32 4 0\n" "gpr r2 .32 8 0\n" "gpr r3 .32 12 0\n" "gpr r4 .32 16 0\n" "gpr r5 .32 20 0\n" "gpr r6 .32 24 0\n" "gpr r7 .32 28 0\n" "gpr r8 .32 32 0\n" "gpr r9 .32 36 0\n" "gpr r10 .32 40 0\n" "gpr r11 .32 44 0\n" "gpr r12 .32 48 0\n" "gpr r13 .32 52 0\n" "gpr r14 .32 56 0\n" "gpr r15 .32 60 0\n" "gpr r16 .32 64 0\n" "gpr r17 .32 68 0\n")

References path, rz_path_home(), and strdup().

Referenced by autocmplt_cmd_arg_file(), rz_file_abspath_rel(), rz_sys_chdir(), rz_sys_fopen(), rz_sys_open(), and rz_syscmd_ls().

◆ rz_path_home_history()

RZ_API RZ_OWN char* rz_path_home_history ( void  )

Return the path for the command history file.

Definition at line 212 of file path.c.

212  {
214 }
#define RZ_HOME_HISTORY
Definition: rz_userconf.h:96

References RZ_HOME_HISTORY, and rz_path_home().

Referenced by rz_core_init(), rz_history_save_handler(), and rz_main_rizin().

◆ rz_path_home_prefix()

RZ_API RZ_OWN char* rz_path_home_prefix ( RZ_NULLABLE const char *  path)

Return path prefixed by the home prefix.

Return path prefixed by the home prefix. Please note that this is not the home directory, but it is usually something like ~/.local.

Parameters
pathPath to put in the home prefix context or NULL to just get the home prefix
Returns
path prefixed by the home prefix or just the home prefix

Definition at line 182 of file path.c.

182  {
183  char *home = rz_sys_getenv(RZ_SYS_HOME);
184  if (!home) {
185  home = rz_file_tmpdir();
186  if (!home) {
187  return NULL;
188  }
189  }
190  char *res = rz_str_newf("%s" RZ_SYS_DIR "%s" RZ_SYS_DIR "%s", home, RZ_HOME_PREFIX, path);
191  free(home);
192  return res;
193 }
RZ_API char * rz_str_newf(const char *fmt,...) RZ_PRINTF_CHECK(1
#define RZ_SYS_DIR
Definition: rz_types.h:218
#define RZ_HOME_PREFIX
Definition: rz_userconf.h:92

References free(), NULL, path, rz_file_tmpdir(), RZ_HOME_PREFIX, rz_str_newf(), RZ_SYS_DIR, rz_sys_getenv(), and RZ_SYS_HOME.

Referenced by __get_panels_config_dir_path(), __load_plugins(), cmd_print_format(), hash_load_plugins(), load_scripts_for(), loadSystemPlugins(), main_help(), main_print_var(), rz_core_analysis_cc_init(), rz_core_analysis_sigdb_list(), rz_core_config_init(), rz_core_loadlibs(), rz_core_theme_list(), rz_core_theme_load(), rz_core_visual_hud(), rz_lib_open(), and rz_main_rz_bin().

◆ rz_path_home_rc()

RZ_API RZ_OWN char* rz_path_home_rc ( void  )

Return the path of the rizinrc file in the home directory.

Definition at line 219 of file path.c.

219  {
220  return rz_path_home(RZ_HOME_RC);
221 }
#define RZ_HOME_RC
Definition: rz_userconf.h:105

References RZ_HOME_RC, and rz_path_home().

Referenced by main_help(), and rz_core_parse_rizinrc().

◆ rz_path_incdir()

RZ_API RZ_OWN char* rz_path_incdir ( void  )

Return the directory where include files are placed.

Definition at line 141 of file path.c.

141  {
142  return rz_path_prefix(RZ_INCDIR);
143 }
#define RZ_INCDIR
Definition: rz_userconf.h:68

References RZ_INCDIR, and rz_path_prefix().

Referenced by main_help(), and main_print_var().

◆ rz_path_libdir()

RZ_API RZ_OWN char* rz_path_libdir ( void  )

Return the directory where the Rizin libraries are placed.

Definition at line 155 of file path.c.

155  {
156  return rz_path_prefix(RZ_LIBDIR);
157 }
#define RZ_LIBDIR
Definition: rz_userconf.h:67

References RZ_LIBDIR, and rz_path_prefix().

Referenced by main_help(), main_print_var(), rz_core_file_loadlib(), and rz_run_config_env().

◆ rz_path_prefix()

RZ_API RZ_OWN char* rz_path_prefix ( RZ_NULLABLE const char *  path)

Return path prefixed by the Rizin install prefix.

The install prefix is taken from the build-time configuration RZ_PREFIX, unless Rizin was not compiled as "portable". In such a case the prefix is discovered from the path of the executable calling this function.

Parameters
pathPath to put in the install prefix context or NULL to just get the install prefix
Returns
path prefixed by the Rizin install prefix or just the install prefix

Definition at line 121 of file path.c.

121  {
122 #if RZ_IS_PORTABLE
123  rz_th_lock_enter(portable_prefix_mutex);
124  if (!portable_prefix_searched) {
125  portable_prefix = set_portable_prefix();
126  portable_prefix_searched = true;
127  }
128  rz_th_lock_leave(portable_prefix_mutex);
129 
130  if (portable_prefix) {
131  return rz_file_path_join(portable_prefix, path);
132  }
133 
134 #endif
136 }
#define RZ_PREFIX
Definition: rz_userconf.h:65
RZ_API void rz_th_lock_leave(RZ_NONNULL RzThreadLock *thl)
Releases a RzThreadLock structure.
Definition: thread_lock.c:75
RZ_API void rz_th_lock_enter(RZ_NONNULL RzThreadLock *thl)
Acquires a RzThreadLock structure.
Definition: thread_lock.c:45

References path, rz_file_path_join(), RZ_PREFIX, rz_th_lock_enter(), and rz_th_lock_leave().

Referenced by cmd_print_format(), main_help(), main_print_var(), rz_core_config_init(), rz_egg_lang_include_init(), rz_path_bindir(), rz_path_incdir(), rz_path_libdir(), rz_path_system(), and rz_path_system_rc().

◆ rz_path_realpath()

RZ_API RZ_OWN char* rz_path_realpath ( RZ_NULLABLE const char *  path)

Return a canonicalized absolute path. Expands all symbolic links and resolves references to /./, /../ and extra '/' characters.

Parameters
pathOriginal file path.
Returns
New canonicalized absolute path.

Definition at line 290 of file path.c.

290  {
291  if (!path) {
292  return NULL;
293  }
294 #if HAVE_REALPATH
295  char buf[PATH_MAX] = { 0 };
296  const char *rp = realpath(path, buf);
297  if (rp) {
298  return strdup(rp);
299  }
300 #elif __WINDOWS__
301  wchar_t buf[MAX_PATH] = { 0 };
302 
303  wchar_t *wpath = rz_utf8_to_utf16(path);
304  DWORD len = GetFullPathNameW(wpath, MAX_PATH, buf, NULL);
305  free(wpath);
306  if (len > 0 && len < MAX_PATH - 1) {
307  return rz_utf16_to_utf8_l(buf, len);
308  }
309 #endif
310  return NULL;
311 }
size_t len
Definition: 6502dis.c:15
static char * rp[]
Definition: i8080dis.c:36
voidpf void * buf
Definition: ioapi.h:138
DWORD

References DWORD, free(), len, NULL, path, rp, and strdup().

◆ rz_path_set_prefix()

RZ_API void rz_path_set_prefix ( RZ_NONNULL const char *  path)

Return path prefixed by the Rizin install prefix.

The install prefix is taken from the build-time configuration RZ_PREFIX, unless Rizin was not compiled as "portable". In such a case the prefix is either discovered from the path of the executable calling this function or stored via the path variable

Parameters
pathPath to use when to prefix or NULL to use the binary location

Definition at line 97 of file path.c.

97  {
98 #if RZ_IS_PORTABLE
99  rz_th_lock_enter(portable_prefix_mutex);
100  free(portable_prefix);
101  if (RZ_STR_ISNOTEMPTY(path)) {
102  portable_prefix = strdup(path);
103  } else {
104  portable_prefix = set_portable_prefix();
105  }
106  portable_prefix_searched = true;
107  rz_th_lock_leave(portable_prefix_mutex);
108 #endif
109 }
#define RZ_STR_ISNOTEMPTY(x)
Definition: rz_str.h:68

References free(), path, RZ_STR_ISNOTEMPTY, rz_th_lock_enter(), rz_th_lock_leave(), and strdup().

◆ rz_path_system()

◆ rz_path_system_rc()

RZ_API RZ_OWN char* rz_path_system_rc ( void  )

Return the system path of the global rizinrc file.

Definition at line 169 of file path.c.

169  {
171 }
#define RZ_GLOBAL_RC
Definition: rz_userconf.h:104

References RZ_GLOBAL_RC, and rz_path_prefix().

Referenced by main_help(), and rz_main_rizin().