4 This file is part of avahi.
6 avahi is free software; you can redistribute it and/or modify it
7 under the terms of the GNU Lesser General Public License as
8 published by the Free Software Foundation; either version 2.1 of the
9 License, or (at your option) any later version.
11 avahi is distributed in the hope that it will be useful, but WITHOUT
12 ANY WARRANTY; without even the implied warranty of MERCHANTABILITY
13 or FITNESS FOR A PARTICULAR PURPOSE. See the GNU Lesser General
14 Public License for more details.
16 You should have received a copy of the GNU Lesser General Public
17 License along with avahi; if not, write to the Free Software
18 Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
31 #include <sys/utsname.h>
34 #include <avahi-common/malloc.h>
36 #include "domain-util.h"
39 static void strip_bad_chars(char *s) {
42 s[strcspn(s, ".")] = 0;
44 for (p = s, d = s; *p; p++)
45 if ((*p >= 'a' && *p <= 'z') ||
46 (*p >= 'A' && *p <= 'Z') ||
47 (*p >= '0' && *p <= '9') ||
54 char *avahi_get_host_name(char *ret_s, size_t size) {
58 if (gethostname(ret_s, size) >= 0) {
60 strip_bad_chars(ret_s);
65 struct utsname utsname;
67 /* No hostname was set, so let's take the OS name */
69 if (uname(&utsname) >= 0) {
70 snprintf(ret_s, size, "%s", utsname.sysname);
71 strip_bad_chars(ret_s);
76 snprintf(ret_s, size, "unnamed");
79 if (size >= AVAHI_LABEL_MAX)
80 ret_s[AVAHI_LABEL_MAX-1] = 0;
85 char *avahi_get_host_name_strdup(void) {
86 char t[AVAHI_DOMAIN_NAME_MAX];
88 if (!(avahi_get_host_name(t, sizeof(t))))
91 return avahi_strdup(t);
94 int avahi_binary_domain_cmp(const char *a, const char *b) {
102 char ca[AVAHI_LABEL_MAX], cb[AVAHI_LABEL_MAX], *p;
105 p = avahi_unescape_label(&a, ca, sizeof(ca));
107 p = avahi_unescape_label(&b, cb, sizeof(cb));
110 if ((r = strcmp(ca, cb)))
118 int avahi_domain_ends_with(const char *domain, const char *suffix) {
123 char dummy[AVAHI_LABEL_MAX], *r;
128 if (avahi_domain_equal(domain, suffix))
131 r = avahi_unescape_label(&domain, dummy, sizeof(dummy));