From: Peter J. Holzer
[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 "mutt.h"
19
20 #ifndef STDC_HEADERS
21 int fclose ();
22 #endif
23
24 /* poor man's version of getdomainname() for systems where it does not return
25  * return the DNS domain, but the NIS domain.
26  */
27
28 static void strip_trailing_dot (char *q)
29 {
30   char *p = q;
31
32   for (; *q; q++)
33     p = q;
34
35   if (*p == '.')
36     *p = '\0';
37 }
38
39 int getdnsdomainname (char *s, size_t l)
40 {
41   FILE *f;
42   char tmp[1024];
43   char *p = NULL;
44   char *q;
45
46   if ((f = fopen ("/etc/resolv.conf", "r")) == NULL)
47     return (-1);
48
49   tmp[sizeof (tmp) - 1] = 0;
50
51   l--;                          /* save room for the terminal \0 */
52
53   while (fgets (tmp, sizeof (tmp) - 1, f) != NULL) {
54     p = tmp;
55     while (ISSPACE (*p))
56       p++;
57     if (str_ncmp ("domain", p, 6) == 0
58         || str_ncmp ("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         strfcpy (s, q, l);
68         safe_fclose (&f);
69         return 0;
70       }
71
72     }
73   }
74
75   safe_fclose (&f);
76   return (-1);
77 }