Commit 02334735 02334735e86f1cfda6a2e07cfea9283e4fc88011 by Sergey Poznyakoff

New interface for composing URLs from parts.

* include/mailutils/url.h (mu_url_sget_name)
(mu_url_aget_name,mu_url_get_name)
(mu_url_invalidate,mu_url_create_null)
(mu_url_set_user,mu_url_set_path,mu_url_set_scheme)
(mu_url_set_host,mu_url_set_port,mu_url_set_service)
(mu_url_set_auth,mu_url_set_secret,mu_url_add_param)
(mu_url_clear_param,mu_url_add_query,mu_url_clear_query): New
prototypes.

* libmailutils/tests/url-comp.at: New test case.
* libmailutils/tests/url-comp.c: New test program.
* libmailutils/tests/.gitignore: Update.
* libmailutils/tests/Makefile.am: Add new files.
* libmailutils/tests/testsuite.at: Add new tests.

* libmailutils/url/Makefile.am: Add new files.
* libmailutils/url/add-param.c: New file.
* libmailutils/url/add-query.c: New file.
* libmailutils/url/clr-param.c: New file.
* libmailutils/url/clr-query.c: New file.
* libmailutils/url/null.c: New file.
* libmailutils/url/set-auth.c: New file.
* libmailutils/url/set-host.c: New file.
* libmailutils/url/set-path.c: New file.
* libmailutils/url/set-port.c: New file.
* libmailutils/url/set-scheme.c: New file.
* libmailutils/url/set-secret.c: New file.
* libmailutils/url/set-service.c: New file.
* libmailutils/url/set-user.c: New file.
* libmailutils/url/urlinv.c: New file.

* libmailutils/url/create.c (mu_url_create_hint): Return EINVAL if
purl is NULL.
* libmailutils/url/scheme.c (mu_url_set_scheme): Remove. See set-scheme.c
* libmailutils/url/urlstr.c (mu_url_sget_name)
(mu_url_aget_name,mu_url_get_name): New functions.
(mu_url_to_string): Rewrite using the above.

