Patrick Welche <prlw1@cam.ac.uk>
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / global / strip_addr.c
blob4aec4dbbacd2e844559e95e0cf286909f9f20a13
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* strip_addr 3
6 /* SUMMARY
7 /* strip extension from full or localpart-only address
8 /* SYNOPSIS
9 /* #include <strip_addr.h>
11 /* char *strip_addr(address, extension, delimiter)
12 /* const char *address;
13 /* char **extension;
14 /* int delimiter;
15 /* DESCRIPTION
16 /* strip_addr() takes an address and either returns a null
17 /* pointer when the address contains no address extension,
18 /* or returns a copy of the address without address extension.
19 /* The caller is expected to pass the copy to myfree().
21 /* Arguments:
22 /* .IP address
23 /* Address localpart or user@domain form.
24 /* .IP extension
25 /* A null pointer, or the address of a pointer that is set to
26 /* the address of a dynamic memory copy of the address extension
27 /* that had to be chopped off.
28 /* The copy includes the recipient address delimiter.
29 /* The caller is expected to pass the copy to myfree().
30 /* .IP delimiter
31 /* Recipient address delimiter.
32 /* SEE ALSO
33 /* split_addr(3) strip extension from localpart
34 /* LICENSE
35 /* .ad
36 /* .fi
37 /* The Secure Mailer license must be distributed with this software.
38 /* AUTHOR(S)
39 /* Wietse Venema
40 /* IBM T.J. Watson Research
41 /* P.O. Box 704
42 /* Yorktown Heights, NY 10598, USA
43 /*--*/
45 /* System library. */
47 #include <sys_defs.h>
48 #include <string.h>
50 /* Utility library. */
52 #include <mymalloc.h>
54 /* Global library. */
56 #include <split_addr.h>
57 #include <strip_addr.h>
59 /* strip_addr - strip extension from address */
61 char *strip_addr(const char *full, char **extension, int delimiter)
63 char *ratsign;
64 char *extent;
65 char *saved_ext;
66 char *stripped;
69 * A quick test to eliminate inputs without delimiter anywhere.
71 if (delimiter == 0 || strchr(full, delimiter) == 0) {
72 stripped = saved_ext = 0;
73 } else {
74 stripped = mystrdup(full);
75 if ((ratsign = strrchr(stripped, '@')) != 0)
76 *ratsign = 0;
77 if ((extent = split_addr(stripped, delimiter)) != 0) {
78 extent -= 1;
79 if (extension) {
80 *extent = delimiter;
81 saved_ext = mystrdup(extent);
82 *extent = 0;
83 } else
84 saved_ext = 0;
85 if (ratsign != 0) {
86 *ratsign = '@';
87 memmove(extent, ratsign, strlen(ratsign) + 1);
89 } else {
90 myfree(stripped);
91 stripped = saved_ext = 0;
94 if (extension)
95 *extension = saved_ext;
96 return (stripped);
99 #ifdef TEST
101 #include <msg.h>
102 #include <mail_params.h>
104 char *var_double_bounce_sender = DEF_DOUBLE_BOUNCE;
106 int main(int unused_argc, char **unused_argv)
108 char *extension;
109 char *stripped;
110 int delim = '-';
113 * Incredible. This function takes only three arguments, and the tests
114 * already take more lines of code than the code being tested.
116 stripped = strip_addr("foo", (char **) 0, 0);
117 if (stripped != 0)
118 msg_panic("strip_addr botch 1");
120 stripped = strip_addr("foo", &extension, 0);
121 if (stripped != 0)
122 msg_panic("strip_addr botch 2");
123 if (extension != 0)
124 msg_panic("strip_addr botch 3");
126 stripped = strip_addr("foo", (char **) 0, delim);
127 if (stripped != 0)
128 msg_panic("strip_addr botch 4");
130 stripped = strip_addr("foo", &extension, delim);
131 if (stripped != 0)
132 msg_panic("strip_addr botch 5");
133 if (extension != 0)
134 msg_panic("strip_addr botch 6");
136 stripped = strip_addr("foo@bar", (char **) 0, 0);
137 if (stripped != 0)
138 msg_panic("strip_addr botch 7");
140 stripped = strip_addr("foo@bar", &extension, 0);
141 if (stripped != 0)
142 msg_panic("strip_addr botch 8");
143 if (extension != 0)
144 msg_panic("strip_addr botch 9");
146 stripped = strip_addr("foo@bar", (char **) 0, delim);
147 if (stripped != 0)
148 msg_panic("strip_addr botch 10");
150 stripped = strip_addr("foo@bar", &extension, delim);
151 if (stripped != 0)
152 msg_panic("strip_addr botch 11");
153 if (extension != 0)
154 msg_panic("strip_addr botch 12");
156 stripped = strip_addr("foo-ext", (char **) 0, 0);
157 if (stripped != 0)
158 msg_panic("strip_addr botch 13");
160 stripped = strip_addr("foo-ext", &extension, 0);
161 if (stripped != 0)
162 msg_panic("strip_addr botch 14");
163 if (extension != 0)
164 msg_panic("strip_addr botch 15");
166 stripped = strip_addr("foo-ext", (char **) 0, delim);
167 if (stripped == 0)
168 msg_panic("strip_addr botch 16");
169 msg_info("wanted: foo-ext -> %s", "foo");
170 msg_info("strip_addr foo-ext -> %s", stripped);
171 myfree(stripped);
173 stripped = strip_addr("foo-ext", &extension, delim);
174 if (stripped == 0)
175 msg_panic("strip_addr botch 17");
176 if (extension == 0)
177 msg_panic("strip_addr botch 18");
178 msg_info("wanted: foo-ext -> %s %s", "foo", "-ext");
179 msg_info("strip_addr foo-ext -> %s %s", stripped, extension);
180 myfree(stripped);
181 myfree(extension);
183 stripped = strip_addr("foo-ext@bar", (char **) 0, 0);
184 if (stripped != 0)
185 msg_panic("strip_addr botch 19");
187 stripped = strip_addr("foo-ext@bar", &extension, 0);
188 if (stripped != 0)
189 msg_panic("strip_addr botch 20");
190 if (extension != 0)
191 msg_panic("strip_addr botch 21");
193 stripped = strip_addr("foo-ext@bar", (char **) 0, delim);
194 if (stripped == 0)
195 msg_panic("strip_addr botch 22");
196 msg_info("wanted: foo-ext@bar -> %s", "foo@bar");
197 msg_info("strip_addr foo-ext@bar -> %s", stripped);
198 myfree(stripped);
200 stripped = strip_addr("foo-ext@bar", &extension, delim);
201 if (stripped == 0)
202 msg_panic("strip_addr botch 23");
203 if (extension == 0)
204 msg_panic("strip_addr botch 24");
205 msg_info("wanted: foo-ext@bar -> %s %s", "foo@bar", "-ext");
206 msg_info("strip_addr foo-ext@bar -> %s %s", stripped, extension);
207 myfree(stripped);
208 myfree(extension);
210 return (0);
213 #endif