8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / lib / libmail / common / getdomain.c
blob6eada87322e459075019910d9b7fcfc085623f6d
1 /*
2 * CDDL HEADER START
4 * The contents of this file are subject to the terms of the
5 * Common Development and Distribution License (the "License").
6 * You may not use this file except in compliance with the License.
8 * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9 * or http://www.opensolaris.org/os/licensing.
10 * See the License for the specific language governing permissions
11 * and limitations under the License.
13 * When distributing Covered Code, include this CDDL HEADER in each
14 * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15 * If applicable, add the following below this CDDL HEADER, with the
16 * fields enclosed by brackets "[]" replaced with your own identifying
17 * information: Portions Copyright [yyyy] [name of copyright owner]
19 * CDDL HEADER END
23 * Copyright 2008 Sun Microsystems, Inc. All rights reserved.
24 * Use is subject to license terms.
27 /* Copyright (c) 1984, 1986, 1987, 1988, 1989 AT&T */
28 /* All Rights Reserved */
30 #pragma ident "%Z%%M% %I% %E% SMI"
32 #include "libmail.h"
33 #include <sys/types.h>
34 #include <ctype.h>
35 #ifdef SVR4
36 #include <sys/utsname.h>
37 #include <sys/systeminfo.h>
38 #endif
40 #define NMLN 512
41 #ifdef SVR4
42 #if SYS_NMLN > NMLN
43 #undef NMLN
44 #define NMLN SYS_NMLN
45 #endif
46 #endif
48 static char *look4domain(char *, char *, int);
49 static char *readdomain(char *, int);
52 * NAME
53 * maildomain() - retrieve the domain name
55 * SYNOPSIS
56 * char *maildomain()
58 * DESCRIPTION
59 * Retrieve the domain name from xgetenv("DOMAIN").
60 * If that is not set, look in /etc/resolv.conf, /etc/inet/named.boot
61 * and /etc/named.boot for "^domain[ ]+<domain>".
62 * If that is not set, use sysinfo(SI_SRPC_DOMAIN) from
63 * -lnsl. Otherwise, return an empty string.
66 /* read a file for the domain */
67 static char *look4domain(char *file, char *buf, int size)
69 char *ret = 0;
70 FILE *fp = fopen(file, "r");
72 if (!fp)
73 return (0);
75 while (fgets(buf, size, fp))
76 if (strncmp(buf, "domain", 6) == 0)
77 if (isspace(buf[6])) {
78 char *x = skipspace(buf + 6);
79 if (isgraph(*x)) {
80 trimnl(x);
81 strmove(buf, x);
82 ret = buf;
83 break;
87 (void) fclose(fp);
88 return (ret);
91 /* read the domain from the xenvironment or one of the files */
92 static char *readdomain(char *buf, int size)
94 char *ret;
96 if ((ret = xgetenv("DOMAIN")) != 0) {
97 (void) strncpy(buf, ret, size);
98 return (buf);
101 if (((ret = look4domain("/etc/resolv.conf", buf, size)) != 0) ||
102 ((ret = look4domain("/etc/inet/named.boot", buf, size)) != 0) ||
103 ((ret = look4domain("/etc/named.boot", buf, size)) != 0))
104 return (ret);
106 #ifdef SVR4
107 if (sysinfo(SI_SRPC_DOMAIN, buf, size) >= 0)
108 return (buf);
109 #endif
111 return (0);
114 char *
115 maildomain(void)
117 static char *domain = 0;
118 static char dombuf[NMLN+1] = ".";
120 /* if we've already been here, return the info */
121 if (domain != 0)
122 return (domain);
124 domain = readdomain(dombuf+1, NMLN);
126 /* Make certain that the domain begins with a single dot */
127 /* and does not have one at the end. */
128 if (domain) {
129 size_t len;
130 domain = dombuf;
131 while (*domain && *domain == '.')
132 domain++;
133 domain--;
134 len = strlen(domain);
135 while ((len > 0) && (domain[len-1] == '.'))
136 domain[--len] = '\0';
138 /* no domain information */
139 else
140 domain = "";
142 return (domain);