1N/A/*
1N/A * Copyright (c) 2001 Sendmail, Inc. and its suppliers.
1N/A * All rights reserved.
1N/A *
1N/A * By using this file, you agree to the terms and conditions set
1N/A * forth in the LICENSE file which can be found at the top level of
1N/A * the sendmail distribution.
1N/A *
1N/A */
1N/A
1N/A#pragma ident "%Z%%M% %I% %E% SMI"
1N/A
1N/A#include <sm/gen.h>
1N/ASM_RCSID("@(#)$Id: cf.c,v 1.4 2001/02/01 02:40:21 dmoen Exp $")
1N/A
1N/A#include <ctype.h>
1N/A#include <errno.h>
1N/A
1N/A#include <sm/cf.h>
1N/A#include <sm/io.h>
1N/A#include <sm/string.h>
1N/A#include <sm/heap.h>
1N/A
1N/A/*
1N/A** SM_CF_GETOPT -- look up option values in the sendmail.cf file
1N/A**
1N/A** Open the sendmail.cf file and parse all of the 'O' directives.
1N/A** Each time one of the options named in the option vector optv
1N/A** is found, store a malloced copy of the option value in optv.
1N/A**
1N/A** Parameters:
1N/A** path -- pathname of sendmail.cf file
1N/A** optc -- size of option vector
1N/A** optv -- pointer to option vector
1N/A**
1N/A** Results:
1N/A** 0 on success, or an errno value on failure.
1N/A** An exception is raised on malloc failure.
1N/A*/
1N/A
1N/Aint
1N/Asm_cf_getopt(path, optc, optv)
1N/A char *path;
1N/A int optc;
1N/A SM_CF_OPT_T *optv;
1N/A{
1N/A SM_FILE_T *cfp;
1N/A char buf[2048];
1N/A char *p;
1N/A char *id;
1N/A char *idend;
1N/A char *val;
1N/A int i;
1N/A
1N/A cfp = sm_io_open(SmFtStdio, SM_TIME_DEFAULT, path, SM_IO_RDONLY, NULL);
1N/A if (cfp == NULL)
1N/A return errno;
1N/A
1N/A while (sm_io_fgets(cfp, SM_TIME_DEFAULT, buf, sizeof(buf)) != NULL)
1N/A {
1N/A p = strchr(buf, '\n');
1N/A if (p != NULL)
1N/A *p = '\0';
1N/A
1N/A if (buf[0] != 'O' || buf[1] != ' ')
1N/A continue;
1N/A
1N/A id = &buf[2];
1N/A val = strchr(id, '=');
1N/A if (val == NULL)
1N/A val = idend = id + strlen(id);
1N/A else
1N/A {
1N/A idend = val;
1N/A ++val;
1N/A while (*val == ' ')
1N/A ++val;
1N/A while (idend > id && idend[-1] == ' ')
1N/A --idend;
1N/A *idend = '\0';
1N/A }
1N/A
1N/A for (i = 0; i < optc; ++i)
1N/A {
1N/A if (sm_strcasecmp(optv[i].opt_name, id) == 0)
1N/A {
1N/A optv[i].opt_val = sm_strdup_x(val);
1N/A break;
1N/A }
1N/A }
1N/A }
1N/A if (sm_io_error(cfp))
1N/A {
1N/A int save_errno = errno;
1N/A
1N/A (void) sm_io_close(cfp, SM_TIME_DEFAULT);
1N/A errno = save_errno;
1N/A return errno;
1N/A }
1N/A (void) sm_io_close(cfp, SM_TIME_DEFAULT);
1N/A return 0;
1N/A}