path.c 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * path.c - path handling functions
  4. *
  5. * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
  6. * Copyright © 2008-2012 Guillem Jover <guillem@debian.org>
  7. *
  8. * This is free software; you can redistribute it and/or modify
  9. * it under the terms of the GNU General Public License as published by
  10. * the Free Software Foundation; either version 2 of the License, or
  11. * (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful,
  14. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. * GNU General Public License for more details.
  17. *
  18. * You should have received a copy of the GNU General Public License
  19. * along with this program. If not, see <https://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <stdlib.h>
  24. #include <string.h>
  25. #include <stdio.h>
  26. #include <dpkg/dpkg.h>
  27. #include <dpkg/string.h>
  28. #include <dpkg/path.h>
  29. /**
  30. * Trim ‘/’ and ‘/.’ from the end of a pathname.
  31. *
  32. * The given string will get NUL-terminatd.
  33. *
  34. * @param path The pathname to trim.
  35. *
  36. * @return The size of the trimmed pathname.
  37. */
  38. size_t
  39. path_trim_slash_slashdot(char *path)
  40. {
  41. char *end;
  42. if (str_is_unset(path))
  43. return 0;
  44. for (end = path + strlen(path) - 1; end - path >= 1; end--) {
  45. if (*end == '/' || (*(end - 1) == '/' && *end == '.'))
  46. *end = '\0';
  47. else
  48. break;
  49. }
  50. return end - path + 1;
  51. }
  52. /**
  53. * Skip ‘/’ and ‘./’ from the beginning of a pathname.
  54. *
  55. * @param path The pathname to skip.
  56. *
  57. * @return The new beginning of the pathname.
  58. */
  59. const char *
  60. path_skip_slash_dotslash(const char *path)
  61. {
  62. while (path[0] == '/' || (path[0] == '.' && path[1] == '/'))
  63. path++;
  64. return path;
  65. }
  66. /**
  67. * Return the last component of a pathname.
  68. *
  69. * @param path The pathname to get the base name from.
  70. *
  71. * @return A pointer to the last component inside pathname.
  72. */
  73. const char *
  74. path_basename(const char *path)
  75. {
  76. const char *last_slash;
  77. last_slash = strrchr(path, '/');
  78. if (last_slash == NULL)
  79. return path;
  80. else
  81. return last_slash + 1;
  82. }
  83. /**
  84. * Create a template for a temporary pathname.
  85. *
  86. * @param suffix The suffix to use for the template string.
  87. *
  88. * @return An allocated string with the created template.
  89. */
  90. char *
  91. path_make_temp_template(const char *suffix)
  92. {
  93. const char *tmpdir;
  94. tmpdir = getenv("TMPDIR");
  95. if (!tmpdir)
  96. tmpdir = P_tmpdir;
  97. return str_fmt("%s/%s.XXXXXX", tmpdir, suffix);
  98. }
  99. /**
  100. * Escape characters in a pathname for safe locale printing.
  101. *
  102. * We need to quote paths so that they do not cause problems when printing
  103. * them, for example with snprintf(3) which does not work if the format
  104. * string contains %s and an argument has invalid characters for the
  105. * current locale, it will then return -1.
  106. *
  107. * To simplify things, we just escape all 8 bit characters, instead of
  108. * just invalid characters.
  109. *
  110. * @param dst The escaped destination string.
  111. * @param src The source string to escape.
  112. * @param n The size of the destination buffer.
  113. *
  114. * @return The destination string.
  115. */
  116. char *
  117. path_quote_filename(char *dst, const char *src, size_t n)
  118. {
  119. char *r = dst;
  120. ssize_t size = (ssize_t)n;
  121. if (size == 0)
  122. return r;
  123. while (*src) {
  124. if (*src == '\\') {
  125. size -= 2;
  126. if (size <= 0)
  127. break;
  128. *dst++ = '\\';
  129. *dst++ = '\\';
  130. src++;
  131. } else if (((*src) & 0x80) == '\0') {
  132. size--;
  133. if (size <= 0)
  134. break;
  135. *dst++ = *src++;
  136. } else {
  137. size -= 4;
  138. if (size <= 0)
  139. break;
  140. sprintf(dst, "\\%03o",
  141. *(const unsigned char *)src);
  142. dst += 4;
  143. src++;
  144. }
  145. }
  146. *dst = '\0';
  147. return r;
  148. }