Integrate test_header in test_view_mail and various coding style modifications
[rmail.git] / src / utils / list.c
blobd375cec8380ef1408f2bbfc43535a494ddb26f98
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <errno.h>
6 #include "list.h"
9 void list_print(struct mail_list *lst)
11 size_t n;
12 int i;
14 if (!lst)
15 return;
17 for (n = 0; n < lst->n; n++) {
18 for (i = 0; i < 16; i++) {
19 fprintf(stderr, "%02x", lst->list[n].addr_md5[i]);
21 fprintf(stderr, "[%u] %s -- %s -- %s\n",
23 lst->list[n].name,
24 lst->list[n].addr,
25 lst->list[n].subj);
29 /**
30 * Allocate a list via malloc of n_elem elements
32 * @param[in|out] n_elem number of elements in the list
33 * @param[in] limit max number of elements
35 * @retval Pointer to the new allocated list or NULL on error
37 struct mail_list *list_create(size_t n_elem, size_t limit)
39 struct mail_list *lst;
41 if (n_elem == 0)
42 return NULL;
44 if (n_elem > limit)
45 n_elem = limit;
47 lst = malloc(sizeof(struct mail_list) + n_elem * sizeof(struct mail_entry));
48 if (!lst) {
49 fprintf(stderr, "%s - MALLOC: %s", __func__, strerror(errno));
50 return NULL;
53 fprintf(stderr, "%s - MALLOC: item %lu [%lu]\n", __func__,
54 (unsigned long) n_elem,
55 (unsigned long) (sizeof(struct mail_list) + n_elem * sizeof(struct mail_entry)));
57 lst->n = n_elem;
58 lst->nfree = 0;
60 return lst;
63 void list_free(struct mail_list *lst)
65 if (lst) {
66 free(lst);
67 lst = NULL;
68 fprintf(stderr, "%s - FREE: list\n", __func__);
72 int list_add_entry(struct mail_list *lst, struct mail_entry *entry)
74 size_t n = 0;
76 if (lst->nfree < MESSAGE_LIMIT) {
77 n = lst->nfree;
78 /* Update List */
79 memcpy(&lst->list[n], entry, sizeof(struct mail_entry));
81 /* Update 1st free slot index */
82 lst->nfree += 1;
83 return 0;
86 /* No free slot */
87 return -1;