string.c 1.4 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465
  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
  19. * License along with dpkg; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <config.h>
  23. #include <compat.h>
  24. #include <string.h>
  25. #include <dpkg/string.h>
  26. char *
  27. str_escape_fmt(char *dst, const char *src)
  28. {
  29. char *d = dst;
  30. const char *s = src;
  31. while (*s) {
  32. if (*s == '%')
  33. *d++ = '%';
  34. *d++ = *s++;
  35. }
  36. *d = '\0';
  37. return d;
  38. }
  39. /* Check and strip possible surrounding quotes in string. */
  40. char *
  41. str_strip_quotes(char *str)
  42. {
  43. if (str[0] == '"' || str[0] == '\'') {
  44. size_t str_len = strlen(str);
  45. if (str[0] != str[str_len - 1])
  46. return NULL;
  47. /* Remove surrounding quotes. */
  48. str[str_len - 1] = '\0';
  49. str++;
  50. }
  51. return str;
  52. }