reimplement getdnsdomainname
authorPierre Habouzit <madcoder@debian.org>
Sun, 12 Nov 2006 14:32:20 +0000 (15:32 +0100)
committerPierre Habouzit <madcoder@debian.org>
Sun, 12 Nov 2006 14:32:20 +0000 (15:32 +0100)
strtok--

Signed-off-by: Pierre Habouzit <madcoder@debian.org>
Makefile.am
getdomain.c [deleted file]
init.c
lib-lib/str.h
lib-sys/unix.c
lib-sys/unix.h
protos.h

index a31ca6b..38fc40b 100644 (file)
@@ -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 (file)
index 204a693..0000000
+++ /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 <stdio.h>
-#include <ctype.h>
-#include <string.h>
-
-#include <lib-lib/file.h>
-#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 (file)
--- 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 != '@') {
index 603e51b..4786d27 100644 (file)
@@ -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++;
index f2141c0..4f189c1 100644 (file)
@@ -9,6 +9,7 @@
  */
 
 #include <errno.h>
+#include <ctype.h>
 #include <stdlib.h>
 #include <sys/wait.h>
 #include <unistd.h>
 #include <lib-lib/macros.h>
 #include <lib-lib/mem.h>
 #include <lib-lib/str.h>
+#include <lib-lib/file.h>
 
 #include "unix.h"
 #include "mutt_signal.h"
 
 #include <imap/imap.h> /* 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;
+}
index 0911521..3172841 100644 (file)
@@ -26,7 +26,7 @@
 #include <lib-lib/rx.h>
 
 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 */
index c253db0..0b6616e 100644 (file)
--- 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