Andreas Krennmair:
[apps/madmutt.git] / getdomain.c
1 #if HAVE_CONFIG_H
2 # include "config.h"
3 #endif
4
5 #include <stdio.h>
6 #include <ctype.h>
7 #include <string.h>
8
9 #include "mutt.h"
10
11 #ifndef STDC_HEADERS
12 int fclose ();
13 #endif
14
15 /* poor man's version of getdomainname() for systems where it does not return
16  * return the DNS domain, but the NIS domain.
17  */
18
19 static void strip_trailing_dot (char *q)
20 {
21   char *p = q;
22
23   for (; *q; q++)
24     p = q;
25
26   if (*p == '.')
27     *p = '\0';
28 }
29
30 int getdnsdomainname (char *s, size_t l)
31 {
32   FILE *f;
33   char tmp[1024];
34   char *p = NULL;
35   char *q;
36
37   if ((f = fopen ("/etc/resolv.conf", "r")) == NULL)
38     return (-1);
39
40   tmp[sizeof (tmp) - 1] = 0;
41
42   l--;                          /* save room for the terminal \0 */
43
44   while (fgets (tmp, sizeof (tmp) - 1, f) != NULL) {
45     p = tmp;
46     while (ISSPACE (*p))
47       p++;
48     if (mutt_strncmp ("domain", p, 6) == 0
49         || mutt_strncmp ("search", p, 6) == 0) {
50       p += 6;
51
52       for (q = strtok (p, " \t\n"); q; q = strtok (NULL, " \t\n"))
53         if (strcmp (q, "."))
54           break;
55
56       if (q) {
57         strip_trailing_dot (q);
58         strfcpy (s, q, l);
59         safe_fclose (&f);
60         return 0;
61       }
62
63     }
64   }
65
66   safe_fclose (&f);
67   return (-1);
68 }