From: Pierre Habouzit Date: Sun, 12 Nov 2006 14:32:20 +0000 (+0100) Subject: reimplement getdnsdomainname X-Git-Url: http://git.madism.org/?p=apps%2Fmadmutt.git;a=commitdiff_plain;h=d23094706c228c63c7c7ab8f337fb5dd886c4109;ds=sidebyside reimplement getdnsdomainname strtok-- Signed-off-by: Pierre Habouzit --- diff --git a/Makefile.am b/Makefile.am index a31ca6b..38fc40b 100644 --- a/Makefile.am +++ b/Makefile.am @@ -20,8 +20,7 @@ madmutt_SOURCES = $(BUILT_SOURCES) \ alias.c attach.c base64.c browser.c buffy.c \ charset.c compress.c commands.c complete.c \ compose.c copy.c editmsg.c init.c keymap.c lib.c \ - flags.c filter.c from.c getdomain.c \ - handler.c hcache.c hdrline.c headers.c help.c history.c hook.c \ + flags.c filter.c from.c handler.c hcache.c hdrline.c headers.c help.c history.c hook.c \ main.c mbox.c mbyte.c mh.c muttlib.c mutt_idna.c mx.c \ pager.c pattern.c postpone.c query.c \ recvattach.c recvcmd.c rfc1524.c rfc3676.c \ diff --git a/getdomain.c b/getdomain.c deleted file mode 100644 index 204a693..0000000 --- a/getdomain.c +++ /dev/null @@ -1,77 +0,0 @@ -/* - * Copyright notice from original mutt: - * [none] - * - * This file is part of mutt-ng, see http://www.muttng.org/. - * It's licensed under the GNU General Public License, - * please see the file GPL in the top level source directory. - */ - -#if HAVE_CONFIG_H -# include "config.h" -#endif - -#include -#include -#include - -#include -#include "mutt.h" - -#ifndef STDC_HEADERS -int fclose (); -#endif - -/* poor man's version of getdomainname() for systems where it does not return - * return the DNS domain, but the NIS domain. - */ - -static void strip_trailing_dot (char *q) -{ - char *p = q; - - for (; *q; q++) - p = q; - - if (*p == '.') - *p = '\0'; -} - -int getdnsdomainname (char *s, size_t l) -{ - FILE *f; - char tmp[1024]; - char *p = NULL; - char *q; - - if ((f = fopen ("/etc/resolv.conf", "r")) == NULL) - return (-1); - - tmp[sizeof (tmp) - 1] = 0; - - l--; /* save room for the terminal \0 */ - - while (fgets (tmp, sizeof (tmp) - 1, f) != NULL) { - p = tmp; - while (ISSPACE (*p)) - p++; - if (m_strncmp("domain", p, 6) == 0 || m_strncmp("search", p, 6) == 0) { - p += 6; - - for (q = strtok (p, " \t\n"); q; q = strtok (NULL, " \t\n")) - if (strcmp (q, ".")) - break; - - if (q) { - strip_trailing_dot (q); - m_strcpy(s, l, q); - safe_fclose (&f); - return 0; - } - - } - } - - safe_fclose (&f); - return (-1); -} diff --git a/init.c b/init.c index a9b318e..f6efc62 100644 --- a/init.c +++ b/init.c @@ -2644,7 +2644,7 @@ void mutt_init (int skip_sys_rc, LIST * commands) else Hostname = m_strdup(utsname.nodename); - if (!p && getdnsdomainname (buffer, sizeof(buffer)) == -1) + if (!p && getdnsdomainname(buffer, sizeof(buffer)) == -1) Fqdn = m_strdup("@"); else if (*buffer != '@') { diff --git a/lib-lib/str.h b/lib-lib/str.h index 603e51b..4786d27 100644 --- a/lib-lib/str.h +++ b/lib-lib/str.h @@ -136,6 +136,12 @@ static inline const char *m_strchrnul(const char *s, int c) { return s; } +static inline const char *m_strnextsp(const char *s) { + while (*s && !isspace((unsigned char)*s)) + s++; + return s; +} + static inline const char *skipspaces(const char *s) { while (*s && isspace((unsigned char)*s)) s++; diff --git a/lib-sys/unix.c b/lib-sys/unix.c index f2141c0..4f189c1 100644 --- a/lib-sys/unix.c +++ b/lib-sys/unix.c @@ -9,6 +9,7 @@ */ #include +#include #include #include #include @@ -16,13 +17,13 @@ #include #include #include +#include #include "unix.h" #include "mutt_signal.h" #include /* for imap_wait_keepalive EEEEK */ - /* Extract the real name from /etc/passwd's GECOS field. * When set, honor the regular expression in rx, * otherwise assume that the GECOS field is a comma-separated list. @@ -156,3 +157,41 @@ int _mutt_system(const char *cmd, int flags) return (pid > 0 && WIFEXITED(rc)) ? WEXITSTATUS(rc) : -1; } + +int getdnsdomainname(char *s, ssize_t n) +{ + char tmp[1024]; + FILE *f; + + if ((f = fopen("/etc/resolv.conf", "r")) == NULL) + return -1; + + while (fgets(tmp, sizeof(tmp), f)) { + const char *p = skipspaces(tmp); + + if (m_strncmp("domain", p, 6) && m_strncmp("search", p, 6)) + continue; + + p += 6; + + while (*p) { + int trailing_dot; + const char *q; + + p = skipspaces(p); + q = m_strnextsp(p); + + trailing_dot = q[-1] == '.'; + if (!trailing_dot || q > p + 1) { + m_strncpy(s, n, p, p - q - trailing_dot); + safe_fclose(&f); + return 0; + } + + p = q; + } + } + + safe_fclose (&f); + return -1; +} diff --git a/lib-sys/unix.h b/lib-sys/unix.h index 0911521..3172841 100644 --- a/lib-sys/unix.h +++ b/lib-sys/unix.h @@ -26,7 +26,7 @@ #include ssize_t mutt_gecos_name(char *dst, ssize_t n, struct passwd *pw, rx_t *rx); - +int getdnsdomainname(char *, ssize_t); /* flags for _mutt_system() */ #define M_DETACH_PROCESS 1 /* detach subprocess from group */ diff --git a/protos.h b/protos.h index c253db0..0b6616e 100644 --- a/protos.h +++ b/protos.h @@ -303,8 +303,6 @@ void mutt_pattern_free (pattern_t ** pat); #define S_ISLNK(x) (((x) & S_IFMT) == S_IFLNK ? 1 : 0) #endif -int getdnsdomainname (char *, size_t); - /* According to SCO support, this is how to detect SCO */ #if defined (_M_UNIX) || defined (M_OS) #define SCO