Clean-up: declare variables at beginning of scope
[lttng-tools.git] / src / common / utils.c
index e3f1bf7e62ad8109db5cbefd211f81484f02d737..ee53477de559fedaf43eabfa194ee3255e58b293 100644 (file)
@@ -36,6 +36,7 @@
 #include <common/compat/getenv.h>
 #include <common/compat/string.h>
 #include <common/compat/dirent.h>
+#include <lttng/constant.h>
 
 #include "utils.h"
 #include "defaults.h"
@@ -81,6 +82,8 @@ char *utils_partial_realpath(const char *path, char *resolved_path, size_t size)
 
        /* Resolve the canonical path of the first part of the path */
        while (try_path != NULL && next != end) {
+               char *try_path_buf = NULL;
+
                /*
                 * If there is not any '/' left, we want to try with
                 * the full path
@@ -97,9 +100,16 @@ char *utils_partial_realpath(const char *path, char *resolved_path, size_t size)
                        goto error;
                }
 
+               try_path_buf = zmalloc(LTTNG_PATH_MAX);
+               if (!try_path_buf) {
+                       PERROR("zmalloc");
+                       goto error;
+               }
+
                /* Try to resolve this part */
-               try_path = realpath((char *)cut_path, NULL);
+               try_path = realpath((char *) cut_path, try_path_buf);
                if (try_path == NULL) {
+                       free(try_path_buf);
                        /*
                         * There was an error, we just want to be assured it
                         * is linked to an unexistent directory, if it's another
@@ -116,6 +126,7 @@ char *utils_partial_realpath(const char *path, char *resolved_path, size_t size)
                        }
                } else {
                        /* Save the place we are before trying the next step */
+                       try_path_buf = NULL;
                        free(try_path_prev);
                        try_path_prev = try_path;
                        prev = next;
@@ -479,7 +490,7 @@ int utils_create_pid_file(pid_t pid, const char *filepath)
                goto error;
        }
 
-       ret = fprintf(fp, "%d\n", pid);
+       ret = fprintf(fp, "%d\n", (int) pid);
        if (ret < 0) {
                PERROR("fprintf pid file");
                goto error;
@@ -488,7 +499,7 @@ int utils_create_pid_file(pid_t pid, const char *filepath)
        if (fclose(fp)) {
                PERROR("fclose");
        }
-       DBG("Pid %d written in file %s", pid, filepath);
+       DBG("Pid %d written in file %s", (int) pid, filepath);
        ret = 0;
 error:
        return ret;
@@ -1220,14 +1231,15 @@ int utils_recursive_rmdir(const char *path)
 
        path_len = strlen(path);
        while ((entry = readdir(dir))) {
-               if (!strcmp(entry->d_name, ".")
-                               || !strcmp(entry->d_name, ".."))
-                       continue;
-
                struct stat st;
                size_t name_len;
                char filename[PATH_MAX];
 
+               if (!strcmp(entry->d_name, ".")
+                               || !strcmp(entry->d_name, "..")) {
+                       continue;
+               }
+
                name_len = strlen(entry->d_name);
                if (path_len + name_len + 2 > sizeof(filename)) {
                        ERR("Failed to remove file: path name too long (%s/%s)",
This page took 0.024488 seconds and 4 git commands to generate.