2 This file is part of catta.
4 catta is free software; you can redistribute it and/or modify it
5 under the terms of the GNU Lesser General Public License as
6 published by the Free Software Foundation; either version 2.1 of the
7 License, or (at your option) any later version.
9 catta is distributed in the hope that it will be useful, but WITHOUT
10 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
11 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
12 Public License for more details.
14 You should have received a copy of the GNU Lesser General Public
15 License along with catta; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include <catta/alternative.h>
30 #include <catta/malloc.h>
31 #include <catta/domain.h>
34 static void drop_incomplete_utf8(char *c) {
41 if (catta_utf8_valid(c))
51 char *catta_alternative_host_name(const char *s) {
57 if (!catta_is_valid_host_name(s))
60 if ((e = strrchr(s, '-'))) {
71 if (e && (*e == '0' || *e == 0))
81 if (!(m = catta_strdup_printf("%i", n)))
86 if (l >= CATTA_LABEL_MAX-1-strlen(m)-1)
87 l = CATTA_LABEL_MAX-1-strlen(m)-1;
89 if (!(c = catta_strndup(s, l))) {
94 drop_incomplete_utf8(c);
96 r = catta_strdup_printf("%s-%s", c, m);
103 if (!(c = catta_strndup(s, CATTA_LABEL_MAX-1-2)))
106 drop_incomplete_utf8(c);
108 r = catta_strdup_printf("%s-2", c);
112 assert(catta_is_valid_host_name(r));
117 char *catta_alternative_service_name(const char *s) {
123 if (!catta_is_valid_service_name(s))
126 if ((e = strstr(s, " #"))) {
130 while ((n = strstr(e, " #")))
139 if (e && (*e == '0' || *e == 0))
149 if (!(m = catta_strdup_printf("%i", n)))
154 if (l >= CATTA_LABEL_MAX-1-strlen(m)-2)
155 l = CATTA_LABEL_MAX-1-strlen(m)-2;
157 if (!(c = catta_strndup(s, l))) {
162 drop_incomplete_utf8(c);
164 r = catta_strdup_printf("%s #%s", c, m);
170 if (!(c = catta_strndup(s, CATTA_LABEL_MAX-1-3)))
173 drop_incomplete_utf8(c);
175 r = catta_strdup_printf("%s #2", c);
179 assert(catta_is_valid_service_name(r));