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>
34 #define SYSTEM_PLUGIN_PATH INSTALL_LIBDIR "/babeltrace/plugins"
35 #define HOME_ENV_VAR "HOME"
36 #define HOME_PLUGIN_SUBPATH "/.local/lib/babeltrace/plugins"
39 const char *bt_common_get_system_plugin_path(void)
41 return SYSTEM_PLUGIN_PATH
;
45 bool bt_common_is_setuid_setgid(void)
47 return (geteuid() != getuid() || getegid() != getgid());
50 static char *bt_secure_getenv(const char *name
)
52 if (bt_common_is_setuid_setgid()) {
53 printf_error("Disregarding %s environment variable for setuid/setgid binary",
60 static const char *get_home_dir(void)
65 val
= bt_secure_getenv(HOME_ENV_VAR
);
69 /* Fallback on password file. */
70 pwd
= getpwuid(getuid());
80 char *bt_common_get_home_plugin_path(void)
85 home_dir
= get_home_dir();
90 if (strlen(home_dir
) + strlen(HOME_PLUGIN_SUBPATH
) + 1 >= PATH_MAX
) {
91 printf_error("Home directory path is too long: `%s`\n",
96 path
= malloc(PATH_MAX
);
101 strcpy(path
, home_dir
);
102 strcat(path
, HOME_PLUGIN_SUBPATH
);
109 int bt_common_append_plugin_path_dirs(const char *paths
, GPtrArray
*dirs
)
114 size_t init_dirs_len
;
117 init_dirs_len
= dirs
->len
;
120 /* Nothing to append */
125 end
= paths
+ strlen(paths
);
129 const char *next_colon
;
131 next_colon
= strchr(at
, ':');
132 if (next_colon
== at
) {
134 * Empty path: try next character (supported
135 * to conform to the typical parsing of $PATH).
139 } else if (!next_colon
) {
140 /* No more colon: use the remaining */
141 next_colon
= paths
+ strlen(paths
);
144 path
= g_string_new(NULL
);
149 g_string_append_len(path
, at
, next_colon
- at
);
151 g_ptr_array_add(dirs
, path
);
159 /* Remove the new entries in dirs */
160 while (dirs
->len
> init_dirs_len
) {
161 g_ptr_array_remove_index(dirs
, init_dirs_len
);
168 static bool supports_colors(void)
170 static bool supports_colors
= false;
171 static bool supports_colors_set
= false;
174 if (supports_colors_set
) {
178 supports_colors_set
= true;
180 term
= getenv("TERM");
185 if (strncmp(term
, "xterm", 5) != 0 &&
186 strncmp(term
, "rxvt", 4) != 0 &&
187 strncmp(term
, "konsole", 7) != 0 &&
188 strncmp(term
, "gnome", 5) != 0) {
196 supports_colors
= true;
199 return supports_colors
;
203 const char *bt_common_color_reset(void)
205 return supports_colors() ? "\033[0m" : "";
209 const char *bt_common_color_bold(void)
211 return supports_colors() ? "\033[1m" : "";
215 const char *bt_common_color_fg_default(void)
217 return supports_colors() ? "\033[39m" : "";
221 const char *bt_common_color_fg_red(void)
223 return supports_colors() ? "\033[31m" : "";
227 const char *bt_common_color_fg_green(void)
229 return supports_colors() ? "\033[32m" : "";
233 const char *bt_common_color_fg_yellow(void)
235 return supports_colors() ? "\033[33m" : "";
239 const char *bt_common_color_fg_blue(void)
241 return supports_colors() ? "\033[34m" : "";
245 const char *bt_common_color_fg_magenta(void)
247 return supports_colors() ? "\033[35m" : "";
251 const char *bt_common_color_fg_cyan(void)
253 return supports_colors() ? "\033[36m" : "";
257 const char *bt_common_color_fg_light_gray(void)
259 return supports_colors() ? "\033[37m" : "";
263 const char *bt_common_color_bg_default(void)
265 return supports_colors() ? "\033[49m" : "";
269 const char *bt_common_color_bg_red(void)
271 return supports_colors() ? "\033[41m" : "";
275 const char *bt_common_color_bg_green(void)
277 return supports_colors() ? "\033[42m" : "";
281 const char *bt_common_color_bg_yellow(void)
283 return supports_colors() ? "\033[43m" : "";
287 const char *bt_common_color_bg_blue(void)
289 return supports_colors() ? "\033[44m" : "";
293 const char *bt_common_color_bg_magenta(void)
295 return supports_colors() ? "\033[45m" : "";
299 const char *bt_common_color_bg_cyan(void)
301 return supports_colors() ? "\033[46m" : "";
305 const char *bt_common_color_bg_light_gray(void)
307 return supports_colors() ? "\033[47m" : "";
This page took 0.035648 seconds and 4 git commands to generate.