+
+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, q - trailing_dot - p);
+ m_fclose(&f);
+ return 0;
+ }
+
+ p = q;
+ }
+ }
+
+ m_fclose(&f);
+ return -1;
+}