po: Update German man pages translation
[dpkg.git] / lib / dpkg / string.c
blobde1829769c40dce318da358fb879ba7d02fcba79
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;
37 va_start(args, dst);
38 while ((src = va_arg(args, const char *))) {
39 size_t len;
41 len = strlen(src);
42 memcpy(dst, src, len);
43 dst += len;
45 va_end(args);
46 *dst = '\0';
48 return dst;
51 /**
52 * Match the end of a string.
54 * @param str The string.
55 * @param end The end to match in str.
57 * @return Whether the string was matched at the end.
59 bool
60 str_match_end(const char *str, const char *end)
62 size_t str_len = strlen(str);
63 size_t end_len = strlen(end);
64 const char *str_end = str + str_len - end_len;
66 if (str_len >= end_len && strcmp(str_end, end) == 0)
67 return true;
68 else
69 return false;
72 /**
73 * Print formatted output to an allocated string.
75 * @param fmt The format string.
76 * @param ... The format arguments.
78 * @return The new allocated formatted output string (never NULL).
80 char *
81 str_fmt(const char *fmt, ...)
83 va_list args;
84 char *str;
86 va_start(args, fmt);
87 m_vasprintf(&str, fmt, args);
88 va_end(args);
90 return str;
93 /**
94 * Escape format characters from a string.
96 * @param dst The destination string.
97 * @param src The source string.
98 * @param n The size of the destination buffer.
100 * @return The end of the destination string.
102 char *
103 str_escape_fmt(char *dst, const char *src, size_t n)
105 char *d = dst;
106 const char *s = src;
108 if (n == 0)
109 return d;
111 while (*s) {
112 if (*s == '%') {
113 if (n-- <= 2)
114 break;
115 *d++ = '%';
117 if (n-- <= 1)
118 break;
119 *d++ = *s++;
122 *d = '\0';
124 return d;
128 * Quote shell metacharacters in a string.
130 * This function allows passing strings to commands without splitting the
131 * arguments, like in system(3)
133 * @param src The source string to escape.
135 * @return The new allocated string (never NULL).
137 char *
138 str_quote_meta(const char *src)
140 char *new_dst, *dst;
142 new_dst = dst = m_malloc(strlen(src) * 2);
144 while (*src) {
145 if (!c_isdigit(*src) && !c_isalpha(*src))
146 *dst++ = '\\';
148 *dst++ = *src++;
151 *dst = '\0';
153 return new_dst;
157 * Check and strip possible surrounding quotes in string.
159 * @param str The string to act on.
161 * @return A pointer to str or NULL if the quotes were unbalanced.
163 char *
164 str_strip_quotes(char *str)
166 if (str[0] == '"' || str[0] == '\'') {
167 size_t str_len = strlen(str);
169 if (str[0] != str[str_len - 1])
170 return NULL;
172 /* Remove surrounding quotes. */
173 str[str_len - 1] = '\0';
174 str++;
177 return str;
181 * Trim possible ending spaces in string.
183 * @param str The string to act on.
184 * @param str_end The end of the string to act on.
186 * @return A pointer to the end of the trimmed string.
188 char *
189 str_rtrim_spaces(const char *str, char *str_end)
191 while (str_end > str && c_isspace(str_end[-1]))
192 str_end--;
193 if (str_end >= str)
194 *str_end = '\0';
195 return str_end;