reimplement getdnsdomainname
[apps/madmutt.git] / lib-sys / unix.c
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;
+}