path.c 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. static char *get_pathname(void)
  23. {
  24. static char pathname_array[4][PATH_MAX];
  25. static int idx;
  26. return pathname_array[3 & ++idx];
  27. }
  28. static char *cleanup_path(char *path)
  29. {
  30. /* Clean it up */
  31. if (!memcmp(path, "./", 2)) {
  32. path += 2;
  33. while (*path == '/')
  34. path++;
  35. }
  36. return path;
  37. }
  38. static char *perf_vsnpath(char *buf, size_t n, const char *fmt, va_list args)
  39. {
  40. const char *perf_dir = get_perf_dir();
  41. size_t len;
  42. len = strlen(perf_dir);
  43. if (n < len + 1)
  44. goto bad;
  45. memcpy(buf, perf_dir, len);
  46. if (len && !is_dir_sep(perf_dir[len-1]))
  47. buf[len++] = '/';
  48. len += vsnprintf(buf + len, n - len, fmt, args);
  49. if (len >= n)
  50. goto bad;
  51. return cleanup_path(buf);
  52. bad:
  53. strlcpy(buf, bad_path, n);
  54. return buf;
  55. }
  56. char *perf_pathdup(const char *fmt, ...)
  57. {
  58. char path[PATH_MAX];
  59. va_list args;
  60. va_start(args, fmt);
  61. (void)perf_vsnpath(path, sizeof(path), fmt, args);
  62. va_end(args);
  63. return xstrdup(path);
  64. }
  65. char *mkpath(const char *fmt, ...)
  66. {
  67. va_list args;
  68. unsigned len;
  69. char *pathname = get_pathname();
  70. va_start(args, fmt);
  71. len = vsnprintf(pathname, PATH_MAX, fmt, args);
  72. va_end(args);
  73. if (len >= PATH_MAX)
  74. return bad_path;
  75. return cleanup_path(pathname);
  76. }
  77. char *perf_path(const char *fmt, ...)
  78. {
  79. const char *perf_dir = get_perf_dir();
  80. char *pathname = get_pathname();
  81. va_list args;
  82. unsigned len;
  83. len = strlen(perf_dir);
  84. if (len > PATH_MAX-100)
  85. return bad_path;
  86. memcpy(pathname, perf_dir, len);
  87. if (len && perf_dir[len-1] != '/')
  88. pathname[len++] = '/';
  89. va_start(args, fmt);
  90. len += vsnprintf(pathname + len, PATH_MAX - len, fmt, args);
  91. va_end(args);
  92. if (len >= PATH_MAX)
  93. return bad_path;
  94. return cleanup_path(pathname);
  95. }
  96. /* strip arbitrary amount of directory separators at end of path */
  97. static inline int chomp_trailing_dir_sep(const char *path, int len)
  98. {
  99. while (len && is_dir_sep(path[len - 1]))
  100. len--;
  101. return len;
  102. }
  103. /*
  104. * If path ends with suffix (complete path components), returns the
  105. * part before suffix (sans trailing directory separators).
  106. * Otherwise returns NULL.
  107. */
  108. char *strip_path_suffix(const char *path, const char *suffix)
  109. {
  110. int path_len = strlen(path), suffix_len = strlen(suffix);
  111. while (suffix_len) {
  112. if (!path_len)
  113. return NULL;
  114. if (is_dir_sep(path[path_len - 1])) {
  115. if (!is_dir_sep(suffix[suffix_len - 1]))
  116. return NULL;
  117. path_len = chomp_trailing_dir_sep(path, path_len);
  118. suffix_len = chomp_trailing_dir_sep(suffix, suffix_len);
  119. }
  120. else if (path[--path_len] != suffix[--suffix_len])
  121. return NULL;
  122. }
  123. if (path_len && !is_dir_sep(path[path_len - 1]))
  124. return NULL;
  125. return strndup(path, chomp_trailing_dir_sep(path, path_len));
  126. }