Back to home page

OSCL-LXR

 
 

    


0001 // SPDX-License-Identifier: GPL-2.0
0002 /*
0003  * I'm tired of doing "vsnprintf()" etc just to open a
0004  * file, so here's a "return static buffer with printf"
0005  * interface for paths.
0006  *
0007  * It's obviously not thread-safe. Sue me. But it's quite
0008  * useful for doing things like
0009  *
0010  *   f = open(mkpath("%s/%s.perf", base, name), O_RDONLY);
0011  *
0012  * which is what it's designed for.
0013  */
0014 #include "path.h"
0015 #include "cache.h"
0016 #include <linux/kernel.h>
0017 #include <limits.h>
0018 #include <stdio.h>
0019 #include <string.h>
0020 #include <sys/types.h>
0021 #include <sys/stat.h>
0022 #include <dirent.h>
0023 #include <unistd.h>
0024 
0025 static char bad_path[] = "/bad-path/";
0026 /*
0027  * One hack:
0028  */
0029 static char *get_pathname(void)
0030 {
0031     static char pathname_array[4][PATH_MAX];
0032     static int idx;
0033 
0034     return pathname_array[3 & ++idx];
0035 }
0036 
0037 static char *cleanup_path(char *path)
0038 {
0039     /* Clean it up */
0040     if (!memcmp(path, "./", 2)) {
0041         path += 2;
0042         while (*path == '/')
0043             path++;
0044     }
0045     return path;
0046 }
0047 
0048 char *mkpath(const char *fmt, ...)
0049 {
0050     va_list args;
0051     unsigned len;
0052     char *pathname = get_pathname();
0053 
0054     va_start(args, fmt);
0055     len = vsnprintf(pathname, PATH_MAX, fmt, args);
0056     va_end(args);
0057     if (len >= PATH_MAX)
0058         return bad_path;
0059     return cleanup_path(pathname);
0060 }
0061 
0062 int path__join(char *bf, size_t size, const char *path1, const char *path2)
0063 {
0064     return scnprintf(bf, size, "%s%s%s", path1, path1[0] ? "/" : "", path2);
0065 }
0066 
0067 int path__join3(char *bf, size_t size, const char *path1, const char *path2, const char *path3)
0068 {
0069     return scnprintf(bf, size, "%s%s%s%s%s", path1, path1[0] ? "/" : "",
0070              path2, path2[0] ? "/" : "", path3);
0071 }
0072 
0073 bool is_regular_file(const char *file)
0074 {
0075     struct stat st;
0076 
0077     if (stat(file, &st))
0078         return false;
0079 
0080     return S_ISREG(st.st_mode);
0081 }
0082 
0083 /* Helper function for filesystems that return a dent->d_type DT_UNKNOWN */
0084 bool is_directory(const char *base_path, const struct dirent *dent)
0085 {
0086     char path[PATH_MAX];
0087     struct stat st;
0088 
0089     snprintf(path, sizeof(path), "%s/%s", base_path, dent->d_name);
0090     if (stat(path, &st))
0091         return false;
0092 
0093     return S_ISDIR(st.st_mode);
0094 }
0095 
0096 bool is_executable_file(const char *base_path, const struct dirent *dent)
0097 {
0098     char path[PATH_MAX];
0099     struct stat st;
0100 
0101     snprintf(path, sizeof(path), "%s/%s", base_path, dent->d_name);
0102     if (stat(path, &st))
0103         return false;
0104 
0105     return !S_ISDIR(st.st_mode) && (st.st_mode & S_IXUSR);
0106 }