path.c 2.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113
  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. char *mkpath(const char *fmt, ...)
  39. {
  40. va_list args;
  41. unsigned len;
  42. char *pathname = get_pathname();
  43. va_start(args, fmt);
  44. len = vsnprintf(pathname, PATH_MAX, fmt, args);
  45. va_end(args);
  46. if (len >= PATH_MAX)
  47. return bad_path;
  48. return cleanup_path(pathname);
  49. }
  50. char *perf_path(const char *fmt, ...)
  51. {
  52. const char *perf_dir = get_perf_dir();
  53. char *pathname = get_pathname();
  54. va_list args;
  55. unsigned len;
  56. len = strlen(perf_dir);
  57. if (len > PATH_MAX-100)
  58. return bad_path;
  59. memcpy(pathname, perf_dir, len);
  60. if (len && perf_dir[len-1] != '/')
  61. pathname[len++] = '/';
  62. va_start(args, fmt);
  63. len += vsnprintf(pathname + len, PATH_MAX - len, fmt, args);
  64. va_end(args);
  65. if (len >= PATH_MAX)
  66. return bad_path;
  67. return cleanup_path(pathname);
  68. }
  69. /* strip arbitrary amount of directory separators at end of path */
  70. static inline int chomp_trailing_dir_sep(const char *path, int len)
  71. {
  72. while (len && is_dir_sep(path[len - 1]))
  73. len--;
  74. return len;
  75. }
  76. /*
  77. * If path ends with suffix (complete path components), returns the
  78. * part before suffix (sans trailing directory separators).
  79. * Otherwise returns NULL.
  80. */
  81. char *strip_path_suffix(const char *path, const char *suffix)
  82. {
  83. int path_len = strlen(path), suffix_len = strlen(suffix);
  84. while (suffix_len) {
  85. if (!path_len)
  86. return NULL;
  87. if (is_dir_sep(path[path_len - 1])) {
  88. if (!is_dir_sep(suffix[suffix_len - 1]))
  89. return NULL;
  90. path_len = chomp_trailing_dir_sep(path, path_len);
  91. suffix_len = chomp_trailing_dir_sep(suffix, suffix_len);
  92. }
  93. else if (path[--path_len] != suffix[--suffix_len])
  94. return NULL;
  95. }
  96. if (path_len && !is_dir_sep(path[path_len - 1]))
  97. return NULL;
  98. return strndup(path, chomp_trailing_dir_sep(path, path_len));
  99. }