xref: /titanic_41/usr/src/cmd/sendmail/libsm/cf.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * Copyright (c) 2001 Sendmail, Inc. and its suppliers.
3*7c478bd9Sstevel@tonic-gate  *      All rights reserved.
4*7c478bd9Sstevel@tonic-gate  *
5*7c478bd9Sstevel@tonic-gate  * By using this file, you agree to the terms and conditions set
6*7c478bd9Sstevel@tonic-gate  * forth in the LICENSE file which can be found at the top level of
7*7c478bd9Sstevel@tonic-gate  * the sendmail distribution.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  */
10*7c478bd9Sstevel@tonic-gate 
11*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
12*7c478bd9Sstevel@tonic-gate 
13*7c478bd9Sstevel@tonic-gate #include <sm/gen.h>
14*7c478bd9Sstevel@tonic-gate SM_RCSID("@(#)$Id: cf.c,v 1.4 2001/02/01 02:40:21 dmoen Exp $")
15*7c478bd9Sstevel@tonic-gate 
16*7c478bd9Sstevel@tonic-gate #include <ctype.h>
17*7c478bd9Sstevel@tonic-gate #include <errno.h>
18*7c478bd9Sstevel@tonic-gate 
19*7c478bd9Sstevel@tonic-gate #include <sm/cf.h>
20*7c478bd9Sstevel@tonic-gate #include <sm/io.h>
21*7c478bd9Sstevel@tonic-gate #include <sm/string.h>
22*7c478bd9Sstevel@tonic-gate #include <sm/heap.h>
23*7c478bd9Sstevel@tonic-gate 
24*7c478bd9Sstevel@tonic-gate /*
25*7c478bd9Sstevel@tonic-gate **  SM_CF_GETOPT -- look up option values in the sendmail.cf file
26*7c478bd9Sstevel@tonic-gate **
27*7c478bd9Sstevel@tonic-gate **	Open the sendmail.cf file and parse all of the 'O' directives.
28*7c478bd9Sstevel@tonic-gate **	Each time one of the options named in the option vector optv
29*7c478bd9Sstevel@tonic-gate **	is found, store a malloced copy of the option value in optv.
30*7c478bd9Sstevel@tonic-gate **
31*7c478bd9Sstevel@tonic-gate **	Parameters:
32*7c478bd9Sstevel@tonic-gate **		path -- pathname of sendmail.cf file
33*7c478bd9Sstevel@tonic-gate **		optc -- size of option vector
34*7c478bd9Sstevel@tonic-gate **		optv -- pointer to option vector
35*7c478bd9Sstevel@tonic-gate **
36*7c478bd9Sstevel@tonic-gate **	Results:
37*7c478bd9Sstevel@tonic-gate **		0 on success, or an errno value on failure.
38*7c478bd9Sstevel@tonic-gate **		An exception is raised on malloc failure.
39*7c478bd9Sstevel@tonic-gate */
40*7c478bd9Sstevel@tonic-gate 
41*7c478bd9Sstevel@tonic-gate int
42*7c478bd9Sstevel@tonic-gate sm_cf_getopt(path, optc, optv)
43*7c478bd9Sstevel@tonic-gate 	char *path;
44*7c478bd9Sstevel@tonic-gate 	int optc;
45*7c478bd9Sstevel@tonic-gate 	SM_CF_OPT_T *optv;
46*7c478bd9Sstevel@tonic-gate {
47*7c478bd9Sstevel@tonic-gate 	SM_FILE_T *cfp;
48*7c478bd9Sstevel@tonic-gate 	char buf[2048];
49*7c478bd9Sstevel@tonic-gate 	char *p;
50*7c478bd9Sstevel@tonic-gate 	char *id;
51*7c478bd9Sstevel@tonic-gate 	char *idend;
52*7c478bd9Sstevel@tonic-gate 	char *val;
53*7c478bd9Sstevel@tonic-gate 	int i;
54*7c478bd9Sstevel@tonic-gate 
55*7c478bd9Sstevel@tonic-gate 	cfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, path, SM_IO_RDONLY, NULL);
56*7c478bd9Sstevel@tonic-gate 	if (cfp == NULL)
57*7c478bd9Sstevel@tonic-gate 		return errno;
58*7c478bd9Sstevel@tonic-gate 
59*7c478bd9Sstevel@tonic-gate 	while (sm_io_fgets(cfp, SM_TIME_DEFAULT, buf, sizeof(buf)) != NULL)
60*7c478bd9Sstevel@tonic-gate 	{
61*7c478bd9Sstevel@tonic-gate 		p = strchr(buf, '\n');
62*7c478bd9Sstevel@tonic-gate 		if (p != NULL)
63*7c478bd9Sstevel@tonic-gate 			*p = '\0';
64*7c478bd9Sstevel@tonic-gate 
65*7c478bd9Sstevel@tonic-gate 		if (buf[0] != 'O' || buf[1] != ' ')
66*7c478bd9Sstevel@tonic-gate 			continue;
67*7c478bd9Sstevel@tonic-gate 
68*7c478bd9Sstevel@tonic-gate 		id = &buf[2];
69*7c478bd9Sstevel@tonic-gate 		val = strchr(id, '=');
70*7c478bd9Sstevel@tonic-gate 		if (val == NULL)
71*7c478bd9Sstevel@tonic-gate 			val = idend = id + strlen(id);
72*7c478bd9Sstevel@tonic-gate 		else
73*7c478bd9Sstevel@tonic-gate 		{
74*7c478bd9Sstevel@tonic-gate 			idend = val;
75*7c478bd9Sstevel@tonic-gate 			++val;
76*7c478bd9Sstevel@tonic-gate 			while (*val == ' ')
77*7c478bd9Sstevel@tonic-gate 				++val;
78*7c478bd9Sstevel@tonic-gate 			while (idend > id && idend[-1] == ' ')
79*7c478bd9Sstevel@tonic-gate 				--idend;
80*7c478bd9Sstevel@tonic-gate 			*idend = '\0';
81*7c478bd9Sstevel@tonic-gate 		}
82*7c478bd9Sstevel@tonic-gate 
83*7c478bd9Sstevel@tonic-gate 		for (i = 0; i < optc; ++i)
84*7c478bd9Sstevel@tonic-gate 		{
85*7c478bd9Sstevel@tonic-gate 			if (sm_strcasecmp(optv[i].opt_name, id) == 0)
86*7c478bd9Sstevel@tonic-gate 			{
87*7c478bd9Sstevel@tonic-gate 				optv[i].opt_val = sm_strdup_x(val);
88*7c478bd9Sstevel@tonic-gate 				break;
89*7c478bd9Sstevel@tonic-gate 			}
90*7c478bd9Sstevel@tonic-gate 		}
91*7c478bd9Sstevel@tonic-gate 	}
92*7c478bd9Sstevel@tonic-gate 	if (sm_io_error(cfp))
93*7c478bd9Sstevel@tonic-gate 	{
94*7c478bd9Sstevel@tonic-gate 		int save_errno = errno;
95*7c478bd9Sstevel@tonic-gate 
96*7c478bd9Sstevel@tonic-gate 		(void) sm_io_close(cfp, SM_TIME_DEFAULT);
97*7c478bd9Sstevel@tonic-gate 		errno = save_errno;
98*7c478bd9Sstevel@tonic-gate 		return errno;
99*7c478bd9Sstevel@tonic-gate 	}
100*7c478bd9Sstevel@tonic-gate 	(void) sm_io_close(cfp, SM_TIME_DEFAULT);
101*7c478bd9Sstevel@tonic-gate 	return 0;
102*7c478bd9Sstevel@tonic-gate }
103