string.c 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138
  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-2014 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 <https://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <string.h>
  24. #include <dpkg/c-ctype.h>
  25. #include <dpkg/string.h>
  26. #include <dpkg/dpkg.h>
  27. /**
  28. * Match the end of a string.
  29. *
  30. * @param str The string.
  31. * @param end The end to match in str.
  32. *
  33. * @return Whether the string was matched at the end.
  34. */
  35. bool
  36. str_match_end(const char *str, const char *end)
  37. {
  38. size_t str_len = strlen(str);
  39. size_t end_len = strlen(end);
  40. const char *str_end = str + str_len - end_len;
  41. if (str_len >= end_len && strcmp(str_end, end) == 0)
  42. return true;
  43. else
  44. return false;
  45. }
  46. /**
  47. * Escape format characters from a string.
  48. *
  49. * @param dst The destination string.
  50. * @param src The source string.
  51. * @param n The size of the destination buffer.
  52. *
  53. * @return The end of the destination string.
  54. */
  55. char *
  56. str_escape_fmt(char *dst, const char *src, size_t n)
  57. {
  58. char *d = dst;
  59. const char *s = src;
  60. if (n == 0)
  61. return d;
  62. while (*s) {
  63. if (*s == '%') {
  64. if (n-- <= 2)
  65. break;
  66. *d++ = '%';
  67. }
  68. if (n-- <= 1)
  69. break;
  70. *d++ = *s++;
  71. }
  72. *d = '\0';
  73. return d;
  74. }
  75. /**
  76. * Quote shell metacharacters in a string.
  77. *
  78. * This function allows passing strings to commands without splitting the
  79. * arguments, like in system(3)
  80. *
  81. * @param src The source string to escape.
  82. *
  83. * @return The new allocated string (never NULL).
  84. */
  85. char *
  86. str_quote_meta(const char *src)
  87. {
  88. char *new_dst, *dst;
  89. new_dst = dst = m_malloc(strlen(src) * 2);
  90. while (*src) {
  91. if (!c_isdigit(*src) && !c_isalpha(*src))
  92. *dst++ = '\\';
  93. *dst++ = *src++;
  94. }
  95. *dst = '\0';
  96. return new_dst;
  97. }
  98. /**
  99. * Check and strip possible surrounding quotes in string.
  100. *
  101. * @param str The string to act on.
  102. *
  103. * @return A pointer to str or NULL if the quotes were unbalanced.
  104. */
  105. char *
  106. str_strip_quotes(char *str)
  107. {
  108. if (str[0] == '"' || str[0] == '\'') {
  109. size_t str_len = strlen(str);
  110. if (str[0] != str[str_len - 1])
  111. return NULL;
  112. /* Remove surrounding quotes. */
  113. str[str_len - 1] = '\0';
  114. str++;
  115. }
  116. return str;
  117. }