2 * Babeltrace common functions
4 * Copyright 2016 Philippe Proulx <pproulx@efficios.com>
6 * Permission is hereby granted, free of charge, to any person obtaining a copy
7 * of this software and associated documentation files (the "Software"), to deal
8 * in the Software without restriction, including without limitation the rights
9 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10 * copies of the Software, and to permit persons to whom the Software is
11 * furnished to do so, subject to the following conditions:
13 * The above copyright notice and this permission notice shall be included in
14 * all copies or substantial portions of the Software.
16 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
27 #include <sys/types.h>
32 #include <babeltrace/babeltrace-internal.h>
33 #include <babeltrace/common-internal.h>
35 #define SYSTEM_PLUGIN_PATH INSTALL_LIBDIR "/babeltrace/plugins"
36 #define HOME_ENV_VAR "HOME"
37 #define HOME_PLUGIN_SUBPATH "/.local/lib/babeltrace/plugins"
40 const char *bt_common_get_system_plugin_path(void)
42 return SYSTEM_PLUGIN_PATH
;
46 bool bt_common_is_setuid_setgid(void)
48 return (geteuid() != getuid() || getegid() != getgid());
51 static char *bt_secure_getenv(const char *name
)
53 if (bt_common_is_setuid_setgid()) {
54 printf_error("Disregarding %s environment variable for setuid/setgid binary",
61 static const char *get_home_dir(void)
66 val
= bt_secure_getenv(HOME_ENV_VAR
);
70 /* Fallback on password file. */
71 pwd
= getpwuid(getuid());
81 char *bt_common_get_home_plugin_path(void)
86 home_dir
= get_home_dir();
91 if (strlen(home_dir
) + strlen(HOME_PLUGIN_SUBPATH
) + 1 >= PATH_MAX
) {
92 printf_error("Home directory path is too long: `%s`\n",
97 path
= malloc(PATH_MAX
);
102 strcpy(path
, home_dir
);
103 strcat(path
, HOME_PLUGIN_SUBPATH
);
110 int bt_common_append_plugin_path_dirs(const char *paths
, GPtrArray
*dirs
)
115 size_t init_dirs_len
;
118 init_dirs_len
= dirs
->len
;
121 /* Nothing to append */
126 end
= paths
+ strlen(paths
);
130 const char *next_colon
;
132 next_colon
= strchr(at
, ':');
133 if (next_colon
== at
) {
135 * Empty path: try next character (supported
136 * to conform to the typical parsing of $PATH).
140 } else if (!next_colon
) {
141 /* No more colon: use the remaining */
142 next_colon
= paths
+ strlen(paths
);
145 path
= g_string_new(NULL
);
150 g_string_append_len(path
, at
, next_colon
- at
);
152 g_ptr_array_add(dirs
, path
);
160 /* Remove the new entries in dirs */
161 while (dirs
->len
> init_dirs_len
) {
162 g_ptr_array_remove_index(dirs
, init_dirs_len
);
170 bool bt_common_colors_supported(void)
172 static bool supports_colors
= false;
173 static bool supports_colors_set
= false;
176 if (supports_colors_set
) {
180 supports_colors_set
= true;
182 term
= getenv("TERM");
187 if (strncmp(term
, "xterm", 5) != 0 &&
188 strncmp(term
, "rxvt", 4) != 0 &&
189 strncmp(term
, "konsole", 7) != 0 &&
190 strncmp(term
, "gnome", 5) != 0) {
198 supports_colors
= true;
201 return supports_colors
;
205 const char *bt_common_color_reset(void)
207 return bt_common_colors_supported() ? BT_COMMON_COLOR_RESET
: "";
211 const char *bt_common_color_bold(void)
213 return bt_common_colors_supported() ? BT_COMMON_COLOR_BOLD
: "";
217 const char *bt_common_color_fg_default(void)
219 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_DEFAULT
: "";
223 const char *bt_common_color_fg_red(void)
225 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_RED
: "";
229 const char *bt_common_color_fg_green(void)
231 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_GREEN
: "";
235 const char *bt_common_color_fg_yellow(void)
237 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_YELLOW
: "";
241 const char *bt_common_color_fg_blue(void)
243 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_BLUE
: "";
247 const char *bt_common_color_fg_magenta(void)
249 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_MAGENTA
: "";
253 const char *bt_common_color_fg_cyan(void)
255 return bt_common_colors_supported() ? BT_COMMON_COLOR_FG_CYAN
: "";
259 const char *bt_common_color_fg_light_gray(void)
261 return bt_common_colors_supported() ?
262 BT_COMMON_COLOR_FG_LIGHT_GRAY
: "";
266 const char *bt_common_color_bg_default(void)
268 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_DEFAULT
: "";
272 const char *bt_common_color_bg_red(void)
274 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_RED
: "";
278 const char *bt_common_color_bg_green(void)
280 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_GREEN
: "";
284 const char *bt_common_color_bg_yellow(void)
286 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_YELLOW
: "";
290 const char *bt_common_color_bg_blue(void)
292 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_BLUE
: "";
296 const char *bt_common_color_bg_magenta(void)
298 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_MAGENTA
: "";
302 const char *bt_common_color_bg_cyan(void)
304 return bt_common_colors_supported() ? BT_COMMON_COLOR_BG_CYAN
: "";
308 const char *bt_common_color_bg_light_gray(void)
310 return bt_common_colors_supported() ?
311 BT_COMMON_COLOR_BG_LIGHT_GRAY
: "";