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