string.c 2.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * string.c - string handling routines
  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 <string.h>
  24. #include <dpkg/string.h>
  25. #include <dpkg/dpkg.h>
  26. /**
  27. * Escape format characters from a string.
  28. *
  29. * @param dst The destination string.
  30. * @param src The source string.
  31. * @param n The size of the destination buffer.
  32. *
  33. * @return The end of the destination string.
  34. */
  35. char *
  36. str_escape_fmt(char *dst, const char *src, size_t n)
  37. {
  38. char *d = dst;
  39. const char *s = src;
  40. if (n == 0)
  41. return d;
  42. while (*s) {
  43. if (*s == '%') {
  44. if (n-- <= 2)
  45. break;
  46. *d++ = '%';
  47. }
  48. if (n-- <= 1)
  49. break;
  50. *d++ = *s++;
  51. }
  52. *d = '\0';
  53. return d;
  54. }
  55. /**
  56. * Quote shell metacharacters in a string.
  57. *
  58. * This function allows passing strings to commands without splitting the
  59. * arguments, like in system(3)
  60. *
  61. * @param src The source string to escape.
  62. *
  63. * @return The new allocated string (never NULL).
  64. */
  65. char *
  66. str_quote_meta(const char *src)
  67. {
  68. char *new_dst, *dst;
  69. new_dst = dst = m_malloc(strlen(src) * 2);
  70. while (*src) {
  71. if (!cisdigit(*src) && !cisalpha(*src))
  72. *dst++ = '\\';
  73. *dst++ = *src++;
  74. }
  75. *dst = '\0';
  76. return new_dst;
  77. }
  78. /**
  79. * Check and strip possible surrounding quotes in string.
  80. *
  81. * @param str The string to act on.
  82. *
  83. * @return A pointer to str or NULL if the quotes were unbalanced.
  84. */
  85. char *
  86. str_strip_quotes(char *str)
  87. {
  88. if (str[0] == '"' || str[0] == '\'') {
  89. size_t str_len = strlen(str);
  90. if (str[0] != str[str_len - 1])
  91. return NULL;
  92. /* Remove surrounding quotes. */
  93. str[str_len - 1] = '\0';
  94. str++;
  95. }
  96. return str;
  97. }