1 // SPDX-License-Identifier: GPL-2.0
4 #include <linux/kernel.h>
13 static char *cleanup_path(char *path)
16 if (!memcmp(path, "./", 2)) {
24 char *mkpath(char *path_buf, size_t sz, const char *fmt, ...)
30 len = vsnprintf(path_buf, sz, fmt, args);
33 strncpy(path_buf, "/bad-path/", sz);
34 return cleanup_path(path_buf);
37 int path__join(char *bf, size_t size, const char *path1, const char *path2)
39 return scnprintf(bf, size, "%s%s%s", path1, path1[0] ? "/" : "", path2);
42 int path__join3(char *bf, size_t size, const char *path1, const char *path2, const char *path3)
44 return scnprintf(bf, size, "%s%s%s%s%s", path1, path1[0] ? "/" : "",
45 path2, path2[0] ? "/" : "", path3);
48 bool is_regular_file(const char *file)
55 return S_ISREG(st.st_mode);
58 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
59 bool is_directory(const char *base_path, const struct dirent *dent)
64 snprintf(path, sizeof(path), "%s/%s", base_path, dent->d_name);
68 return S_ISDIR(st.st_mode);
71 bool is_executable_file(const char *base_path, const struct dirent *dent)
76 snprintf(path, sizeof(path), "%s/%s", base_path, dent->d_name);
80 return !S_ISDIR(st.st_mode) && (st.st_mode & S_IXUSR);