fix mutt compilation, that's ugly, but it works for now
[apps/madmutt.git] / getdomain.c
1 /*
2  * Copyright notice from original mutt:
3  * [none]
4  *
5  * This file is part of mutt-ng, see http://www.muttng.org/.
6  * It's licensed under the GNU General Public License,
7  * please see the file GPL in the top level source directory.
8  */
9
10 #if HAVE_CONFIG_H
11 # include "config.h"
12 #endif
13
14 #include <stdio.h>
15 #include <ctype.h>
16 #include <string.h>
17
18 #include <lib-lib/file.h>
19 #include "mutt.h"
20
21 #ifndef STDC_HEADERS
22 int fclose ();
23 #endif
24
25 /* poor man's version of getdomainname() for systems where it does not return
26  * return the DNS domain, but the NIS domain.
27  */
28
29 static void strip_trailing_dot (char *q)
30 {
31   char *p = q;
32
33   for (; *q; q++)
34     p = q;
35
36   if (*p == '.')
37     *p = '\0';
38 }
39
40 int getdnsdomainname (char *s, size_t l)
41 {
42   FILE *f;
43   char tmp[1024];
44   char *p = NULL;
45   char *q;
46
47   if ((f = fopen ("/etc/resolv.conf", "r")) == NULL)
48     return (-1);
49
50   tmp[sizeof (tmp) - 1] = 0;
51
52   l--;                          /* save room for the terminal \0 */
53
54   while (fgets (tmp, sizeof (tmp) - 1, f) != NULL) {
55     p = tmp;
56     while (ISSPACE (*p))
57       p++;
58     if (m_strncmp("domain", p, 6) == 0 || m_strncmp("search", p, 6) == 0) {
59       p += 6;
60
61       for (q = strtok (p, " \t\n"); q; q = strtok (NULL, " \t\n"))
62         if (strcmp (q, "."))
63           break;
64
65       if (q) {
66         strip_trailing_dot (q);
67         m_strcpy(s, l, q);
68         safe_fclose (&f);
69         return 0;
70       }
71
72     }
73   }
74
75   safe_fclose (&f);
76   return (-1);
77 }