*/
#define _LGPL_SOURCE
-#include <common/macros.hpp>
#include <common/common.hpp>
+#include <common/macros.hpp>
#include <common/path.hpp>
/*
*
* Return a newly-allocated string.
*/
-static
-char *utils_partial_realpath(const char *path)
+static char *utils_partial_realpath(const char *path)
{
- char *cut_path = NULL, *try_path = NULL, *try_path_prev = NULL;
+ char *cut_path = nullptr, *try_path = nullptr, *try_path_prev = nullptr;
const char *next, *prev, *end;
- char *resolved_path = NULL;
+ char *resolved_path = nullptr;
/* Safety net */
- if (path == NULL) {
+ if (path == nullptr) {
goto error;
}
* the path given as argument
*/
end = path + strlen(path);
- if (*(end-1) == '/') {
+ if (*(end - 1) == '/') {
end--;
}
next = path;
prev = next;
/* Only to ensure try_path is not NULL to enter the while */
- try_path = (char *)next;
+ try_path = (char *) next;
/* Resolve the canonical path of the first part of the path */
- while (try_path != NULL && next != end) {
- char *try_path_buf = NULL;
+ while (try_path != nullptr && next != end) {
+ char *try_path_buf = nullptr;
/*
* If there is not any '/' left, we want to try with
* the full path
*/
next = strpbrk(next + 1, "/");
- if (next == NULL) {
+ if (next == nullptr) {
next = end;
}
/* Cut the part we will be trying to resolve */
cut_path = lttng_strndup(path, next - path);
- if (cut_path == NULL) {
+ if (cut_path == nullptr) {
PERROR("lttng_strndup");
goto error;
}
- try_path_buf = (char *) zmalloc(LTTNG_PATH_MAX);
+ try_path_buf = zmalloc<char>(LTTNG_PATH_MAX);
if (!try_path_buf) {
PERROR("zmalloc");
goto error;
/* Try to resolve this part */
try_path = realpath((char *) cut_path, try_path_buf);
- if (try_path == NULL) {
+ if (try_path == nullptr) {
free(try_path_buf);
/*
* There was an error, we just want to be assured it
}
} else {
/* Save the place we are before trying the next step */
- try_path_buf = NULL;
+ try_path_buf = nullptr;
free(try_path_prev);
try_path_prev = try_path;
prev = next;
/* Free the allocated memory */
free(cut_path);
- cut_path = NULL;
+ cut_path = nullptr;
}
/* Allocate memory for the resolved path. */
- resolved_path = (char *) zmalloc(LTTNG_PATH_MAX);
- if (resolved_path == NULL) {
+ resolved_path = zmalloc<char>(LTTNG_PATH_MAX);
+ if (resolved_path == nullptr) {
PERROR("zmalloc resolved path");
goto error;
}
* If we were able to solve at least partially the path, we can concatenate
* what worked and what didn't work
*/
- if (try_path_prev != NULL) {
+ if (try_path_prev != nullptr) {
/* If we risk to concatenate two '/', we remove one of them */
if (try_path_prev[strlen(try_path_prev) - 1] == '/' && prev[0] == '/') {
try_path_prev[strlen(try_path_prev) - 1] = '\0';
* path are pointers for the same memory space
*/
cut_path = strdup(prev);
- if (cut_path == NULL) {
+ if (cut_path == nullptr) {
PERROR("strdup");
goto error;
}
/* Concatenate the strings */
- snprintf(resolved_path, LTTNG_PATH_MAX, "%s%s",
- try_path_prev, cut_path);
+ snprintf(resolved_path, LTTNG_PATH_MAX, "%s%s", try_path_prev, cut_path);
/* Free the allocated memory */
free(cut_path);
free(try_path_prev);
- cut_path = NULL;
- try_path_prev = NULL;
- /*
- * Else, we just copy the path in our resolved_path to
- * return it as is
- */
+ cut_path = nullptr;
+ try_path_prev = nullptr;
+ /*
+ * Else, we just copy the path in our resolved_path to
+ * return it as is
+ */
} else {
strncpy(resolved_path, path, LTTNG_PATH_MAX);
}
if (try_path_prev != try_path) {
free(try_path_prev);
}
- return NULL;
+ return nullptr;
}
-static
-int expand_double_slashes_dot_and_dotdot(char *path)
+static int expand_double_slashes_dot_and_dotdot(char *path)
{
size_t expanded_path_len, path_len;
const char *curr_char, *path_last_char, *next_slash, *prev_slash;
}
next_slash = (const char *) memchr(curr_char, '/', path_last_char - curr_char);
- if (next_slash == NULL) {
+ if (next_slash == nullptr) {
/* Reached the end of the provided path. */
next_slash = path_last_char;
}
/* Compute how long is the previous token. */
curr_token_len = next_slash - curr_char;
- switch(curr_token_len) {
+ switch (curr_token_len) {
case 0:
/*
* The pointer has not move meaning that curr_char is
* previous forward slash and substract that
* len to the resulting path.
*/
- prev_slash = (const char *) lttng_memrchr(path, '/', expanded_path_len);
+ prev_slash =
+ (const char *) lttng_memrchr(path, '/', expanded_path_len);
/*
* If prev_slash is NULL, we reached the
* beginning of the path. We can't go back any
* further.
*/
- if (prev_slash != NULL) {
+ if (prev_slash != nullptr) {
expanded_path_len = prev_slash - path;
}
continue;
* The returned string was allocated in the function, it is thus of
* the responsibility of the caller to free this memory.
*/
-static
-char *_utils_expand_path(const char *path, bool keep_symlink)
+static char *_utils_expand_path(const char *path, bool keep_symlink)
{
int ret;
- char *absolute_path = NULL;
+ char *absolute_path = nullptr;
char *last_token;
bool is_dot, is_dotdot;
/* Safety net */
- if (path == NULL) {
+ if (path == nullptr) {
goto error;
}
/* Allocate memory for the absolute_path */
- absolute_path = (char *) zmalloc(LTTNG_PATH_MAX);
- if (absolute_path == NULL) {
+ absolute_path = zmalloc<char>(LTTNG_PATH_MAX);
+ if (absolute_path == nullptr) {
PERROR("zmalloc expand path");
goto error;
}
* Get the number of character in the CWD and allocate an array
* to can hold it and the path provided by the caller.
*/
- ret = snprintf(absolute_path, LTTNG_PATH_MAX, "%s/%s",
- current_working_dir, path);
+ ret = snprintf(absolute_path, LTTNG_PATH_MAX, "%s/%s", current_working_dir, path);
if (ret >= LTTNG_PATH_MAX) {
ERR("Concatenating current working directory %s and path %s exceeds maximal size of %i bytes",
- current_working_dir, path, LTTNG_PATH_MAX);
+ current_working_dir,
+ path,
+ LTTNG_PATH_MAX);
goto error;
}
}
error:
free(absolute_path);
- return NULL;
+ return nullptr;
}
char *utils_expand_path(const char *path)
{