path.c 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  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-2011 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/path.h>
  28. /**
  29. * Trim ‘/’ and ‘/.’ from the end of a pathname.
  30. *
  31. * The given string will get NUL-terminatd.
  32. *
  33. * @param path The pathname to trim.
  34. *
  35. * @return The size of the trimmed pathname.
  36. */
  37. size_t
  38. path_trim_slash_slashdot(char *path)
  39. {
  40. char *end;
  41. if (!path || !*path)
  42. return 0;
  43. for (end = path + strlen(path) - 1; end - path >= 1; end--) {
  44. if (*end == '/' || (*(end - 1) == '/' && *end == '.'))
  45. *end = '\0';
  46. else
  47. break;
  48. }
  49. return end - path + 1;
  50. }
  51. /**
  52. * Skip ‘/’ and ‘./’ from the beginning of a pathname.
  53. *
  54. * @param path The pathname to skip.
  55. *
  56. * @return The new beginning of the pathname.
  57. */
  58. const char *
  59. path_skip_slash_dotslash(const char *path)
  60. {
  61. while (path[0] == '/' || (path[0] == '.' && path[1] == '/'))
  62. path++;
  63. return path;
  64. }
  65. /**
  66. * Return the last component of a pathname.
  67. *
  68. * @param path The pathname to get the base name from.
  69. *
  70. * @return A pointer to the last component inside pathname.
  71. */
  72. const char *
  73. path_basename(const char *path)
  74. {
  75. const char *last_slash;
  76. last_slash = strrchr(path, '/');
  77. if (last_slash == NULL)
  78. return path;
  79. else
  80. return last_slash + 1;
  81. }
  82. /**
  83. * Create a template for a temporary pathname.
  84. *
  85. * @param suffix The suffix to use for the template string.
  86. *
  87. * @return An allocated string with the created template.
  88. */
  89. char *
  90. path_make_temp_template(const char *suffix)
  91. {
  92. const char *tmpdir;
  93. char *template;
  94. tmpdir = getenv("TMPDIR");
  95. #ifdef P_tmpdir
  96. if (!tmpdir)
  97. tmpdir = P_tmpdir;
  98. #endif
  99. if (!tmpdir)
  100. tmpdir = "/tmp";
  101. m_asprintf(&template, "%s/%s.XXXXXX", tmpdir, suffix);
  102. return template;
  103. }
  104. /**
  105. * Escape characters in a pathname for safe locale printing.
  106. *
  107. * We need to quote paths so that they do not cause problems when printing
  108. * them, for example with snprintf(3) which does not work if the format
  109. * string contains %s and an argument has invalid characters for the
  110. * current locale, it will then return -1.
  111. *
  112. * To simplify things, we just escape all 8 bit characters, instead of
  113. * just invalid characters.
  114. *
  115. * @param dst The escaped destination string.
  116. * @param src The source string to escape.
  117. * @param n The size of the destination buffer.
  118. *
  119. * @return The destination string.
  120. */
  121. char *
  122. path_quote_filename(char *dst, const char *src, size_t n)
  123. {
  124. char *r = dst;
  125. ssize_t size = (ssize_t)n;
  126. if (size == 0)
  127. return r;
  128. while (*src) {
  129. if (*src == '\\') {
  130. size -= 2;
  131. if (size <= 0)
  132. break;
  133. *dst++ = '\\';
  134. *dst++ = '\\';
  135. src++;
  136. } else if (((*src) & 0x80) == '\0') {
  137. size--;
  138. if (size <= 0)
  139. break;
  140. *dst++ = *src++;
  141. } else {
  142. size -= 4;
  143. if (size <= 0)
  144. break;
  145. sprintf(dst, "\\%03o",
  146. *(const unsigned char *)src);
  147. dst += 4;
  148. src++;
  149. }
  150. }
  151. *dst = '\0';
  152. return r;
  153. }