summaryrefslogtreecommitdiff
path: root/libproto/imap/tag.c
blob: 73a0eda83ffe19bf7ed1aab424e6369a99f53805 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
/* GNU Mailutils -- a suite of utilities for electronic mail
   Copyright (C) 2010-2012, 2014-2016 Free Software Foundation, Inc.

   This library is free software; you can redistribute it and/or
   modify it under the terms of the GNU Lesser General Public
   License as published by the Free Software Foundation; either
   version 3 of the License, or (at your option) any later version.

   This library is distributed in the hope that it will be useful,
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
   Lesser General Public License for more details.

   You should have received a copy of the GNU Lesser General
   Public License along with this library.  If not, see
   <http://www.gnu.org/licenses/>. */

#ifdef HAVE_CONFIG_H
# include <config.h>
#endif
#include <stdlib.h>
#include <errno.h>
#include <mailutils/sys/imap.h>

static int
_mu_imap_tag_incr (mu_imap_t imap)
{
  int i = 0;
  
  while (1)
    {
      if (++imap->tag_buf[i] <= 9)
	break;
      imap->tag_buf[i] = 0;
      if (++i == imap->tag_len)
	{
	  char *sp;
	  int *np = realloc (imap->tag_buf,
			     (imap->tag_len + 1) * sizeof imap->tag_buf[0]);
	  if (!np)
	    return ENOMEM;
	  imap->tag_buf = np;
	  sp = realloc (imap->tag_str,
			(imap->tag_len + 2) * sizeof imap->tag_str[0]);
	  if (!sp)
	    return ENOMEM;
	  imap->tag_str = sp;
	  imap->tag_len++;
	}
    }
  return 0;
}

static void
_mu_imap_tag_print (mu_imap_t imap)
{
  int i;

  for (i = 0; i < imap->tag_len; i++)
    imap->tag_str[imap->tag_len-i-1] = imap->tag_buf[i] + '0';
  imap->tag_str[i] = 0;
}

int
_mu_imap_tag_clr (mu_imap_t imap)
{
  int i;
  
  if (imap->tag_len == 0)
    {
      imap->tag_len = 2;
      imap->tag_buf = calloc (imap->tag_len, sizeof (imap->tag_buf[0]));
      if (!imap->tag_buf)
	{
	  imap->tag_len = 0;
	  return ENOMEM;
	}
      imap->tag_str = calloc (imap->tag_len + 1, sizeof (imap->tag_str[0]));
      if (!imap->tag_str)
	{
	  free (imap->tag_buf);
	  imap->tag_buf = NULL;
	  imap->tag_len = 0;
	  return ENOMEM;
	}
    }
  for (i = 0; i < imap->tag_len; i++)
    imap->tag_buf[i] = 0;
  _mu_imap_tag_print (imap);
  return 0;
}

int
_mu_imap_tag_next (mu_imap_t imap)
{
  int status;
  status = _mu_imap_tag_incr (imap);
  if (status == 0)
    _mu_imap_tag_print (imap);
  return status;
}

int
mu_imap_tag (mu_imap_t imap, const char **ptag)
{
  if (!imap)
    return EINVAL;
  *ptag = imap->tag_str;
  return 0;
}

Return to:

Send suggestions and report system problems to the System administrator.