string.c 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. /*
  2. * libdpkg - Debian packaging suite library routines
  3. * string.c - string handling routines
  4. *
  5. * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
  6. * Copyright © 2008-2015 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. * Print formatted output to an allocated string.
  48. *
  49. * @param fmt The format string.
  50. * @param ... The format arguments.
  51. *
  52. * @return The new allocated formatted output string (never NULL).
  53. */
  54. char *
  55. str_fmt(const char *fmt, ...)
  56. {
  57. va_list args;
  58. char *str;
  59. va_start(args, fmt);
  60. m_vasprintf(&str, fmt, args);
  61. va_end(args);
  62. return str;
  63. }
  64. /**
  65. * Escape format characters from a string.
  66. *
  67. * @param dst The destination string.
  68. * @param src The source string.
  69. * @param n The size of the destination buffer.
  70. *
  71. * @return The end of the destination string.
  72. */
  73. char *
  74. str_escape_fmt(char *dst, const char *src, size_t n)
  75. {
  76. char *d = dst;
  77. const char *s = src;
  78. if (n == 0)
  79. return d;
  80. while (*s) {
  81. if (*s == '%') {
  82. if (n-- <= 2)
  83. break;
  84. *d++ = '%';
  85. }
  86. if (n-- <= 1)
  87. break;
  88. *d++ = *s++;
  89. }
  90. *d = '\0';
  91. return d;
  92. }
  93. /**
  94. * Quote shell metacharacters in a string.
  95. *
  96. * This function allows passing strings to commands without splitting the
  97. * arguments, like in system(3)
  98. *
  99. * @param src The source string to escape.
  100. *
  101. * @return The new allocated string (never NULL).
  102. */
  103. char *
  104. str_quote_meta(const char *src)
  105. {
  106. char *new_dst, *dst;
  107. new_dst = dst = m_malloc(strlen(src) * 2);
  108. while (*src) {
  109. if (!c_isdigit(*src) && !c_isalpha(*src))
  110. *dst++ = '\\';
  111. *dst++ = *src++;
  112. }
  113. *dst = '\0';
  114. return new_dst;
  115. }
  116. /**
  117. * Check and strip possible surrounding quotes in string.
  118. *
  119. * @param str The string to act on.
  120. *
  121. * @return A pointer to str or NULL if the quotes were unbalanced.
  122. */
  123. char *
  124. str_strip_quotes(char *str)
  125. {
  126. if (str[0] == '"' || str[0] == '\'') {
  127. size_t str_len = strlen(str);
  128. if (str[0] != str[str_len - 1])
  129. return NULL;
  130. /* Remove surrounding quotes. */
  131. str[str_len - 1] = '\0';
  132. str++;
  133. }
  134. return str;
  135. }