~ubuntu-branches/ubuntu/oneiric/fetchmail/oneiric

« back to all changes in this revision

Viewing changes to idlist.c

  • Committer: Bazaar Package Importer
  • Author(s): Artur Rona
  • Date: 2010-07-25 03:17:28 UTC
  • mfrom: (0.1.11 sid)
  • Revision ID: james.westby@ubuntu.com-20100725031728-mm78sfgx9d656h9r
Tags: 6.3.17-4ubuntu1
* Merge from Debian unstable, remaining changes: (LP: #609634)
  - debian/init, debian/fetchmail.postinst: don't stop fetchmail in
    runlevels [06], the default 'killall' handler should be sufficient
    because fetchmail writes are properly completed before telling the
    server it's ok to delete.

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
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
#ifdef __UNUSED__
 
86
void free_str_pair_list(struct idlist **idl)
 
87
/* free the given ID pair list */
 
88
{
 
89
    if (*idl == (struct idlist *)NULL)
 
90
        return;
 
91
 
 
92
    free_idpair_list(&(*idl)->next);
 
93
    free ((*idl)->id);
 
94
    free ((*idl)->val.id2);
 
95
    free(*idl);
 
96
    *idl = (struct idlist *)NULL;
 
97
}
 
98
#endif
 
99
 
 
100
/** Check if ID \a str is in idlist \a idl. \return idlist entry if found,
 
101
 * NULL if not found. */
 
102
struct idlist *str_in_list(struct idlist **idl, const char *str,
 
103
const flag caseblind /** if true, use strcasecmp, if false, use strcmp */)
 
104
{
 
105
    struct idlist *walk;
 
106
    if (caseblind) {
 
107
        for( walk = *idl; walk; walk = walk->next )
 
108
            if( strcasecmp( str, walk->id) == 0 )
 
109
                return walk;
 
110
    } else {
 
111
        for( walk = *idl; walk; walk = walk->next )
 
112
            if( strcmp( str, walk->id) == 0 )
 
113
                return walk;
 
114
    }
 
115
    return NULL;
 
116
}
 
117
 
 
118
/** \return position of first occurrence of \a str in idlist \a idl */
 
119
int str_nr_in_list(struct idlist **idl, const char *str)
 
120
{
 
121
    int nr;
 
122
    struct idlist *walk;
 
123
 
 
124
    if (!str)
 
125
        return -1;
 
126
    for (walk = *idl, nr = 0; walk; nr ++, walk = walk->next)
 
127
        if (strcmp(str, walk->id) == 0)
 
128
            return nr;
 
129
    return -1;
 
130
}
 
131
 
 
132
/** \return position of last occurrence of \a str in idlist \a idl */
 
133
int str_nr_last_in_list( struct idlist **idl, const char *str)
 
134
{
 
135
    int nr, ret = -1;
 
136
    struct idlist *walk;
 
137
    if ( !str )
 
138
        return -1;
 
139
    for( walk = *idl, nr = 0; walk; nr ++, walk = walk->next )
 
140
        if( strcmp( str, walk->id) == 0 )
 
141
            ret = nr;
 
142
    return ret;
 
143
}
 
144
 
 
145
/** Update the mark of an id \a str in idlist \a idl to given value \a val. */
 
146
void str_set_mark( struct idlist **idl, const char *str, const flag val)
 
147
{
 
148
    int nr;
 
149
    struct idlist *walk;
 
150
    if (!str)
 
151
        return;
 
152
    for(walk = *idl, nr = 0; walk; nr ++, walk = walk->next)
 
153
        if (strcmp(str, walk->id) == 0)
 
154
            walk->val.status.mark = val;
 
155
}
 
156
 
 
157
/** Count the number of elements in the idlist \a idl. 
 
158
 * \return number of elements */
 
159
int count_list(struct idlist **idl)
 
160
{
 
161
        int i = 0;
 
162
        struct idlist *it;
 
163
 
 
164
        for (it = *idl ; it ; it = it->next)
 
165
                ++i;
 
166
 
 
167
        return i;
 
168
}
 
169
 
 
170
/** return the \a number'th id string on idlist \a idl */
 
171
/*@null@*/ char *str_from_nr_list(struct idlist **idl, long number)
 
172
{
 
173
    if( !*idl  || number < 0)
 
174
        return 0;
 
175
    if( number == 0 )
 
176
        return (*idl)->id;
 
177
    return str_from_nr_list(&(*idl)->next, number-1);
 
178
}
 
179
 
 
180
 
 
181
/** Search idlist \a idl for entry with given \a number.
 
182
 * \return id member of idlist entry. */
 
183
char *str_find(struct idlist **idl, long number)
 
184
{
 
185
    if (*idl == (struct idlist *) 0)
 
186
        return((char *) 0);
 
187
    else if (number == (*idl)->val.status.num)
 
188
        return((*idl)->id);
 
189
    else
 
190
        return(str_find(&(*idl)->next, number));
 
191
}
 
192
 
 
193
/** Search idlist \a idl for entry with given \a number.
 
194
 * \return idlist entry. */
 
195
struct idlist *id_find(struct idlist **idl, long number)
 
196
{
 
197
    struct idlist       *idp;
 
198
    for (idp = *idl; idp; idp = idp->next)
 
199
        if (idp->val.status.num == number)
 
200
            return(idp);
 
201
    return(0);
 
202
}
 
203
 
 
204
/** Return the id of the given \a id in the given idlist \a idl, comparing
 
205
 * case insensitively. \returns the respective other \a idlist member (the one
 
206
 * that was not searched for). */
 
207
char *idpair_find(struct idlist **idl, const char *id)
 
208
{
 
209
    if (*idl == (struct idlist *) 0)
 
210
        return((char *) 0);
 
211
    else if (strcasecmp(id, (*idl)->id) == 0)
 
212
        return((*idl)->val.id2 ? (*idl)->val.id2 : (*idl)->id);
 
213
    else
 
214
        return(idpair_find(&(*idl)->next, id));
 
215
}
 
216
 
 
217
/** Mark message number \a num on given idlist \a idl as deleted.
 
218
 * \return 1 if found, 0 if not found. */
 
219
int delete_str(struct idlist **idl, long num)
 
220
{
 
221
    struct idlist       *idp;
 
222
 
 
223
    for (idp = *idl; idp; idp = idp->next)
 
224
        if (idp->val.status.num == num)
 
225
        {
 
226
            idp->val.status.mark = UID_DELETED;
 
227
            return(1);
 
228
        }
 
229
    return(0);
 
230
}
 
231
 
 
232
/** Copy the given UID list \a idl. \return A newly malloc()ed copy of the list. */
 
233
struct idlist *copy_str_list(struct idlist *idl)
 
234
{
 
235
    struct idlist *newnode ;
 
236
 
 
237
    if (idl == (struct idlist *)NULL)
 
238
        return(NULL);
 
239
    else
 
240
    {
 
241
        newnode = (struct idlist *)xmalloc(sizeof(struct idlist));
 
242
        memcpy(newnode, idl, sizeof(struct idlist));
 
243
        newnode->next = copy_str_list(idl->next);
 
244
        return(newnode);
 
245
    }
 
246
}
 
247
 
 
248
/** Append \a nidl to \a idl (does not copy *) */
 
249
void append_str_list(struct idlist **idl, struct idlist **nidl)
 
250
{
 
251
    if ((*nidl) == (struct idlist *)NULL || *nidl == *idl)
 
252
        return;
 
253
    else if ((*idl) == (struct idlist *)NULL)
 
254
        *idl = *nidl;
 
255
    else if ((*idl)->next == (struct idlist *)NULL)
 
256
        (*idl)->next = *nidl;
 
257
    else if ((*idl)->next != *nidl)
 
258
        append_str_list(&(*idl)->next, nidl);
 
259
}
 
260
 
 
261
/* idlist.c ends here */