X-Git-Url: http://git.efficios.com/?a=blobdiff_plain;f=src%2Fcommon%2Fcommon.c;h=ed079c940c28a310f297554eb6dc430afa270c54;hb=3a5e626b3a9e60e09aea53f134f6cf8559a25494;hp=f37bec4112de72b90edb4afa8d50a8859ca92b15;hpb=578e048b5debf169e286e5b5cc747b5d6c16886d;p=babeltrace.git diff --git a/src/common/common.c b/src/common/common.c index f37bec41..ed079c94 100644 --- a/src/common/common.c +++ b/src/common/common.c @@ -22,8 +22,9 @@ * SOFTWARE. */ +#define BT_LOG_OUTPUT_LEVEL log_level #define BT_LOG_TAG "COMMON" -#include "logging.h" +#include "logging/log.h" #include #include @@ -39,7 +40,7 @@ #include #include #include -#include "common/babeltrace.h" +#include "common/macros.h" #include "common/common.h" #include "compat/unistd.h" @@ -115,8 +116,15 @@ bool bt_common_is_setuid_setgid(void) } #endif /* __MINGW32__ */ +#ifdef __MINGW32__ +static +const char *bt_get_home_dir(int log_level) +{ + return g_get_home_dir(); +} +#else /* __MINGW32__ */ static -char *bt_secure_getenv(const char *name) +char *bt_secure_getenv(const char *name, int log_level) { if (bt_common_is_setuid_setgid()) { BT_LOGD("Disregarding environment variable for setuid/setgid binary: " @@ -126,20 +134,13 @@ char *bt_secure_getenv(const char *name) return getenv(name); } -#ifdef __MINGW32__ static -const char *bt_get_home_dir(void) -{ - return g_get_home_dir(); -} -#else /* __MINGW32__ */ -static -const char *bt_get_home_dir(void) +const char *bt_get_home_dir(int log_level) { char *val = NULL; struct passwd *pwd; - val = bt_secure_getenv(HOME_ENV_VAR); + val = bt_secure_getenv(HOME_ENV_VAR, log_level); if (val) { goto end; } @@ -155,13 +156,13 @@ end: #endif /* __MINGW32__ */ BT_HIDDEN -char *bt_common_get_home_plugin_path(void) +char *bt_common_get_home_plugin_path(int log_level) { char *path = NULL; const char *home_dir; size_t length; - home_dir = bt_get_home_dir(); + home_dir = bt_get_home_dir(log_level); if (!home_dir) { goto end; } @@ -169,8 +170,8 @@ char *bt_common_get_home_plugin_path(void) length = strlen(home_dir) + strlen(HOME_PLUGIN_SUBPATH) + 1; if (length >= PATH_MAX) { - BT_LOGW("Home directory path is too long: length=%zu", - length); + BT_LOGW("Home directory path is too long: " + "length=%zu, max-length=%u", length, PATH_MAX); goto end; } @@ -1087,6 +1088,38 @@ end_of_pattern: return p[-1] == '*' && at_end_of_pattern(p, pattern, pattern_len); } +#ifdef __MINGW32__ +BT_HIDDEN +GString *bt_common_normalize_path(const char *path, const char *wd) +{ + char *tmp; + GString *norm_path = NULL; + + BT_ASSERT(path); + + tmp = _fullpath(NULL, path, PATH_MAX); + if (!tmp) { + goto error; + } + + norm_path = g_string_new(tmp); + if (!norm_path) { + goto error; + } + + goto end; +error: + if (norm_path) { + g_string_free(norm_path, TRUE); + norm_path = NULL; + } +end: + if (tmp) { + free(tmp); + } + return norm_path; +} +#else static void append_path_parts(const char *path, GPtrArray *parts) { @@ -1120,38 +1153,6 @@ void destroy_gstring(void *gstring) (void) g_string_free(gstring, TRUE); } -#ifdef __MINGW32__ -BT_HIDDEN -GString *bt_common_normalize_path(const char *path, const char *wd) -{ - char *tmp; - GString *norm_path = NULL; - - BT_ASSERT(path); - - tmp = _fullpath(NULL, path, PATH_MAX); - if (!tmp) { - goto error; - } - - norm_path = g_string_new(tmp); - if (!norm_path) { - goto error; - } - - goto end; -error: - if (norm_path) { - g_string_free(norm_path, TRUE); - norm_path = NULL; - } -end: - if (tmp) { - free(tmp); - } - return norm_path; -} -#else BT_HIDDEN GString *bt_common_normalize_path(const char *path, const char *wd) { @@ -1238,7 +1239,7 @@ end: #endif BT_HIDDEN -size_t bt_common_get_page_size(void) +size_t bt_common_get_page_size(int log_level) { int page_size; @@ -1567,3 +1568,106 @@ void bt_common_custom_snprintf(char *buf, size_t buf_size, priv_data, fmt, &args); va_end(args); } + +BT_HIDDEN +void bt_common_sep_digits(char *str, unsigned int digits_per_group, char sep) +{ + const char *rd; + char *wr; + uint64_t i = 0; + uint64_t orig_len; + uint64_t sep_count; + uint64_t new_len; + + BT_ASSERT(digits_per_group > 0); + BT_ASSERT(sep != '\0'); + + /* Compute new length of `str` */ + orig_len = strlen(str); + BT_ASSERT(orig_len > 0); + sep_count = (orig_len - 1) / digits_per_group; + new_len = strlen(str) + sep_count; + + /* + * Do the work in place. Have the reading pointer `rd` start at + * the end of the original string, and the writing pointer `wr` + * start at the end of the new string, making sure to also put a + * null character there. + */ + rd = str + orig_len - 1; + wr = str + new_len; + *wr = '\0'; + wr--; + + /* + * Here's what the process looks like (3 digits per group): + * + * Source: 12345678 + * ^ + * Destination: 12345678#8 + * ^ + * + * Source: 12345678 + * ^ + * Destination: 1234567878 + * ^ + * + * Source: 12345678 + * ^ + * Destination: 1234567678 + * ^ + * + * Source: 12345678 + * ^ + * Destination: 123456,678 + * ^ + * + * Source: 12345678 + * ^ + * Destination: 123455,678 + * ^ + * + * Source: 12345678 + * ^ + * Destination: 123445,678 + * ^ + * + * Source: 12345678 + * ^ + * Destination: 123345,678 + * ^ + * + * Source: 12345678 + * ^ + * Destination: 12,345,678 + * ^ + * + * Source: 12345678 + * ^ + * Destination: 12,345,678 + * ^ + * + * Source: 12345678 + * ^ + * Destination: 12,345,678 + * ^ + */ + while (rd != str - 1) { + if (i == digits_per_group) { + /* + * Time to append the separator: decrement `wr`, + * but keep `rd` as is. + */ + i = 0; + *wr = sep; + wr--; + continue; + } + + /* Copy read-side character to write-side character */ + *wr = *rd; + wr--; + rd--; + i++; + } +}