8322 nl: misleading-indentation
[unleashed/tickless.git] / usr / src / cmd / sendmail / libsm / cf.c
blobc1bf90d8142f61f52a41f73b4f238d0e1b7d6b5d
1 /*
2 * Copyright (c) 2001 Sendmail, Inc. and its suppliers.
3 * All rights reserved.
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.
9 */
11 #pragma ident "%Z%%M% %I% %E% SMI"
13 #include <sm/gen.h>
14 SM_RCSID("@(#)$Id: cf.c,v 1.4 2001/02/01 02:40:21 dmoen Exp $")
16 #include <ctype.h>
17 #include <errno.h>
19 #include <sm/cf.h>
20 #include <sm/io.h>
21 #include <sm/string.h>
22 #include <sm/heap.h>
25 ** SM_CF_GETOPT -- look up option values in the sendmail.cf file
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.
31 ** Parameters:
32 ** path -- pathname of sendmail.cf file
33 ** optc -- size of option vector
34 ** optv -- pointer to option vector
36 ** Results:
37 ** 0 on success, or an errno value on failure.
38 ** An exception is raised on malloc failure.
41 int
42 sm_cf_getopt(path, optc, optv)
43 char *path;
44 int optc;
45 SM_CF_OPT_T *optv;
47 SM_FILE_T *cfp;
48 char buf[2048];
49 char *p;
50 char *id;
51 char *idend;
52 char *val;
53 int i;
55 cfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, path, SM_IO_RDONLY, NULL);
56 if (cfp == NULL)
57 return errno;
59 while (sm_io_fgets(cfp, SM_TIME_DEFAULT, buf, sizeof(buf)) != NULL)
61 p = strchr(buf, '\n');
62 if (p != NULL)
63 *p = '\0';
65 if (buf[0] != 'O' || buf[1] != ' ')
66 continue;
68 id = &buf[2];
69 val = strchr(id, '=');
70 if (val == NULL)
71 val = idend = id + strlen(id);
72 else
74 idend = val;
75 ++val;
76 while (*val == ' ')
77 ++val;
78 while (idend > id && idend[-1] == ' ')
79 --idend;
80 *idend = '\0';
83 for (i = 0; i < optc; ++i)
85 if (sm_strcasecmp(optv[i].opt_name, id) == 0)
87 optv[i].opt_val = sm_strdup_x(val);
88 break;
92 if (sm_io_error(cfp))
94 int save_errno = errno;
96 (void) sm_io_close(cfp, SM_TIME_DEFAULT);
97 errno = save_errno;
98 return errno;
100 (void) sm_io_close(cfp, SM_TIME_DEFAULT);
101 return 0;