2 This file is part of avahi.
4 avahi 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 avahi 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 avahi; if not, write to the Free Software
16 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
29 #include "alternative.h"
34 static void drop_incomplete_utf8(char *c) {
41 if (avahi_utf8_valid(c))
51 char *avahi_alternative_host_name(const char *s) {
57 if (!avahi_is_valid_host_name(s))
60 if ((e = strrchr(s, '-'))) {
71 if (e && (*e == '0' || *e == 0))
81 if (!(m = avahi_strdup_printf("%i", n)))
86 if (l >= AVAHI_LABEL_MAX-1-strlen(m)-1)
87 l = AVAHI_LABEL_MAX-1-strlen(m)-1;
89 if (!(c = avahi_strndup(s, l))) {
94 drop_incomplete_utf8(c);
96 r = avahi_strdup_printf("%s-%s", c, m);
103 if (!(c = avahi_strndup(s, AVAHI_LABEL_MAX-1-2)))
106 drop_incomplete_utf8(c);
108 r = avahi_strdup_printf("%s-2", c);
112 assert(avahi_is_valid_host_name(r));
117 char *avahi_alternative_service_name(const char *s) {
123 if (!avahi_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 = avahi_strdup_printf("%i", n)))
154 if (l >= AVAHI_LABEL_MAX-1-strlen(m)-2)
155 l = AVAHI_LABEL_MAX-1-strlen(m)-2;
157 if (!(c = avahi_strndup(s, l))) {
162 drop_incomplete_utf8(c);
164 r = avahi_strdup_printf("%s #%s", c, m);
170 if (!(c = avahi_strndup(s, AVAHI_LABEL_MAX-1-3)))
173 drop_incomplete_utf8(c);
175 r = avahi_strdup_printf("%s #2", c);
179 assert(avahi_is_valid_service_name(r));