Nico Golde:
[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) return (-1);
38
39   tmp[sizeof (tmp) - 1] = 0;
40
41   l--; /* save room for the terminal \0 */
42
43   while (fgets (tmp, sizeof (tmp) - 1, f) != NULL)
44   {
45     p = tmp;
46     while (ISSPACE (*p)) p++;
47     if (mutt_strncmp ("domain", p, 6) == 0 || mutt_strncmp ("search", p, 6) == 0)
48     {
49       p += 6;
50       
51       for (q = strtok (p, " \t\n"); q; q = strtok (NULL, " \t\n"))
52         if (strcmp (q, "."))
53           break;
54
55       if (q)
56       {
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 }