path.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * path.c - path handling functions
  4. *
  5. * Copyright © 1995 Ian Jackson <ian@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 <http://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. char *template;
  95. tmpdir = getenv("TMPDIR");
  96. #ifdef P_tmpdir
  97. if (!tmpdir)
  98. tmpdir = P_tmpdir;
  99. #endif
  100. if (!tmpdir)
  101. tmpdir = "/tmp";
  102. m_asprintf(&template, "%s/%s.XXXXXX", tmpdir, suffix);
  103. return template;
  104. }
  105. /**
  106. * Escape characters in a pathname for safe locale printing.
  107. *
  108. * We need to quote paths so that they do not cause problems when printing
  109. * them, for example with snprintf(3) which does not work if the format
  110. * string contains %s and an argument has invalid characters for the
  111. * current locale, it will then return -1.
  112. *
  113. * To simplify things, we just escape all 8 bit characters, instead of
  114. * just invalid characters.
  115. *
  116. * @param dst The escaped destination string.
  117. * @param src The source string to escape.
  118. * @param n The size of the destination buffer.
  119. *
  120. * @return The destination string.
  121. */
  122. char *
  123. path_quote_filename(char *dst, const char *src, size_t n)
  124. {
  125. char *r = dst;
  126. ssize_t size = (ssize_t)n;
  127. if (size == 0)
  128. return r;
  129. while (*src) {
  130. if (*src == '\\') {
  131. size -= 2;
  132. if (size <= 0)
  133. break;
  134. *dst++ = '\\';
  135. *dst++ = '\\';
  136. src++;
  137. } else if (((*src) & 0x80) == '\0') {
  138. size--;
  139. if (size <= 0)
  140. break;
  141. *dst++ = *src++;
  142. } else {
  143. size -= 4;
  144. if (size <= 0)
  145. break;
  146. sprintf(dst, "\\%03o",
  147. *(const unsigned char *)src);
  148. dst += 4;
  149. src++;
  150. }
  151. }
  152. *dst = '\0';
  153. return r;
  154. }