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