xrt: Add u_file_get_runtime_dir() and u_file_get_path_in_runtime_dir()

Analog to u_file_get_path_in_config_dir.
This commit is contained in:
Christoph Haag 2020-10-21 20:48:54 +02:00 committed by Ryan Pavlik
parent 0e18e1f8b0
commit c92bc0a704
2 changed files with 30 additions and 0 deletions

View file

@ -104,6 +104,30 @@ u_file_open_file_in_config_dir(const char *filename, const char *mode)
return fopen(file_str, mode);
}
ssize_t
u_file_get_runtime_dir(char *out_path, size_t out_path_size)
{
const char *xgd_rt = getenv("XDG_RUNTIME_DIR");
if (xgd_rt != NULL) {
return snprintf(out_path, out_path_size, "%s", xgd_rt);
}
const char *tmp = "/tmp";
return snprintf(out_path, out_path_size, "%s", tmp);
}
ssize_t
u_file_get_path_in_runtime_dir(const char *filename, char *out_path, size_t out_path_size)
{
char tmp[PATH_MAX];
ssize_t i = u_file_get_runtime_dir(tmp, sizeof(tmp));
if (i <= 0) {
return -1;
}
return snprintf(out_path, out_path_size, "%s/%s", tmp, filename);
}
#endif
char *

View file

@ -28,9 +28,15 @@ u_file_get_path_in_config_dir(const char *suffix, char *out_path, size_t out_pat
FILE *
u_file_open_file_in_config_dir(const char *filename, const char *mode);
ssize_t
u_file_get_runtime_dir(char *out_path, size_t out_path_size);
char *
u_file_read_content(FILE *file);
ssize_t
u_file_get_path_in_runtime_dir(const char *filename, char *out_path, size_t out_path_size);
#ifdef __cplusplus
}
#endif