-gchar *avahi_get_host_name(void) {
-#ifdef HOST_NAME_MAX
- char t[HOST_NAME_MAX];
-#else
- char t[256];
-#endif
- gethostname(t, sizeof(t));
- t[sizeof(t)-1] = 0;
- return avahi_normalize_name(t);
-}
-
-static gchar *unescape_uneeded(const gchar *src, gchar *ret_dest, size_t size) {
- gboolean escaped = FALSE;
-
- g_assert(src);
- g_assert(ret_dest);
- g_assert(size > 0);
-
- for (; *src; src++) {
-
- if (!escaped && *src == '\\')
- escaped = TRUE;
- else if (escaped && (*src == '.' || *src == '\\')) {
-
- if ((size -= 2) <= 1) break;
-
- *(ret_dest++) = '\\';
- *(ret_dest++) = *src;
-
- escaped = FALSE;
- } else {
- if (--size <= 1) break;
-
- *(ret_dest++) = *src;
- escaped = FALSE;
- }
-
- }
-
- *ret_dest = 0;
-
- return ret_dest;
-}
-
-gchar *avahi_normalize_name(const gchar *s) {
- gchar tmp[256];
- gchar *n, *t;
- guint l;
- g_assert(s);
-
- unescape_uneeded(s, tmp, sizeof(tmp));
-
- n = g_utf8_normalize(tmp, -1, G_NORMALIZE_DEFAULT);
-
- if ((l = strlen(n)) == 0) {
- g_free(n);
- return g_strdup(".");
- }
-
- if (n[l-1] == '.')
- return n;
-
- t = g_strdup_printf("%s.", n);
- g_free(n);
- return t;
-}
-
-gint avahi_timeval_compare(const GTimeVal *a, const GTimeVal *b) {
- g_assert(a);
- g_assert(b);
-
- if (a->tv_sec < b->tv_sec)
- return -1;
-
- if (a->tv_sec > b->tv_sec)
- return 1;
-
- if (a->tv_usec < b->tv_usec)
- return -1;
-
- if (a->tv_usec > b->tv_usec)
- return 1;
-
- return 0;
-}
-
-glong avahi_timeval_diff(const GTimeVal *a, const GTimeVal *b) {
- g_assert(a);
- g_assert(b);
-
- if (avahi_timeval_compare(a, b) < 0)
- return avahi_timeval_diff(b, a);
-
- return ((glong) a->tv_sec - b->tv_sec)*1000000 + a->tv_usec - b->tv_usec;
-}
-
-
-gint avahi_set_cloexec(gint fd) {
- gint n;
-
- g_assert(fd >= 0);
-
- if ((n = fcntl(fd, F_GETFD)) < 0)
- return -1;
-
- if (n & FD_CLOEXEC)
- return 0;
-
- return fcntl(fd, F_SETFD, n|FD_CLOEXEC);
-}
-
-gint avahi_set_nonblock(gint fd) {
- gint n;
-
- g_assert(fd >= 0);
-
- if ((n = fcntl(fd, F_GETFL)) < 0)
- return -1;
-
- if (n & O_NONBLOCK)
- return 0;
-
- return fcntl(fd, F_SETFL, n|O_NONBLOCK);
-}
-
-gint avahi_wait_for_write(gint fd) {
- fd_set fds;
- gint r;
-
- FD_ZERO(&fds);
- FD_SET(fd, &fds);
-
- if ((r = select(fd+1, NULL, &fds, NULL, NULL)) < 0) {
- g_message("select() failed: %s", strerror(errno));
-
- return -1;
- }
-
- g_assert(r > 0);
-
- return 0;
-}
-
-GTimeVal *avahi_elapse_time(GTimeVal *tv, guint msec, guint jitter) {
- g_assert(tv);
-
- g_get_current_time(tv);
-
- if (msec)
- g_time_val_add(tv, msec*1000);
-
- if (jitter)
- g_time_val_add(tv, g_random_int_range(0, jitter) * 1000);
-
- return tv;
-}
-
-glong avahi_age(const GTimeVal *a) {
- GTimeVal now;
-
- g_assert(a);
-
- g_get_current_time(&now);
-
- return avahi_timeval_diff(&now, a);
-}