path.c 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  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-2010 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. * Create a template for a temporary pathname.
  67. *
  68. * @param suffix The suffix to use for the template string.
  69. *
  70. * @return An allocated string with the created template.
  71. */
  72. char *
  73. path_make_temp_template(const char *suffix)
  74. {
  75. const char *tmpdir;
  76. char *template;
  77. tmpdir = getenv("TMPDIR");
  78. #ifdef P_tmpdir
  79. if (!tmpdir)
  80. tmpdir = P_tmpdir;
  81. #endif
  82. if (!tmpdir)
  83. tmpdir = "/tmp";
  84. m_asprintf(&template, "%s/%s.XXXXXX", tmpdir, suffix);
  85. return template;
  86. }
  87. /**
  88. * Escape characters in a pathname for safe locale printing.
  89. *
  90. * We need to quote paths so that they do not cause problems when printing
  91. * them, for example with snprintf(3) which does not work if the format
  92. * string contains %s and an argument has invalid characters for the
  93. * current locale, it will then return -1.
  94. *
  95. * To simplify things, we just escape all 8 bit characters, instead of
  96. * just invalid characters.
  97. *
  98. * @param dst The escaped destination string.
  99. * @param src The source string to escape.
  100. * @param n The size of the destination buffer.
  101. *
  102. * @return The destination string.
  103. */
  104. char *
  105. path_quote_filename(char *dst, const char *src, size_t n)
  106. {
  107. char *r = dst;
  108. ssize_t size = (ssize_t)n;
  109. if (size == 0)
  110. return r;
  111. while (*src) {
  112. if (*src == '\\') {
  113. size -= 2;
  114. if (size <= 0)
  115. break;
  116. *dst++ = '\\';
  117. *dst++ = '\\';
  118. src++;
  119. } else if (((*src) & 0x80) == '\0') {
  120. size--;
  121. if (size <= 0)
  122. break;
  123. *dst++ = *src++;
  124. } else {
  125. size -= 4;
  126. if (size <= 0)
  127. break;
  128. sprintf(dst, "\\%03o",
  129. *(const unsigned char *)src);
  130. dst += 4;
  131. src++;
  132. }
  133. }
  134. *dst = '\0';
  135. return r;
  136. }