summaryrefslogtreecommitdiff
path: root/libmailutils/url/copy.c
blob: a40fe45fc68531a9dc8b12362c00cb3a2227deb5 (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
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
/* 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 <errno.h>
#include <string.h>
#ifdef HAVE_STRINGS_H
# include <strings.h>
#endif
#include <stdlib.h>
#include <mailutils/types.h>
#include <mailutils/argcv.h>
#include <mailutils/secret.h>
#include <mailutils/util.h>
#include <mailutils/sys/url.h>

struct copy_tab
{
  int mask;
  int (*fun) (mu_url_t, mu_url_t, size_t);
  size_t off;
};

static int
_url_copy_str (mu_url_t dest_url, mu_url_t src_url, size_t off)
{
  char **dest = (char**) ((char*) dest_url + off);
  char *src = *(char**) ((char*) src_url + off);
  char *p = strdup (src);
  if (!p)
    return ENOMEM;
  *dest = p;
  return 0;
}
  
static int
_url_copy_secret (mu_url_t dest, mu_url_t src, size_t off)
{
  return mu_secret_dup (src->secret, &dest->secret);
}

static int
_url_copy_port (mu_url_t dest, mu_url_t src, size_t off)
{
  if (src->portstr)
    {
      dest->portstr = strdup (src->portstr);
      if (!dest->portstr)
	return ENOMEM;
    }
  dest->port = src->port;
  return 0;
}

static char **
argcv_copy (size_t argc, char **argv)
{
  size_t i;
  char **nv = calloc (argc + 1, sizeof (nv[0]));
  if (!nv)
    return NULL;
  for (i = 0; i < argc; i++)
    if ((nv[i] = strdup (argv[i])) == NULL)
      {
	mu_argcv_free (i, nv);
	free (nv);
	return NULL;
      }
  return nv;
}

static int
_url_copy_param (mu_url_t dest, mu_url_t src, size_t off)
{
  if ((dest->fvpairs = argcv_copy (src->fvcount, src->fvpairs)) == NULL)
    return ENOMEM;
  dest->fvcount = src->fvcount;
  return 0;
}

static int
_url_copy_query (mu_url_t dest, mu_url_t src, size_t off)
{
  if ((dest->qargv = argcv_copy (src->qargc, src->qargv)) == NULL)
    return ENOMEM;
  dest->qargc = src->qargc;
  return 0;
}

static struct copy_tab copy_tab[] = {
  { MU_URL_SCHEME, _url_copy_str, mu_offsetof (struct _mu_url, scheme) },
  { MU_URL_USER,   _url_copy_str, mu_offsetof (struct _mu_url, user) },
  { MU_URL_SECRET, _url_copy_secret, 0 },
  { MU_URL_AUTH,   _url_copy_str, mu_offsetof (struct _mu_url, auth) },
  { MU_URL_HOST,   _url_copy_str, mu_offsetof (struct _mu_url, host) },
  { MU_URL_PORT,   _url_copy_port, 0 },
  { MU_URL_PATH,   _url_copy_str, mu_offsetof (struct _mu_url, path) },
  { MU_URL_PARAM,  _url_copy_param, 0 },
  { MU_URL_QUERY,  _url_copy_query, 0 }
};

int
mu_url_copy_hints (mu_url_t url, mu_url_t hint)
{
  int i;

  if (!url)
    return EINVAL;
  if (!hint)
    return 0;
  for (i = 0; i < MU_ARRAY_SIZE (copy_tab); i++)
    {
      if (!(url->flags & copy_tab[i].mask) &&
	  (hint->flags & copy_tab[i].mask))
	{
	  int rc = copy_tab[i].fun (url, hint, copy_tab[i].off);
	  if (rc)
	    return rc;
	  url->flags |= copy_tab[i].mask;
	}
    }
  return 0;
}

    



Return to:

Send suggestions and report system problems to the System administrator.