string.c 1.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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
  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 <dpkg/string.h>
  25. char *
  26. str_escape_fmt(char *dst, const char *src)
  27. {
  28. char *d = dst;
  29. const char *s = src;
  30. while (*s) {
  31. if (*s == '%')
  32. *d++ = '%';
  33. *d++ = *s++;
  34. }
  35. *d = '\0';
  36. return d;
  37. }
  38. /* Check and strip possible surrounding quotes in string. */
  39. char *
  40. str_strip_quotes(char *str)
  41. {
  42. if (str[0] == '"' || str[0] == '\'') {
  43. size_t str_len = strlen(str);
  44. if (str[0] != str[str_len - 1])
  45. return NULL;
  46. /* Remove surrounding quotes. */
  47. str[str_len - 1] = '\0';
  48. str++;
  49. }
  50. return str;
  51. }