path.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161
  1. /*
  2. * I'm tired of doing "vsnprintf()" etc just to open a
  3. * file, so here's a "return static buffer with printf"
  4. * interface for paths.
  5. *
  6. * It's obviously not thread-safe. Sue me. But it's quite
  7. * useful for doing things like
  8. *
  9. * f = open(mkpath("%s/%s.perf", base, name), O_RDONLY);
  10. *
  11. * which is what it's designed for.
  12. */
  13. #include "cache.h"
  14. static char bad_path[] = "/bad-path/";
  15. /*
  16. * Two hacks:
  17. */
  18. static const char *get_perf_dir(void)
  19. {
  20. return ".";
  21. }
  22. /*
  23. * If libc has strlcpy() then that version will override this
  24. * implementation:
  25. */
  26. size_t __weak strlcpy(char *dest, const char *src, size_t size)
  27. {
  28. size_t ret = strlen(src);
  29. if (size) {
  30. size_t len = (ret >= size) ? size - 1 : ret;
  31. memcpy(dest, src, len);
  32. dest[len] = '\0';
  33. }
  34. return ret;
  35. }
  36. static char *get_pathname(void)
  37. {
  38. static char pathname_array[4][PATH_MAX];
  39. static int idx;
  40. return pathname_array[3 & ++idx];
  41. }
  42. static char *cleanup_path(char *path)
  43. {
  44. /* Clean it up */
  45. if (!memcmp(path, "./", 2)) {
  46. path += 2;
  47. while (*path == '/')
  48. path++;
  49. }
  50. return path;
  51. }
  52. static char *perf_vsnpath(char *buf, size_t n, const char *fmt, va_list args)
  53. {
  54. const char *perf_dir = get_perf_dir();
  55. size_t len;
  56. len = strlen(perf_dir);
  57. if (n < len + 1)
  58. goto bad;
  59. memcpy(buf, perf_dir, len);
  60. if (len && !is_dir_sep(perf_dir[len-1]))
  61. buf[len++] = '/';
  62. len += vsnprintf(buf + len, n - len, fmt, args);
  63. if (len >= n)
  64. goto bad;
  65. return cleanup_path(buf);
  66. bad:
  67. strlcpy(buf, bad_path, n);
  68. return buf;
  69. }
  70. char *perf_pathdup(const char *fmt, ...)
  71. {
  72. char path[PATH_MAX];
  73. va_list args;
  74. va_start(args, fmt);
  75. (void)perf_vsnpath(path, sizeof(path), fmt, args);
  76. va_end(args);
  77. return xstrdup(path);
  78. }
  79. char *mkpath(const char *fmt, ...)
  80. {
  81. va_list args;
  82. unsigned len;
  83. char *pathname = get_pathname();
  84. va_start(args, fmt);
  85. len = vsnprintf(pathname, PATH_MAX, fmt, args);
  86. va_end(args);
  87. if (len >= PATH_MAX)
  88. return bad_path;
  89. return cleanup_path(pathname);
  90. }
  91. char *perf_path(const char *fmt, ...)
  92. {
  93. const char *perf_dir = get_perf_dir();
  94. char *pathname = get_pathname();
  95. va_list args;
  96. unsigned len;
  97. len = strlen(perf_dir);
  98. if (len > PATH_MAX-100)
  99. return bad_path;
  100. memcpy(pathname, perf_dir, len);
  101. if (len && perf_dir[len-1] != '/')
  102. pathname[len++] = '/';
  103. va_start(args, fmt);
  104. len += vsnprintf(pathname + len, PATH_MAX - len, fmt, args);
  105. va_end(args);
  106. if (len >= PATH_MAX)
  107. return bad_path;
  108. return cleanup_path(pathname);
  109. }
  110. /* strip arbitrary amount of directory separators at end of path */
  111. static inline int chomp_trailing_dir_sep(const char *path, int len)
  112. {
  113. while (len && is_dir_sep(path[len - 1]))
  114. len--;
  115. return len;
  116. }
  117. /*
  118. * If path ends with suffix (complete path components), returns the
  119. * part before suffix (sans trailing directory separators).
  120. * Otherwise returns NULL.
  121. */
  122. char *strip_path_suffix(const char *path, const char *suffix)
  123. {
  124. int path_len = strlen(path), suffix_len = strlen(suffix);
  125. while (suffix_len) {
  126. if (!path_len)
  127. return NULL;
  128. if (is_dir_sep(path[path_len - 1])) {
  129. if (!is_dir_sep(suffix[suffix_len - 1]))
  130. return NULL;
  131. path_len = chomp_trailing_dir_sep(path, path_len);
  132. suffix_len = chomp_trailing_dir_sep(suffix, suffix_len);
  133. }
  134. else if (path[--path_len] != suffix[--suffix_len])
  135. return NULL;
  136. }
  137. if (path_len && !is_dir_sep(path[path_len - 1]))
  138. return NULL;
  139. return strndup(path, chomp_trailing_dir_sep(path, path_len));
  140. }