po: Update German man pages translation
[dpkg.git] / lib / dpkg / string.c
blob0a8d1d026ff430211979379f04ab372aa5e4c758
1 /*
2 * libdpkg - Debian packaging suite library routines
3 * string.c - string handling routines
5 * Copyright © 1995 Ian Jackson <ijackson@chiark.greenend.org.uk>
6 * Copyright © 2008-2015 Guillem Jover <guillem@debian.org>
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.
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.
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/>.
22 #include <config.h>
23 #include <compat.h>
25 #include <string.h>
27 #include <dpkg/c-ctype.h>
28 #include <dpkg/string.h>
29 #include <dpkg/dpkg.h>
31 char *
32 str_concat(char *dst, ...)
34 va_list args;
35 const char *src;
36 size_t len;
38 va_start(args, dst);
39 while ((src = va_arg(args, const char *))) {
40 len = strlen(src);
41 memcpy(dst, src, len);
42 dst += len;
44 va_end(args);
45 *dst = '\0';
47 return dst;
50 /**
51 * Match the end of a string.
53 * @param str The string.
54 * @param end The end to match in str.
56 * @return Whether the string was matched at the end.
58 bool
59 str_match_end(const char *str, const char *end)
61 size_t str_len = strlen(str);
62 size_t end_len = strlen(end);
63 const char *str_end = str + str_len - end_len;
65 if (str_len >= end_len && strcmp(str_end, end) == 0)
66 return true;
67 else
68 return false;
71 /**
72 * Print formatted output to an allocated string.
74 * @param fmt The format string.
75 * @param ... The format arguments.
77 * @return The new allocated formatted output string (never NULL).
79 char *
80 str_fmt(const char *fmt, ...)
82 va_list args;
83 char *str;
85 va_start(args, fmt);
86 m_vasprintf(&str, fmt, args);
87 va_end(args);
89 return str;
92 /**
93 * Escape format characters from a string.
95 * @param dst The destination string.
96 * @param src The source string.
97 * @param n The size of the destination buffer.
99 * @return The end of the destination string.
101 char *
102 str_escape_fmt(char *dst, const char *src, size_t n)
104 char *d = dst;
105 const char *s = src;
107 if (n == 0)
108 return d;
110 while (*s) {
111 if (*s == '%') {
112 if (n-- <= 2)
113 break;
114 *d++ = '%';
116 if (n-- <= 1)
117 break;
118 *d++ = *s++;
121 *d = '\0';
123 return d;
127 * Quote shell metacharacters in a string.
129 * This function allows passing strings to commands without splitting the
130 * arguments, like in system(3)
132 * @param src The source string to escape.
134 * @return The new allocated string (never NULL).
136 char *
137 str_quote_meta(const char *src)
139 char *new_dst, *dst;
141 new_dst = dst = m_malloc(strlen(src) * 2);
143 while (*src) {
144 if (!c_isdigit(*src) && !c_isalpha(*src))
145 *dst++ = '\\';
147 *dst++ = *src++;
150 *dst = '\0';
152 return new_dst;
156 * Check and strip possible surrounding quotes in string.
158 * @param str The string to act on.
160 * @return A pointer to str or NULL if the quotes were unbalanced.
162 char *
163 str_strip_quotes(char *str)
165 if (str[0] == '"' || str[0] == '\'') {
166 size_t str_len = strlen(str);
168 if (str[0] != str[str_len - 1])
169 return NULL;
171 /* Remove surrounding quotes. */
172 str[str_len - 1] = '\0';
173 str++;
176 return str;
180 * Trim possible ending spaces in string.
182 * @param str The string to act on.
184 * @return A pointer to the end of the trimmed string.
186 char *
187 str_rtrim_spaces(const char *str, char *str_end)
189 while (str_end > str && c_isspace(str_end[-1]))
190 str_end--;
191 if (str_end >= str)
192 *str_end = '\0';
193 return str_end;