diff options
Diffstat (limited to 'libmailutils/sockaddr/insert.c')
-rw-r--r-- | libmailutils/sockaddr/insert.c | 64 |
1 files changed, 64 insertions, 0 deletions
diff --git a/libmailutils/sockaddr/insert.c b/libmailutils/sockaddr/insert.c new file mode 100644 index 000000000..dbac151c1 --- /dev/null +++ b/libmailutils/sockaddr/insert.c | |||
@@ -0,0 +1,64 @@ | |||
1 | /* GNU Mailutils -- a suite of utilities for electronic mail | ||
2 | Copyright (C) 2011 Free Software Foundation, Inc. | ||
3 | |||
4 | This library is free software; you can redistribute it and/or | ||
5 | modify it under the terms of the GNU Lesser General Public | ||
6 | License as published by the Free Software Foundation; either | ||
7 | version 3 of the License, or (at your option) any later version. | ||
8 | |||
9 | This library is distributed in the hope that it will be useful, | ||
10 | but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
11 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
12 | Lesser General Public License for more details. | ||
13 | |||
14 | You should have received a copy of the GNU Lesser General | ||
15 | Public License along with this library. If not, see | ||
16 | <http://www.gnu.org/licenses/>. */ | ||
17 | |||
18 | #ifdef HAVE_CONFIG_H | ||
19 | # include <config.h> | ||
20 | #endif | ||
21 | |||
22 | #include <stdlib.h> | ||
23 | #include <mailutils/sockaddr.h> | ||
24 | |||
25 | static void | ||
26 | set_next (struct mu_sockaddr *sp, struct mu_sockaddr *tgt) | ||
27 | { | ||
28 | for (; sp->next; sp = sp->next) | ||
29 | ; | ||
30 | sp->next = tgt; | ||
31 | if (tgt) | ||
32 | tgt->prev = sp; | ||
33 | } | ||
34 | |||
35 | struct mu_sockaddr * | ||
36 | mu_sockaddr_insert (struct mu_sockaddr *anchor, struct mu_sockaddr *addr, | ||
37 | int before) | ||
38 | { | ||
39 | struct mu_sockaddr *ret = anchor; | ||
40 | |||
41 | if (!anchor) | ||
42 | { | ||
43 | addr->prev = NULL; | ||
44 | set_next (addr, NULL); | ||
45 | return addr; | ||
46 | } | ||
47 | |||
48 | if (before) | ||
49 | { | ||
50 | if (anchor->prev) | ||
51 | anchor = anchor->prev; | ||
52 | else | ||
53 | { | ||
54 | addr->prev = NULL; | ||
55 | set_next (addr, anchor); | ||
56 | return addr; | ||
57 | } | ||
58 | } | ||
59 | |||
60 | set_next (addr, anchor->next); | ||
61 | anchor->next = addr; | ||
62 | addr->prev = anchor; | ||
63 | return ret; | ||
64 | } | ||