path.c 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  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, 2009 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/varbuf.h>
  28. #include <dpkg/path.h>
  29. size_t
  30. path_rtrim_slash_slashdot(char *path)
  31. {
  32. char *end;
  33. if (!path || !*path)
  34. return 0;
  35. for (end = path + strlen(path) - 1; end - path >= 1; end--) {
  36. if (*end == '/' || (*(end - 1) == '/' && *end == '.'))
  37. *end = '\0';
  38. else
  39. break;
  40. }
  41. return end - path + 1;
  42. }
  43. const char *
  44. path_skip_slash_dotslash(const char *path)
  45. {
  46. while (path[0] == '/' || (path[0] == '.' && path[1] == '/'))
  47. path++;
  48. return path;
  49. }
  50. /**
  51. * Create a template for a temporary pathname.
  52. *
  53. * @param suffix The suffix to use for the template string.
  54. *
  55. * @return An allocated string with the created template.
  56. */
  57. char *
  58. path_make_temp_template(const char *suffix)
  59. {
  60. const char *tmpdir;
  61. struct varbuf template = VARBUF_INIT;
  62. tmpdir = getenv("TMPDIR");
  63. #ifdef P_tmpdir
  64. if (!tmpdir)
  65. tmpdir = P_tmpdir;
  66. #endif
  67. if (!tmpdir)
  68. tmpdir = "/tmp";
  69. varbufprintf(&template, "%s/%s.XXXXXX", tmpdir, suffix);
  70. return varbuf_detach(&template);
  71. }
  72. /*
  73. * snprintf(3) doesn't work if format contains %.<nnn>s and an argument has
  74. * invalid char for locale, then it returns -1.
  75. * ohshite() is ok, but fd_fd_copy(), which is used in tarobject(), is not
  76. * ok, because:
  77. *
  78. * - fd_fd_copy() == buffer_copy_TYPE() ‘lib/dpkg/buffer.h’.
  79. * - buffer_copy_TYPE() uses varbufvprintf(&v, desc, al); ‘lib/dpkg/buffer.c’.
  80. * - varbufvprintf() fails, because it calls with:
  81. * fmt = "backend dpkg-deb during '%.255s'"
  82. * arg may contain some invalid char, for example,
  83. * «/usr/share/doc/console-tools/examples/unicode/\342\231\252\342\231\254»
  84. * in console-tools.
  85. *
  86. * In this case, if the user uses some locale which doesn't support
  87. * “\342\231...”, vsnprintf() always returns -1 and varbufextend() fails.
  88. *
  89. * So, we need to escape invalid char, probably as in
  90. * ‘tar-1.13.19/lib/quotearg.c: quotearg_buffer_restyled()’
  91. * but here we escape all 8 bit chars, in order make it simple.
  92. */
  93. char *
  94. path_quote_filename(char *dst, const char *src, size_t size)
  95. {
  96. char *r = dst;
  97. while (size > 0) {
  98. switch (*src) {
  99. case '\0':
  100. *dst = '\0';
  101. return r;
  102. case '\\':
  103. *dst++ = '\\';
  104. *dst++ = '\\';
  105. size -= 2;
  106. break;
  107. default:
  108. if (((*src) & 0x80) == '\0') {
  109. *dst++ = *src++;
  110. --size;
  111. } else {
  112. if (size > 4) {
  113. sprintf(dst, "\\%03o",
  114. *(const unsigned char *)src);
  115. size -= 4;
  116. dst += 4;
  117. src++;
  118. } else {
  119. /* Buffer full. */
  120. *dst = '\0'; /* XXX */
  121. return r;
  122. }
  123. }
  124. }
  125. }
  126. *dst = '\0'; /* XXX */
  127. return r;
  128. }