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