]> git.ipfire.org Git - thirdparty/mdadm.git/blame - dlink.c
Create: support --readonly flag.
[thirdparty/mdadm.git] / dlink.c
CommitLineData
82b27616
NB
1
2/* doubly linked lists */
3/* This is free software. No strings attached. No copyright claimed */
4
5#include <unistd.h>
6#include <stdlib.h>
7#include <string.h>
98c6faba
NB
8#ifdef __dietlibc__
9char *strncpy(char *dest, const char *src, size_t n) __THROW;
10#endif
503975b9 11void *xcalloc(size_t num, size_t size);
82b27616
NB
12#include "dlink.h"
13
14
15void *dl_head()
16{
17 void *h;
18 h = dl_alloc(0);
19 dl_next(h) = h;
20 dl_prev(h) = h;
21 return h;
22}
23
24void dl_free(void *v)
25{
26 struct __dl_head *vv = v;
27 free(vv-1);
28}
29
30void dl_init(void *v)
31{
32 dl_next(v) = v;
33 dl_prev(v) = v;
34}
35
36void dl_insert(void *head, void *val)
37{
38 dl_next(val) = dl_next(head);
39 dl_prev(val) = head;
40 dl_next(dl_prev(val)) = val;
41 dl_prev(dl_next(val)) = val;
42}
43
44void dl_add(void *head, void *val)
45{
46 dl_prev(val) = dl_prev(head);
47 dl_next(val) = head;
48 dl_next(dl_prev(val)) = val;
49 dl_prev(dl_next(val)) = val;
50}
51
52void dl_del(void *val)
53{
54 if (dl_prev(val) == 0 || dl_next(val) == 0)
55 return;
56 dl_prev(dl_next(val)) = dl_prev(val);
57 dl_next(dl_prev(val)) = dl_next(val);
58 dl_prev(val) = dl_next(val) = 0;
59}
60
61char *dl_strndup(char *s, int l)
62{
63 char *n;
64 if (s == NULL)
65 return NULL;
66 n = dl_newv(char, l+1);
503975b9
N
67 strncpy(n, s, l);
68 n[l] = 0;
69 return n;
82b27616
NB
70}
71
72char *dl_strdup(char *s)
73{
74 return dl_strndup(s, (int)strlen(s));
75}