Fixed EOF detection for encrypted packets.
[gnupg.git] / jnlib / strlist.c
blobd45a1648a68ab37e1b9b44262d44f3d681ba0656
1 /* strlist.c - string helpers
2 * Copyright (C) 1998, 2000, 2001, 2006 Free Software Foundation, Inc.
4 * This file is part of JNLIB.
6 * JNLIB is free software; you can redistribute it and/or modify it
7 * under the terms of the GNU Lesser General Public License as
8 * published by the Free Software Foundation; either version 3 of
9 * the License, or (at your option) any later version.
11 * JNLIB is distributed in the hope that it will be useful, but
12 * WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this program; if not, see <http://www.gnu.org/licenses/>.
20 #include <config.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <stdarg.h>
24 #include <ctype.h>
26 #include "libjnlib-config.h"
27 #include "strlist.h"
28 #ifdef JNLIB_NEED_UTF8CONV
29 #include "utf8conv.h"
30 #endif
32 void
33 free_strlist( strlist_t sl )
35 strlist_t sl2;
37 for(; sl; sl = sl2 ) {
38 sl2 = sl->next;
39 jnlib_free(sl);
44 /* Add STRING to the LIST at the front. This function terminates the
45 process on memory shortage. */
46 strlist_t
47 add_to_strlist( strlist_t *list, const char *string )
49 strlist_t sl;
51 sl = jnlib_xmalloc( sizeof *sl + strlen(string));
52 sl->flags = 0;
53 strcpy(sl->d, string);
54 sl->next = *list;
55 *list = sl;
56 return sl;
60 /* Same as add_to_strlist() but if IS_UTF8 is *not* set, a conversion
61 to UTF-8 is done. This function terminates the process on memory
62 shortage. */
63 #ifdef JNLIB_NEED_UTF8CONV
64 strlist_t
65 add_to_strlist2( strlist_t *list, const char *string, int is_utf8 )
67 strlist_t sl;
69 if (is_utf8)
70 sl = add_to_strlist( list, string );
71 else
73 char *p = native_to_utf8( string );
74 sl = add_to_strlist( list, p );
75 jnlib_free ( p );
77 return sl;
79 #endif /* JNLIB_NEED_UTF8CONV*/
82 /* Add STRING to the LIST at the end. This function terminates the
83 process on memory shortage. */
84 strlist_t
85 append_to_strlist( strlist_t *list, const char *string )
87 strlist_t r, sl;
89 sl = jnlib_xmalloc( sizeof *sl + strlen(string));
90 sl->flags = 0;
91 strcpy(sl->d, string);
92 sl->next = NULL;
93 if( !*list )
94 *list = sl;
95 else {
96 for( r = *list; r->next; r = r->next )
98 r->next = sl;
100 return sl;
104 #ifdef JNLIB_NEED_UTF8CONV
105 strlist_t
106 append_to_strlist2( strlist_t *list, const char *string, int is_utf8 )
108 strlist_t sl;
110 if( is_utf8 )
111 sl = append_to_strlist( list, string );
112 else
114 char *p = native_to_utf8 (string);
115 sl = append_to_strlist( list, p );
116 jnlib_free( p );
118 return sl;
120 #endif /* JNLIB_NEED_UTF8CONV */
123 /* Return a copy of LIST. This function terminates the process on
124 memory shortage.*/
125 strlist_t
126 strlist_copy (strlist_t list)
128 strlist_t newlist = NULL, sl, *last;
130 last = &newlist;
131 for (; list; list = list->next)
133 sl = jnlib_xmalloc (sizeof *sl + strlen (list->d));
134 sl->flags = list->flags;
135 strcpy(sl->d, list->d);
136 sl->next = NULL;
137 *last = sl;
138 last = &sl;
140 return newlist;
145 strlist_t
146 strlist_prev( strlist_t head, strlist_t node )
148 strlist_t n;
150 for(n=NULL; head && head != node; head = head->next )
151 n = head;
152 return n;
155 strlist_t
156 strlist_last( strlist_t node )
158 if( node )
159 for( ; node->next ; node = node->next )
161 return node;
165 /* Remove the first item from LIST and return its content in an
166 allocated buffer. This function terminates the process on memory
167 shortage. */
168 char *
169 strlist_pop (strlist_t *list)
171 char *str=NULL;
172 strlist_t sl=*list;
174 if(sl)
176 str=jnlib_xmalloc(strlen(sl->d)+1);
177 strcpy(str,sl->d);
179 *list=sl->next;
180 jnlib_free(sl);
183 return str;