1 /* strlist.c - string helpers
2 * Copyright (C) 1998, 2000, 2001, 2006 Free Software Foundation, Inc.
3 * Copyright (C) 2015 g10 Code GmbH
5 * This file is part of GnuPG.
7 * GnuPG is free software; you can redistribute it and/or modify it
8 * under the terms of either
10 * - the GNU Lesser General Public License as published by the Free
11 * Software Foundation; either version 3 of the License, or (at
12 * your option) any later version.
16 * - the GNU General Public License as published by the Free
17 * Software Foundation; either version 2 of the License, or (at
18 * your option) any later version.
20 * or both in parallel, as here.
22 * GnuPG is distributed in the hope that it will be useful, but
23 * WITHOUT ANY WARRANTY; without even the implied warranty of
24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * General Public License for more details.
27 * You should have received a copies of the GNU General Public License
28 * and the GNU Lesser General Public License along with this program;
29 * if not, see <https://www.gnu.org/licenses/>.
39 #include "common-defs.h"
45 free_strlist( strlist_t sl )
49 for(; sl; sl = sl2 ) {
57 free_strlist_wipe (strlist_t sl)
61 for(; sl; sl = sl2 ) {
63 wipememory (sl, sizeof *sl + strlen (sl->d));
69 /* Add STRING to the LIST at the front. This function terminates the
70 process on memory shortage. */
72 add_to_strlist( strlist_t *list, const char *string )
76 sl = xmalloc( sizeof *sl + strlen(string));
78 strcpy(sl->d, string);
85 /* Add STRING to the LIST at the front. This function returns NULL
86 and sets ERRNO on memory shortage. */
88 add_to_strlist_try (strlist_t *list, const char *string)
92 sl = xtrymalloc (sizeof *sl + strlen (string));
96 strcpy (sl->d, string);
104 /* Same as add_to_strlist() but if IS_UTF8 is *not* set, a conversion
105 to UTF-8 is done. This function terminates the process on memory
108 add_to_strlist2( strlist_t *list, const char *string, int is_utf8 )
113 sl = add_to_strlist( list, string );
116 char *p = native_to_utf8( string );
117 sl = add_to_strlist( list, p );
124 /* Add STRING to the LIST at the end. This function terminates the
125 process on memory shortage. */
127 append_to_strlist( strlist_t *list, const char *string )
130 sl = append_to_strlist_try (list, string);
137 /* Add STRING to the LIST at the end. */
139 append_to_strlist_try (strlist_t *list, const char *string)
143 sl = xtrymalloc( sizeof *sl + strlen(string));
148 strcpy(sl->d, string);
153 for( r = *list; r->next; r = r->next )
162 append_to_strlist2( strlist_t *list, const char *string, int is_utf8 )
167 sl = append_to_strlist( list, string );
170 char *p = native_to_utf8 (string);
171 sl = append_to_strlist( list, p );
178 /* Return a copy of LIST. This function terminates the process on
181 strlist_copy (strlist_t list)
183 strlist_t newlist = NULL, sl, *last;
186 for (; list; list = list->next)
188 sl = xmalloc (sizeof *sl + strlen (list->d));
189 sl->flags = list->flags;
190 strcpy(sl->d, list->d);
201 strlist_prev( strlist_t head, strlist_t node )
205 for(n=NULL; head && head != node; head = head->next )
211 strlist_last( strlist_t node )
214 for( ; node->next ; node = node->next )
220 /* Remove the first item from LIST and return its content in an
221 allocated buffer. This function terminates the process on memory
224 strlist_pop (strlist_t *list)
231 str = xmalloc(strlen(sl->d)+1);
241 /* Return the first element of the string list HAYSTACK whose string
242 matches NEEDLE. If no elements match, return NULL. */
244 strlist_find (strlist_t haystack, const char *needle)
248 haystack = haystack->next)
249 if (strcmp (haystack->d, needle) == 0)
255 strlist_length (strlist_t list)
258 for (i = 0; list; list = list->next)
264 /* Reverse the list *LIST in place. */
266 strlist_rev (strlist_t *list)
269 strlist_t lrev = NULL;
273 strlist_t tail = l->next;