Merge branch 'debian'
[hcoop/debian/courier-authlib.git] / authoption.c
CommitLineData
d9898ee8 1/*
8d138742 2** Copyright 2002-2009 Double Precision, Inc. See COPYING for
d9898ee8 3** distribution information.
4*/
5
6#if HAVE_CONFIG_H
7#include "courier_auth_config.h"
8#endif
9#include <stdio.h>
10#include <stdlib.h>
11#include <string.h>
12#include <errno.h>
13#include <pwd.h>
14#if HAVE_UNISTD_H
15#include <unistd.h>
16#endif
17
18#include "auth.h"
19
8d138742 20static const char rcsid[]="$Id: authoption.c,v 1.4 2009/11/22 03:42:48 mrsam Exp $";
d9898ee8 21
22
23int auth_getoptionenvint(const char *keyword)
24{
25 char *p = auth_getoptionenv(keyword);
26 int i;
27
28 if (!p) return 0;
29
30 i = atoi(p);
8d138742
CE
31
32 if (i == 0 && strchr("tTyY", *p))
33 i=1; /* Convert 'true', 'TRUE', 'yes', 'YES' to 1 */
d9898ee8 34 free(p);
35 return i;
36}
37
38char *auth_getoptionenv(const char *keyword)
39{
40 return auth_getoption(getenv("OPTIONS"), keyword);
41}
42
43char *auth_getoption(const char *options, const char *keyword)
44{
45 size_t keyword_l=strlen(keyword);
46 char *p;
47
48 while (options)
49 {
50 if (strncmp(options, keyword, keyword_l) == 0)
51 {
52 if (options[keyword_l] == 0 ||
53 options[keyword_l] == ',')
54 return strdup("");
55
56 if (options[keyword_l] == '=')
57 {
58 options += keyword_l;
59 ++options;
60
61 for (keyword_l=0;
62 options[keyword_l] &&
63 options[keyword_l] != ',';
64 ++keyword_l)
65 ;
66
67 if (!(p=malloc(keyword_l+1)))
68 return NULL;
69 memcpy(p, options, keyword_l);
70 p[keyword_l]=0;
71 return p;
72 }
73 }
74
75 options=strchr(options, ',');
76 if (options)
77 ++options;
78 }
79 errno=ENOENT;
80 return NULL;
81}