]> Pileus Git - ~andy/fetchmail/blob - idlist.c
Fix typo repsonsible -> responsible.
[~andy/fetchmail] / idlist.c
1 /**
2  * \file idlist.c -- string list handling
3  *
4  * For license terms, see the file COPYING in this directory.
5  */
6
7 #include "config.h"
8
9 #include <sys/stat.h>
10 #include <errno.h>
11 #include <stdio.h>
12 #include <limits.h>
13 #if defined(STDC_HEADERS)
14 #include <stdlib.h>
15 #include <string.h>
16 #endif
17 #if defined(HAVE_UNISTD_H)
18 #include <unistd.h>
19 #endif
20
21 #include "fetchmail.h"
22
23 /** Save string \a str to idlist \a idl with status \a status.
24  * \return Pointer to the last element of the list to help the quick,
25  * constant-time addition to the list. */
26 /*@shared@*/ static
27 struct idlist **save_str_quick(/*@shared@*/ struct idlist **idl,
28                                /*@only@*/ char *str /** caller-allocated string */, flag status)
29 /* save a number/UID pair on the given UID list */
30 {
31     struct idlist **end;
32
33     /* do it nonrecursively so the list is in the right order */
34     for (end = idl; *end; end = &(*end)->next)
35         continue;
36
37     *end = (struct idlist *)xmalloc(sizeof(struct idlist));
38     (*end)->id = str;
39     (*end)->val.status.mark = status;
40     (*end)->val.status.num = 0;
41     (*end)->next = NULL;
42
43     return end;
44 }
45
46 /** Save string \a str to idlist \a idl with status \a status.
47  * \return the end list element for direct modification. */
48 struct idlist *save_str(struct idlist **idl, const char *str /** implicitly strdup()ed */, flag status)
49 {
50     return *save_str_quick(idl, str ? xstrdup(str) : NULL, status);
51 }
52
53 /** Free string list \a idl and free each of the id members. */
54 void free_str_list(struct idlist **idl)
55 {
56     struct idlist *i = *idl;
57
58     while(i) {
59         struct idlist *t = i->next;
60         free(i->id);
61         free(i);
62         i = t;
63     }
64     *idl = 0;
65 }
66
67 /** Save an ID pair made of \a str1 and \a str2 on the given idlist \a idl. */
68 void save_str_pair(struct idlist **idl, const char *str1, const char *str2)
69 {
70     struct idlist **end;
71
72     /* do it nonrecursively so the list is in the right order */
73     for (end = idl; *end; end = &(*end)->next)
74         continue;
75
76     *end = (struct idlist *)xmalloc(sizeof(struct idlist));
77     (*end)->id = str1 ? xstrdup(str1) : (char *)NULL;
78     if (str2)
79         (*end)->val.id2 = xstrdup(str2);
80     else
81         (*end)->val.id2 = (char *)NULL;
82     (*end)->next = (struct idlist *)NULL;
83 }
84
85 /** Check if ID \a str is in idlist \a idl. \return idlist entry if found,
86  * NULL if not found. */
87 struct idlist *str_in_list(struct idlist **idl, const char *str,
88 const flag caseblind /** if true, use strcasecmp, if false, use strcmp */)
89 {
90     struct idlist *walk;
91     if (caseblind) {
92         for( walk = *idl; walk; walk = walk->next )
93             if( strcasecmp( str, walk->id) == 0 )
94                 return walk;
95     } else {
96         for( walk = *idl; walk; walk = walk->next )
97             if( strcmp( str, walk->id) == 0 )
98                 return walk;
99     }
100     return NULL;
101 }
102
103 /** \return position of first occurrence of \a str in idlist \a idl */
104 int str_nr_in_list(struct idlist **idl, const char *str)
105 {
106     int nr;
107     struct idlist *walk;
108
109     if (!str)
110         return -1;
111     for (walk = *idl, nr = 0; walk; nr ++, walk = walk->next)
112         if (strcmp(str, walk->id) == 0)
113             return nr;
114     return -1;
115 }
116
117 /** \return position of last occurrence of \a str in idlist \a idl */
118 int str_nr_last_in_list( struct idlist **idl, const char *str)
119 {
120     int nr, ret = -1;
121     struct idlist *walk;
122     if ( !str )
123         return -1;
124     for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
125         if( strcmp( str, walk->id) == 0 )
126             ret = nr;
127     return ret;
128 }
129
130 /** Update the mark of an id \a str in idlist \a idl to given value \a val. */
131 void str_set_mark( struct idlist **idl, const char *str, const flag val)
132 {
133     int nr;
134     struct idlist *walk;
135     if (!str)
136         return;
137     for(walk = *idl, nr = 0; walk; nr ++, walk = walk->next)
138         if (strcmp(str, walk->id) == 0)
139             walk->val.status.mark = val;
140 }
141
142 /** Count the number of elements in the idlist \a idl. 
143  * \return number of elements */
144 int count_list(struct idlist **idl)
145 {
146         int i = 0;
147         struct idlist *it;
148
149         for (it = *idl ; it ; it = it->next)
150                 ++i;
151
152         return i;
153 }
154
155 /** return the \a number'th id string on idlist \a idl */
156 /*@null@*/ char *str_from_nr_list(struct idlist **idl, long number)
157 {
158     if( !*idl  || number < 0)
159         return 0;
160     if( number == 0 )
161         return (*idl)->id;
162     return str_from_nr_list(&(*idl)->next, number-1);
163 }
164
165
166 /** Search idlist \a idl for entry with given \a number.
167  * \return id member of idlist entry. */
168 char *str_find(struct idlist **idl, long number)
169 {
170     if (*idl == (struct idlist *) 0)
171         return((char *) 0);
172     else if (number == (*idl)->val.status.num)
173         return((*idl)->id);
174     else
175         return(str_find(&(*idl)->next, number));
176 }
177
178 /** Search idlist \a idl for entry with given \a number.
179  * \return idlist entry. */
180 struct idlist *id_find(struct idlist **idl, long number)
181 {
182     struct idlist       *idp;
183     for (idp = *idl; idp; idp = idp->next)
184         if (idp->val.status.num == number)
185             return(idp);
186     return(0);
187 }
188
189 /** Return the id of the given \a id in the given idlist \a idl, comparing
190  * case insensitively. \returns the respective other \a idlist member (the one
191  * that was not searched for). */
192 char *idpair_find(struct idlist **idl, const char *id)
193 {
194     if (*idl == (struct idlist *) 0)
195         return((char *) 0);
196     else if (strcasecmp(id, (*idl)->id) == 0)
197         return((*idl)->val.id2 ? (*idl)->val.id2 : (*idl)->id);
198     else
199         return(idpair_find(&(*idl)->next, id));
200 }
201
202 /** Mark message number \a num on given idlist \a idl as deleted.
203  * \return 1 if found, 0 if not found. */
204 int delete_str(struct idlist **idl, long num)
205 {
206     struct idlist       *idp;
207
208     for (idp = *idl; idp; idp = idp->next)
209         if (idp->val.status.num == num)
210         {
211             idp->val.status.mark = UID_DELETED;
212             return(1);
213         }
214     return(0);
215 }
216
217 /** Copy the given UID list \a idl. \return A newly malloc()ed copy of the list. */
218 struct idlist *copy_str_list(struct idlist *idl)
219 {
220     struct idlist *newnode ;
221
222     if (idl == (struct idlist *)NULL)
223         return(NULL);
224     else
225     {
226         newnode = (struct idlist *)xmalloc(sizeof(struct idlist));
227         memcpy(newnode, idl, sizeof(struct idlist));
228         newnode->next = copy_str_list(idl->next);
229         return(newnode);
230     }
231 }
232
233 /** Append \a nidl to \a idl (does not copy *) */
234 void append_str_list(struct idlist **idl, struct idlist **nidl)
235 {
236     if ((*nidl) == (struct idlist *)NULL || *nidl == *idl)
237         return;
238     else if ((*idl) == (struct idlist *)NULL)
239         *idl = *nidl;
240     else if ((*idl)->next == (struct idlist *)NULL)
241         (*idl)->next = *nidl;
242     else if ((*idl)->next != *nidl)
243         append_str_list(&(*idl)->next, nidl);
244 }
245
246 /* idlist.c ends here */