* libmailutils/base/Makefile.am: Add fscompose.c
* libmailutils/base/fscompose.c: New file.
1 parent e9a329bf
...@@ -80,6 +80,10 @@ int mu_url_has_flag (mu_url_t, int); ...@@ -80,6 +80,10 @@ int mu_url_has_flag (mu_url_t, int);
80 80
81 void mu_url_destroy (mu_url_t *); 81 void mu_url_destroy (mu_url_t *);
82 82
83 int mu_url_sget_name (const mu_url_t, const char **);
84 int mu_url_aget_name (const mu_url_t, char **);
85 int mu_url_get_name (const mu_url_t, char *, size_t, size_t *);
86
83 int mu_url_sget_scheme (const mu_url_t, const char **); 87 int mu_url_sget_scheme (const mu_url_t, const char **);
84 int mu_url_aget_scheme (const mu_url_t, char **); 88 int mu_url_aget_scheme (const mu_url_t, char **);
85 int mu_url_get_scheme (const mu_url_t, char *, size_t, size_t *); 89 int mu_url_get_scheme (const mu_url_t, char *, size_t, size_t *);
...@@ -120,8 +124,6 @@ int mu_url_aget_param (const mu_url_t url, const char *param, char **val); ...@@ -120,8 +124,6 @@ int mu_url_aget_param (const mu_url_t url, const char *param, char **val);
120 int mu_url_expand_path (mu_url_t url); 124 int mu_url_expand_path (mu_url_t url);
121 const char *mu_url_to_string (const mu_url_t); 125 const char *mu_url_to_string (const mu_url_t);
122 126
123 int mu_url_set_scheme (mu_url_t url, const char *scheme);
124
125 int mu_url_is_scheme (mu_url_t, const char *scheme); 127 int mu_url_is_scheme (mu_url_t, const char *scheme);
126 128
127 int mu_url_is_same_scheme (mu_url_t, mu_url_t); 129 int mu_url_is_same_scheme (mu_url_t, mu_url_t);
...@@ -133,6 +135,22 @@ int mu_url_is_same_port (mu_url_t, mu_url_t); ...@@ -133,6 +135,22 @@ int mu_url_is_same_port (mu_url_t, mu_url_t);
133 int mu_url_matches_ticket (mu_url_t ticket, mu_url_t url, int *wcn); 135 int mu_url_matches_ticket (mu_url_t ticket, mu_url_t url, int *wcn);
134 136
135 int mu_url_decode (mu_url_t url); 137 int mu_url_decode (mu_url_t url);
138
139 int mu_url_invalidate (mu_url_t url);
140
141 int mu_url_create_null (mu_url_t *purl);
142 int mu_url_set_user (mu_url_t url, const char *user);
143 int mu_url_set_path (mu_url_t url, const char *path);
144 int mu_url_set_scheme (mu_url_t url, const char *scheme);
145 int mu_url_set_host (mu_url_t url, const char *host);
146 int mu_url_set_port (mu_url_t url, unsigned port);
147 int mu_url_set_service (mu_url_t url, const char *str);
148 int mu_url_set_auth (mu_url_t url, const char *auth);
149 int mu_url_set_secret (mu_url_t url, mu_secret_t secret);
150 int mu_url_add_param (mu_url_t url, size_t pc, const char **pv);
151 int mu_url_clear_param (mu_url_t url);
152 int mu_url_add_query (mu_url_t url, size_t pc, const char **pv);
153 int mu_url_clear_query (mu_url_t url);
136 154
137 #ifdef __cplusplus 155 #ifdef __cplusplus
138 } 156 }
......
...@@ -31,6 +31,7 @@ libbase_la_SOURCES = \ ...@@ -31,6 +31,7 @@ libbase_la_SOURCES = \
31 fgetpwent.c\ 31 fgetpwent.c\
32 filename.c\ 32 filename.c\
33 freeitem.c\ 33 freeitem.c\
34 fscompose.c\
34 getcwd.c\ 35 getcwd.c\
35 getmaxfd.c\ 36 getmaxfd.c\
36 getpass.c\ 37 getpass.c\
......
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 #include <string.h>
22 #include <mailutils/types.h>
23 #include <mailutils/util.h>
24 #include <mailutils/nls.h>
25 #include <mailutils/error.h>
26 #include <mailutils/errno.h>
27
28 int
29 mu_file_safety_compose (int *res, const char *name, int defval)
30 {
31 int negate = 0;
32 int val;
33
34 if (*name == '-')
35 {
36 negate = 1;
37 name++;
38 }
39 else if (*name == '+')
40 name++;
41
42 if (strcmp (name, "none") == 0)
43 {
44 *res = negate ? MU_FILE_SAFETY_ALL : MU_FILE_SAFETY_NONE;
45 return 0;
46 }
47 else if (strcmp (name, "all") == 0)
48 {
49 *res = negate ? MU_FILE_SAFETY_NONE : MU_FILE_SAFETY_ALL;
50 return 0;
51 }
52 else if (strcmp (name, "default") == 0)
53 val = defval;
54 else if (mu_file_safety_name_to_code (name, &val))
55 return MU_ERR_NOENT;
56
57 if (negate)
58 *res &= ~val;
59 else
60 *res |= val;
61
62 return 0;
63 }
...@@ -15,6 +15,7 @@ fsaf ...@@ -15,6 +15,7 @@ fsaf
15 listop 15 listop
16 mailcap 16 mailcap
17 prop 17 prop
18 url-comp
18 url-parse 19 url-parse
19 wicket 20 wicket
20 wsp 21 wsp
......
...@@ -51,6 +51,7 @@ noinst_PROGRAMS = \ ...@@ -51,6 +51,7 @@ noinst_PROGRAMS = \
51 mailcap\ 51 mailcap\
52 prop\ 52 prop\
53 tempfile\ 53 tempfile\
54 url-comp\
54 url-parse\ 55 url-parse\
55 wicket\ 56 wicket\
56 wsp 57 wsp
...@@ -80,6 +81,7 @@ TESTSUITE_AT = \ ...@@ -80,6 +81,7 @@ TESTSUITE_AT = \
80 prop.at\ 81 prop.at\
81 testsuite.at\ 82 testsuite.at\
82 url.at\ 83 url.at\
84 url-comp.at\
83 wicket.at\ 85 wicket.at\
84 wordsplit.at 86 wordsplit.at
85 87
......
...@@ -58,6 +58,7 @@ m4_include([list.at]) ...@@ -58,6 +58,7 @@ m4_include([list.at])
58 m4_include([address.at]) 58 m4_include([address.at])
59 m4_include([wordsplit.at]) 59 m4_include([wordsplit.at])
60 m4_include([url.at]) 60 m4_include([url.at])
61 m4_include([url-comp.at])
61 m4_include([mailcap.at]) 62 m4_include([mailcap.at])
62 m4_include([wicket.at]) 63 m4_include([wicket.at])
63 m4_include([prop.at]) 64 m4_include([prop.at])
......
1 # This file is part of GNU Mailutils. -*- Autotest -*-
2 # Copyright (C) 2011 Free Software Foundation, Inc.
3 #
4 # GNU Mailutils is free software; you can redistribute it and/or
5 # modify it under the terms of the GNU General Public License as
6 # published by the Free Software Foundation; either version 3, or (at
7 # your option) any later version.
8 #
9 # GNU Mailutils is distributed in the hope that it will be useful, but
10 # WITHOUT ANY WARRANTY; without even the implied warranty of
11 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 # General Public License for more details.
13 #
14 # You should have received a copy of the GNU General Public License
15 # along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>.
16
17 AT_BANNER(URL Composition)
18
19 dnl ------------------------------------------------------------
20 1 2 3 4
21 dnl URLCOMP(KW, ARGS, [STDOUT = `'], [STDERR = `'])
22 dnl
23
24 m4_pushdef([URLCOMP],[
25 AT_SETUP([$2 => $3])
26 AT_KEYWORDS([url-comp url-comp-$1])
27 AT_CHECK([url-comp $2],
28 [m4_if([$4],,0,1)],
29 [m4_if([$3],,,[$3
30 ])],
31 m4_if([$4],,,[url-comp: $4
32 ]))
33 AT_CLEANUP
34 ])
35
36 URLCOMP(0, [], [], [URL missing required parts])
37 URLCOMP(1, [path=foo], [foo])
38 URLCOMP(2, [scheme=file], [file://])
39 URLCOMP(3, [scheme=file path=foo], [file://foo])
40 URLCOMP(4, [scheme=smtp host=localhost port=587], [smtp://localhost:587])
41 URLCOMP(5,
42 [scheme=smtp host=localhost port=587 param=strip-domain param=domain=example.com],
43 [smtp://localhost:587;strip-domain;domain=example.com])
44 URLCOMP(6, [scheme=imap user=gray host=localhost path=INBOX],
45 [imap://gray@localhost/INBOX])
46 URLCOMP(7,
47 [scheme=imap user=gray pass=test host=localhost path=INBOX],
48 [imap://gray:***@localhost/INBOX])
49 URLCOMP(8,
50 [scheme=pop user=gray pass=test auth=+APOP host=localhost path=INBOX],
51 [pop://gray:***;AUTH=+APOP@localhost/INBOX])
52 URLCOMP(9,
53 [scheme=prog path=/bin/mailman query=request query=list],
54 [prog:///bin/mailman?request&list])
55 URLCOMP(10,
56 [scheme=prog path=/bin/mailman query=request query=list param=test],
57 [prog:///bin/mailman;test?request&list])
58 URLCOMP(11,
59 [url=imap://gray@localhost/INBOX path=],
60 [imap://gray@localhost])
61 URLCOMP(12,
62 [url=imap://gray@localhost/INBOX path= user=],
63 [imap://localhost])
64 URLCOMP(13,
65 [url='prog:///bin/mailman;test?request&list' query= param=],
66 [prog:///bin/mailman])
67 URLCOMP(14,
68 [url=smtp://gray@localhost/test host=],
69 [],
70 [URL missing required parts])
71
72 m4_popdef([URLCOMP])
...\ No newline at end of file ...\ No newline at end of file
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2011 Free Software Foundation, Inc.
3
4 GNU Mailutils is free software; you can redistribute it and/or modify
5 it under the terms of the GNU General Public License as published by
6 the Free Software Foundation; either version 3, or (at your option)
7 any later version.
8
9 GNU Mailutils 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
12 GNU General Public License for more details.
13
14 You should have received a copy of the GNU General Public License
15 along with GNU Mailutils. If not, see <http://www.gnu.org/licenses/>. */
16
17 #ifdef HAVE_CONFIG_H
18 # include <config.h>
19 #endif
20 #include <stdlib.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <mailutils/error.h>
24 #include <mailutils/errno.h>
25 #include <mailutils/url.h>
26 #include <mailutils/secret.h>
27 #include <mailutils/kwd.h>
28
29 static const char *
30 strval (const char *str)
31 {
32 return str[0] ? str : NULL;
33 }
34
35 void
36 usage (FILE *str, int code)
37 {
38 fprintf (str, "usage: %s [url=URL] [OPTIONS]\n", mu_program_name);
39 exit (code);
40 }
41
42 int
43 main (int argc, char **argv)
44 {
45 int i = 1, rc;
46 mu_url_t url;
47 const char *arg;
48
49 mu_set_program_name (argv[0]);
50
51 if (argc > 1)
52 {
53 if (strcmp (argv[1], "help") == 0 ||
54 strcmp (argv[1], "--help") == 0 ||
55 strcmp (argv[1], "-h") == 0)
56 usage (stdout, 0);
57
58 if (strncmp (argv[1], "url=", 4) == 0)
59 {
60 MU_ASSERT (mu_url_create (&url, argv[1] + 4));
61 i = 2;
62 }
63 }
64
65 if (!url)
66 {
67 MU_ASSERT (mu_url_create_null (&url));
68 i = 1;
69 }
70
71 for (; i < argc; i++)
72 {
73 if (strncmp (argv[i], "scheme=", 7) == 0)
74 {
75 MU_ASSERT (mu_url_set_scheme (url, strval (argv[i] + 7)));
76 }
77 else if (strncmp (argv[i], "user=", 5) == 0)
78 {
79 MU_ASSERT (mu_url_set_user (url, strval (argv[i] + 5)));
80 }
81 else if (strncmp (argv[i], "path=", 5) == 0)
82 {
83 MU_ASSERT (mu_url_set_path (url, strval (argv[i] + 5)));
84 }
85 else if (strncmp (argv[i], "host=", 5) == 0)
86 {
87 MU_ASSERT (mu_url_set_host (url, strval (argv[i] + 5)));
88 }
89 else if (strncmp (argv[i], "port=", 5) == 0)
90 {
91 MU_ASSERT (mu_url_set_port (url, atoi (argv[i] + 5)));
92 }
93 else if (strncmp (argv[i], "service=", 8) == 0)
94 {
95 MU_ASSERT (mu_url_set_service (url, strval (argv[i] + 8)));
96 }
97 else if (strncmp (argv[i], "auth=", 5) == 0)
98 {
99 MU_ASSERT (mu_url_set_auth (url, strval (argv[i] + 5)));
100 }
101 else if (strncmp (argv[i], "pass=", 5) == 0)
102 {
103 mu_secret_t secret;
104
105 arg = strval (argv[i] + 5);
106 if (arg)
107 {
108 MU_ASSERT (mu_secret_create (&secret, arg, strlen (arg)));
109 }
110 else
111 secret = NULL;
112 MU_ASSERT (mu_url_set_secret (url, secret));
113 }
114 else if (strncmp (argv[i], "param=", 6) == 0)
115 {
116 arg = strval (argv[i] + 6);
117 if (arg)
118 MU_ASSERT (mu_url_add_param (url, 1, (const char **)&arg));
119 else
120 MU_ASSERT (mu_url_clear_param (url));
121 }
122 else if (strncmp (argv[i], "query=", 6) == 0)
123 {
124 arg = strval (argv[i] + 6);
125 if (arg)
126 MU_ASSERT (mu_url_add_query (url, 1, (const char **)&arg));
127 else
128 MU_ASSERT (mu_url_clear_query (url));
129 }
130 else
131 {
132 mu_error ("unrecognized argument: %s", argv[i]);
133 return 1;
134 }
135 }
136
137 rc = mu_url_sget_name (url, &arg);
138 if (rc)
139 {
140 mu_error ("%s", mu_strerror (rc));
141 return 1;
142 }
143
144
145 printf ("%s\n", arg);
146 return 0;
147 }
...@@ -19,6 +19,10 @@ noinst_LTLIBRARIES = liburl.la ...@@ -19,6 +19,10 @@ noinst_LTLIBRARIES = liburl.la
19 19
20 liburl_la_SOURCES = \ 20 liburl_la_SOURCES = \
21 accessor.h\ 21 accessor.h\
22 add-param.c\
23 clr-param.c\
24 add-query.c\
25 clr-query.c\
22 copy.c\ 26 copy.c\
23 create.c\ 27 create.c\
24 decode.c\ 28 decode.c\
...@@ -36,9 +40,19 @@ liburl_la_SOURCES = \ ...@@ -36,9 +40,19 @@ liburl_la_SOURCES = \
36 get-secret.c\ 40 get-secret.c\
37 get-user.c\ 41 get-user.c\
38 match.c\ 42 match.c\
43 null.c\
39 port.c\ 44 port.c\
40 scheme.c\ 45 scheme.c\
46 set-auth.c\
47 set-host.c\
48 set-path.c\
49 set-port.c\
50 set-scheme.c\
51 set-secret.c\
52 set-service.c\
53 set-user.c\
41 uplevel.c\ 54 uplevel.c\
55 urlinv.c\
42 urlstr.c 56 urlstr.c
43 57
44 INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils 58 INCLUDES = @MU_LIB_COMMON_INCLUDES@ -I/libmailutils
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2010 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 <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #ifdef HAVE_STRINGS_H
26 # include <strings.h>
27 #endif
28
29 #include <mailutils/types.h>
30 #include <mailutils/errno.h>
31 #include <mailutils/sys/url.h>
32 #include <mailutils/url.h>
33
34 int
35 mu_url_add_param (mu_url_t url, size_t pc, const char **pv)
36 {
37 char **fv;
38 int i, j;
39
40 if (!url)
41 return EINVAL;
42 if (!pc || !pv)
43 return 0;
44
45 fv = realloc (url->fvpairs,
46 sizeof (url->fvpairs[0]) * (url->fvcount + pc + 1));
47 if (!fv)
48 return ENOMEM;
49 url->fvpairs = fv;
50 for (i = url->fvcount, j = 0; j < pc; i++, j++)
51 {
52 fv[i] = strdup (pv[j]);
53 if (!fv[i])
54 {
55 /* Restore the status quo */
56 for (; j; j--)
57 free (fv[--i]);
58 if (url->fvcount)
59 fv[url->fvcount] = NULL;
60 else
61 {
62 free (url->fvpairs);
63 url->fvpairs = NULL;
64 url->fvcount = 0;
65 }
66 return ENOMEM;
67 }
68 }
69 fv[i] = NULL;
70 url->fvcount = i;
71 url->flags |= MU_URL_PARAM;
72 mu_url_invalidate (url);
73 return 0;
74 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2010 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 <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #ifdef HAVE_STRINGS_H
26 # include <strings.h>
27 #endif
28
29 #include <mailutils/types.h>
30 #include <mailutils/errno.h>
31 #include <mailutils/sys/url.h>
32 #include <mailutils/url.h>
33
34 int
35 mu_url_add_query (mu_url_t url, size_t pc, const char **pv)
36 {
37 char **fv;
38 int i, j;
39
40 if (!url)
41 return EINVAL;
42 if (!pc || !pv)
43 return 0;
44
45 fv = realloc (url->qargv,
46 sizeof (url->qargv[0]) * (url->qargc + pc + 1));
47 if (!fv)
48 return ENOMEM;
49 url->qargv = fv;
50 for (i = url->qargc, j = 0; j < pc; i++, j++)
51 {
52 fv[i] = strdup (pv[j]);
53 if (!fv[i])
54 {
55 /* Restore the status quo */
56 for (; j; j--)
57 free (fv[--i]);
58 if (url->qargc)
59 fv[url->qargc] = NULL;
60 else
61 {
62 free (url->qargv);
63 url->qargv = NULL;
64 url->qargc = 0;
65 }
66 return ENOMEM;
67 }
68 }
69 fv[i] = NULL;
70 url->qargc = i;
71 url->flags |= MU_URL_QUERY;
72 mu_url_invalidate (url);
73 return 0;
74 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2010 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 <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #ifdef HAVE_STRINGS_H
26 # include <strings.h>
27 #endif
28
29 #include <mailutils/types.h>
30 #include <mailutils/errno.h>
31 #include <mailutils/sys/url.h>
32 #include <mailutils/url.h>
33
34 int
35 mu_url_clear_param (mu_url_t url)
36 {
37 int i;
38
39 if (!url)
40 return EINVAL;
41
42 for (i = 0; i < url->fvcount; i++)
43 free (url->fvpairs[i]);
44 free (url->fvpairs);
45 url->fvpairs = NULL;
46 url->fvcount = 0;
47 url->flags &= ~MU_URL_PARAM;
48 mu_url_invalidate (url);
49 return 0;
50 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2010 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 <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #ifdef HAVE_STRINGS_H
26 # include <strings.h>
27 #endif
28
29 #include <mailutils/types.h>
30 #include <mailutils/errno.h>
31 #include <mailutils/sys/url.h>
32 #include <mailutils/url.h>
33
34 int
35 mu_url_clear_query (mu_url_t url)
36 {
37 int i;
38
39 if (!url)
40 return EINVAL;
41
42 for (i = 0; i < url->qargc; i++)
43 free (url->qargv[i]);
44 free (url->qargv);
45 url->qargv = NULL;
46 url->qargc = 0;
47 url->flags &= ~MU_URL_QUERY;
48 mu_url_invalidate (url);
49 return 0;
50 }
...@@ -532,7 +532,11 @@ mu_url_create_hint (mu_url_t *purl, const char *str, int flags, ...@@ -532,7 +532,11 @@ mu_url_create_hint (mu_url_t *purl, const char *str, int flags,
532 { 532 {
533 int rc; 533 int rc;
534 struct mu_url_ctx ctx; 534 struct mu_url_ctx ctx;
535 mu_url_t url = calloc (1, sizeof (*url)); 535 mu_url_t url;
536
537 if (!purl)
538 return EINVAL;
539 url = calloc (1, sizeof (*url));
536 if (url == NULL) 540 if (url == NULL)
537 return ENOMEM; 541 return ENOMEM;
538 url->name = strdup (str); 542 url->name = strdup (str);
......
...@@ -27,7 +27,7 @@ mu_url_get_flags (mu_url_t url, int *pf) ...@@ -27,7 +27,7 @@ mu_url_get_flags (mu_url_t url, int *pf)
27 { 27 {
28 if (!url || !pf) 28 if (!url || !pf)
29 return EINVAL; 29 return EINVAL;
30 *pf = url->flags; 30 *pf = url->flags;
31 return 0; 31 return 0;
32 } 32 }
33 33
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2010, 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 #include <stdlib.h>
22 #include <errno.h>
23 #include <mailutils/sys/url.h>
24 #include <mailutils/url.h>
25
26 int
27 mu_url_create_null (mu_url_t *purl)
28 {
29 mu_url_t url;
30
31 if (!purl)
32 return EINVAL;
33 url = calloc (1, sizeof (*url));
34 if (url == NULL)
35 return ENOMEM;
36 *purl = url;
37 return 0;
38 }
...@@ -31,19 +31,6 @@ ...@@ -31,19 +31,6 @@
31 #include <mailutils/sys/url.h> 31 #include <mailutils/sys/url.h>
32 32
33 int 33 int
34 mu_url_set_scheme (mu_url_t url, const char *scheme)
35 {
36 char *p;
37 if (!url || !scheme)
38 return EINVAL;
39 p = realloc (url->scheme, strlen (scheme) + 1);
40 if (!p)
41 return ENOMEM;
42 strcpy (url->scheme, scheme);
43 return 0;
44 }
45
46 int
47 mu_url_is_scheme (mu_url_t url, const char *scheme) 34 mu_url_is_scheme (mu_url_t url, const char *scheme)
48 { 35 {
49 if (url && scheme && url->scheme 36 if (url && scheme && url->scheme
......
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2010 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 <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #ifdef HAVE_STRINGS_H
26 # include <strings.h>
27 #endif
28
29 #include <mailutils/types.h>
30 #include <mailutils/errno.h>
31 #include <mailutils/sys/url.h>
32 #include <mailutils/url.h>
33
34 int
35 mu_url_set_auth (mu_url_t url, const char *auth)
36 {
37 char *copy;
38
39 if (!url)
40 return EINVAL;
41 if (auth)
42 {
43 copy = strdup (auth);
44 if (!copy)
45 return ENOMEM;
46 url->flags |= MU_URL_AUTH;
47 }
48 else
49 {
50 url->flags &= ~MU_URL_AUTH;
51 copy = NULL;
52 }
53 free (url->auth);
54 url->auth = copy;
55 url->_get_auth = NULL;
56 mu_url_invalidate (url);
57 return 0;
58 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2010 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 <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #ifdef HAVE_STRINGS_H
26 # include <strings.h>
27 #endif
28
29 #include <mailutils/types.h>
30 #include <mailutils/errno.h>
31 #include <mailutils/sys/url.h>
32 #include <mailutils/url.h>
33
34 int
35 mu_url_set_host (mu_url_t url, const char *host)
36 {
37 char *copy;
38
39 if (!url)
40 return EINVAL;
41 if (host)
42 {
43 size_t len;
44 int flag = MU_URL_HOST;
45
46 len = strlen (host);
47 if (len == 0)
48 return EINVAL;
49 if (host[0] == '[' && host[len-1] == ']')
50 {
51 flag |= MU_URL_IPV6;
52 host++;
53 len -= 2;
54 }
55
56 copy = malloc (len + 1);
57 if (!copy)
58 return ENOMEM;
59 memcpy (copy, host, len);
60 copy[len] = 0;
61 url->flags |= flag;
62 }
63 else
64 {
65 url->flags &= ~(MU_URL_HOST|MU_URL_IPV6);
66 copy = NULL;
67 }
68 url->_get_host = NULL;
69 free (url->host);
70 url->host = copy;
71 mu_url_invalidate (url);
72 return 0;
73 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2010 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 <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #ifdef HAVE_STRINGS_H
26 # include <strings.h>
27 #endif
28
29 #include <mailutils/types.h>
30 #include <mailutils/errno.h>
31 #include <mailutils/sys/url.h>
32 #include <mailutils/url.h>
33
34 int
35 mu_url_set_path (mu_url_t url, const char *path)
36 {
37 char *copy;
38
39 if (!url)
40 return EINVAL;
41 if (path)
42 {
43 copy = strdup (path);
44 if (!copy)
45 return ENOMEM;
46 url->flags |= MU_URL_PATH;
47 }
48 else
49 {
50 url->flags &= ~MU_URL_PATH;
51 copy = NULL;
52 }
53 free (url->path);
54 url->path = copy;
55 url->_get_path = NULL;
56 mu_url_invalidate (url);
57 return 0;
58 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2010 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 <errno.h>
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #ifdef HAVE_STRINGS_H
27 # include <strings.h>
28 #endif
29
30 #include <mailutils/types.h>
31 #include <mailutils/errno.h>
32 #include <mailutils/sys/url.h>
33 #include <mailutils/url.h>
34
35 int
36 mu_url_set_port (mu_url_t url, unsigned port)
37 {
38 char *copy;
39
40 if (!url)
41 return EINVAL;
42 if (port)
43 {
44 char nbuf[128];
45 snprintf (nbuf, sizeof nbuf, "%u", port);
46 copy = strdup (nbuf);
47 if (!copy)
48 return ENOMEM;
49 url->flags |= MU_URL_PORT;
50 }
51 else
52 {
53 copy = NULL;
54 url->flags &= ~MU_URL_PORT;
55 }
56 url->_get_port = NULL;
57 url->_get_portstr = NULL;
58 free (url->portstr);
59 url->port = port;
60 url->portstr = copy;
61 mu_url_invalidate (url);
62 return 0;
63 }
64
65
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2010 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 <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #ifdef HAVE_STRINGS_H
26 # include <strings.h>
27 #endif
28
29 #include <mailutils/types.h>
30 #include <mailutils/errno.h>
31 #include <mailutils/sys/url.h>
32 #include <mailutils/url.h>
33
34 int
35 mu_url_set_scheme (mu_url_t url, const char *scheme)
36 {
37 char *copy;
38
39 if (!url)
40 return EINVAL;
41 if (scheme)
42 {
43 copy = strdup (scheme);
44 if (!copy)
45 return ENOMEM;
46 url->flags |= MU_URL_SCHEME;
47 }
48 else
49 {
50 url->flags &= ~MU_URL_SCHEME;
51 copy = NULL;
52 }
53 free (url->scheme);
54 url->scheme = copy;
55 url->_get_scheme = NULL;
56 mu_url_invalidate (url);
57 return 0;
58 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2010 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 <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #ifdef HAVE_STRINGS_H
26 # include <strings.h>
27 #endif
28
29 #include <mailutils/types.h>
30 #include <mailutils/errno.h>
31 #include <mailutils/sys/url.h>
32 #include <mailutils/url.h>
33 #include <mailutils/secret.h>
34
35 int
36 mu_url_set_secret (mu_url_t url, mu_secret_t secret)
37 {
38 if (!url)
39 return EINVAL;
40 if (secret)
41 {
42 url->flags |= MU_URL_SECRET;
43 }
44 else
45 {
46 url->flags &= ~MU_URL_SECRET;
47 }
48 mu_secret_destroy (&url->secret);
49 url->secret = secret;
50 url->_get_secret = NULL;
51 mu_url_invalidate (url);
52 return 0;
53 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2010, 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 <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #include <netdb.h>
26 #include <arpa/inet.h>
27 #ifdef HAVE_STRINGS_H
28 # include <strings.h>
29 #endif
30
31 #include <mailutils/types.h>
32 #include <mailutils/errno.h>
33 #include <mailutils/sys/url.h>
34 #include <mailutils/url.h>
35
36 int
37 mu_url_set_service (mu_url_t url, const char *str)
38 {
39 unsigned port;
40 char *copy;
41
42 if (!url)
43 return EINVAL;
44 if (str)
45 {
46 unsigned long n;
47 char *p;
48
49 n = strtoul (str, &p, 10);
50 if (*p)
51 {
52 /* FIXME: 1. This assumes MU_URL_PARSE_PORTSRV. */
53 /* FIXME: 2. Another proto? */
54 struct servent *sp = getservbyname (str, "tcp");
55 if (!sp)
56 return MU_ERR_TCP_NO_PORT; /*FIXME: Error code?*/
57 port = ntohs (sp->s_port);
58 }
59 else if (n > USHRT_MAX)
60 return ERANGE;
61
62 copy = strdup (str);
63 if (!copy)
64 return ENOMEM;
65 url->flags |= MU_URL_PORT;
66 }
67 else
68 {
69 copy = NULL;
70 port = 0;
71 url->flags &= ~MU_URL_PORT;
72 }
73 url->_get_port = NULL;
74 url->_get_portstr = NULL;
75 free (url->portstr);
76 url->port = port;
77 url->portstr = copy;
78 mu_url_invalidate (url);
79 return 0;
80 }
1 /* GNU Mailutils -- a suite of utilities for electronic mail
2 Copyright (C) 2010 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 <errno.h>
23 #include <stdlib.h>
24 #include <string.h>
25 #ifdef HAVE_STRINGS_H
26 # include <strings.h>
27 #endif
28
29 #include <mailutils/types.h>
30 #include <mailutils/errno.h>
31 #include <mailutils/sys/url.h>
32 #include <mailutils/url.h>
33
34 int
35 mu_url_set_user (mu_url_t url, const char *user)
36 {
37 char *copy;
38
39 if (!url)
40 return EINVAL;
41 if (user)
42 {
43 copy = strdup (user);
44 if (!copy)
45 return ENOMEM;
46 url->flags |= MU_URL_USER;
47 }
48 else
49 {
50 url->flags &= ~MU_URL_USER;
51 copy = NULL;
52 }
53 free (url->user);
54 url->user = copy;
55 url->_get_user = NULL;
56 mu_url_invalidate (url);
57 return 0;
58 }
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 #include <stdlib.h>
22 #include <errno.h>
23 #include <mailutils/sys/url.h>
24
25 int
26 mu_url_invalidate (mu_url_t url)
27 {
28 if (!url)
29 return EINVAL;
30 free (url->name);
31 url->name = NULL;
32 return 0;
33 }
...@@ -19,13 +19,160 @@ ...@@ -19,13 +19,160 @@
19 # include <config.h> 19 # include <config.h>
20 #endif 20 #endif
21 #include <stdlib.h> 21 #include <stdlib.h>
22 #include <string.h>
22 #include <mailutils/types.h> 23 #include <mailutils/types.h>
23 #include <mailutils/sys/url.h> 24 #include <mailutils/sys/url.h>
25 #include <mailutils/errno.h>
26 #include <mailutils/util.h>
27 #include <mailutils/opool.h>
28
29 #define AUTH_PFX ";AUTH="
30
31 static int
32 url_reconstruct_to_pool (mu_url_t url, mu_opool_t pool)
33 {
34 if (url->flags & MU_URL_SCHEME)
35 {
36 int i;
37
38 mu_opool_append (pool, url->scheme, strlen (url->scheme));
39 mu_opool_append (pool, "://", 3);
40
41 if (url->flags & MU_URL_USER)
42 mu_opool_append (pool, url->user, strlen (url->user));
43 if (url->flags & MU_URL_SECRET)
44 mu_opool_append (pool, ":***", 4); /* FIXME: How about MU_URL_PARSE_HIDEPASS? */
45 if (url->flags & MU_URL_AUTH)
46 {
47 mu_opool_append (pool, AUTH_PFX, sizeof AUTH_PFX - 1);
48 mu_opool_append (pool, url->auth, strlen (url->auth));
49 }
50 if (url->flags & MU_URL_HOST)
51 {
52 if (url->flags & (MU_URL_USER|MU_URL_SECRET|MU_URL_AUTH))
53 mu_opool_append_char (pool, '@');
54 mu_opool_append (pool, url->host, strlen (url->host));
55 if (url->flags & MU_URL_PORT)
56 {
57 mu_opool_append_char (pool, ':');
58 mu_opool_append (pool, url->portstr, strlen (url->portstr));
59 }
60 }
61 else if (url->flags & (MU_URL_USER|MU_URL_SECRET|MU_URL_AUTH))
62 return MU_ERR_URL_MISS_PARTS;
63
64 if (url->flags & MU_URL_PATH)
65 {
66 if (url->flags & MU_URL_HOST)
67 mu_opool_append_char (pool, '/');
68 mu_opool_append (pool, url->path, strlen (url->path));
69 }
70
71 if (url->flags & MU_URL_PARAM)
72 {
73 for (i = 0; i < url->fvcount; i++)
74 {
75 mu_opool_append_char (pool, ';');
76 mu_opool_append (pool, url->fvpairs[i],
77 strlen (url->fvpairs[i]));
78 }
79 }
80 if (url->flags & MU_URL_QUERY)
81 {
82 mu_opool_append_char (pool, '?');
83 mu_opool_append (pool, url->qargv[0],
84 strlen (url->qargv[0]));
85 for (i = 1; i < url->qargc; i++)
86 {
87 mu_opool_append_char (pool, '&');
88 mu_opool_append (pool, url->qargv[i],
89 strlen (url->qargv[i]));
90 }
91 }
92 return 0;
93 }
94 else if (url->flags == MU_URL_PATH)
95 {
96 mu_opool_append (pool, url->path, strlen (url->path));
97 return 0;
98 }
99 return MU_ERR_URL_MISS_PARTS;
100 }
101
102 int
103 mu_url_sget_name (const mu_url_t url, const char **retptr)
104 {
105 if (!url)
106 return EINVAL;
107 if (!url->name)
108 {
109 mu_opool_t pool;
110 int rc;
111 char *ptr, *newname;
112 size_t size;
113
114 rc = mu_opool_create (&pool, 0);
115 if (rc)
116 return rc;
117 rc = url_reconstruct_to_pool (url, pool);
118 if (rc)
119 {
120 mu_opool_destroy (&pool);
121 return rc;
122 }
123 ptr = mu_opool_finish (pool, &size);
124
125 newname = realloc (url->name, size + 1);
126 if (!newname)
127 {
128 mu_opool_destroy (&pool);
129 return ENOMEM;
130 }
131 memcpy (newname, ptr, size);
132 newname[size] = 0;
133 url->name = newname;
134 mu_opool_destroy (&pool);
135 }
136 if (retptr)
137 *retptr = url->name;
138 return 0;
139 }
140
141 int
142 mu_url_aget_name (const mu_url_t url, char **ret)
143 {
144 char *s;
145 const char *ptr;
146 int rc = mu_url_sget_name (url, &ptr);
147 if (rc)
148 return rc;
149 s = strdup (ptr);
150 if (!s)
151 return errno;
152 *ret = s;
153 return 0;
154 }
155
156 int
157 mu_url_get_name (const mu_url_t url, char *buf, size_t size, size_t *n)
158 {
159 size_t i;
160 const char *ptr;
161 int rc = mu_url_sget_name (url, &ptr);
162 if (rc)
163 return rc;
164 i = mu_cpystr (buf, ptr, size);
165 if (n)
166 *n = i;
167 return 0;
168 }
24 169
25 const char * 170 const char *
26 mu_url_to_string (const mu_url_t url) 171 mu_url_to_string (const mu_url_t url)
27 { 172 {
28 if (url == NULL || url->name == NULL) 173 const char *ptr;
174
175 if (mu_url_sget_name (url, &ptr))
29 return ""; 176 return "";
30 return url->name; 177 return ptr;
31 } 178 }
......