path.c 3.4 KB

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