path.c 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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 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
  10. * published by the Free Software Foundation; either version 2,
  11. * or (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful, but
  14. * 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 <string.h>
  24. #include <stdio.h>
  25. #include <dpkg/path.h>
  26. size_t
  27. path_rtrim_slash_slashdot(char *path)
  28. {
  29. char *end;
  30. if (!path || !*path)
  31. return 0;
  32. for (end = path + strlen(path) - 1; end - path >= 1; end--) {
  33. if (*end == '/' || (*(end - 1) == '/' && *end == '.'))
  34. *end = '\0';
  35. else
  36. break;
  37. }
  38. return end - path + 1;
  39. }
  40. const char *
  41. path_skip_slash_dotslash(const char *path)
  42. {
  43. while (path[0] == '/' || (path[0] == '.' && path[1] == '/'))
  44. path++;
  45. return path;
  46. }
  47. /*
  48. * snprintf(3) doesn't work if format contains %.<nnn>s and an argument has
  49. * invalid char for locale, then it returns -1.
  50. * ohshite() is ok, but fd_fd_copy(), which is used in tarobject(), is not
  51. * ok, because:
  52. *
  53. * - fd_fd_copy() == buffer_copy_TYPE() ‘lib/dpkg/buffer.h’.
  54. * - buffer_copy_TYPE() uses varbufvprintf(&v, desc, al); ‘lib/dpkg/buffer.c’.
  55. * - varbufvprintf() fails, because it calls with:
  56. * fmt = "backend dpkg-deb during '%.255s'"
  57. * arg may contain some invalid char, for example,
  58. * «/usr/share/doc/console-tools/examples/unicode/\342\231\252\342\231\254»
  59. * in console-tools.
  60. *
  61. * In this case, if the user uses some locale which doesn't support
  62. * “\342\231...”, vsnprintf() always returns -1 and varbufextend() fails.
  63. *
  64. * So, we need to escape invalid char, probably as in
  65. * ‘tar-1.13.19/lib/quotearg.c: quotearg_buffer_restyled()’
  66. * but here we escape all 8 bit chars, in order make it simple.
  67. */
  68. char *
  69. path_quote_filename(char *dst, const char *src, size_t size)
  70. {
  71. char *r = dst;
  72. while (size > 0) {
  73. switch (*src) {
  74. case '\0':
  75. *dst = '\0';
  76. return r;
  77. case '\\':
  78. *dst++ = '\\';
  79. *dst++ = '\\';
  80. size -= 2;
  81. break;
  82. default:
  83. if (((*src) & 0x80) == '\0') {
  84. *dst++ = *src++;
  85. --size;
  86. } else {
  87. if (size > 4) {
  88. sprintf(dst, "\\%03o",
  89. *(unsigned char *)src);
  90. size -= 4;
  91. dst += 4;
  92. src++;
  93. } else {
  94. /* Buffer full. */
  95. *dst = '\0'; /* XXX */
  96. return r;
  97. }
  98. }
  99. }
  100. }
  101. *dst = '\0'; /* XXX */
  102. return r;
  103. }