Sync usage with man page.
[netbsd-mini2440.git] / external / ibm-public / postfix / dist / src / util / dict_env.c
blob5ebcf081117c51c0d59642f3ed94315ed5b57130
1 /* $NetBSD$ */
3 /*++
4 /* NAME
5 /* dict_env 3
6 /* SUMMARY
7 /* dictionary manager interface to environment variables
8 /* SYNOPSIS
9 /* #include <dict_env.h>
11 /* DICT *dict_env_open(name, dummy, dict_flags)
12 /* const char *name;
13 /* int dummy;
14 /* int dict_flags;
15 /* DESCRIPTION
16 /* dict_env_open() opens the environment variable array and
17 /* makes it accessible via the generic operations documented
18 /* in dict_open(3). The \fIname\fR and \fIdummy\fR arguments
19 /* are ignored.
20 /* SEE ALSO
21 /* dict(3) generic dictionary manager
22 /* safe_getenv(3) safe getenv() interface
23 /* LICENSE
24 /* .ad
25 /* .fi
26 /* The Secure Mailer license must be distributed with this software.
27 /* AUTHOR(S)
28 /* Wietse Venema
29 /* IBM T.J. Watson Research
30 /* P.O. Box 704
31 /* Yorktown Heights, NY 10598, USA
32 /*--*/
34 /* System library. */
36 #include "sys_defs.h"
37 #include <stdio.h> /* sprintf() prototype */
38 #include <stdlib.h>
39 #include <unistd.h>
40 #include <string.h>
42 /* Utility library. */
44 #include "mymalloc.h"
45 #include "msg.h"
46 #include "safe.h"
47 #include "stringops.h"
48 #include "dict.h"
49 #include "dict_env.h"
51 /* dict_env_update - update environment array */
53 static void dict_env_update(DICT *dict, const char *name, const char *value)
57 * Optionally fold the key.
59 if (dict->flags & DICT_FLAG_FOLD_FIX) {
60 if (dict->fold_buf == 0)
61 dict->fold_buf = vstring_alloc(10);
62 vstring_strcpy(dict->fold_buf, name);
63 name = lowercase(vstring_str(dict->fold_buf));
65 if (setenv(name, value, 1))
66 msg_fatal("setenv: %m");
69 /* dict_env_lookup - access environment array */
71 static const char *dict_env_lookup(DICT *dict, const char *name)
73 dict_errno = 0;
76 * Optionally fold the key.
78 if (dict->flags & DICT_FLAG_FOLD_FIX) {
79 if (dict->fold_buf == 0)
80 dict->fold_buf = vstring_alloc(10);
81 vstring_strcpy(dict->fold_buf, name);
82 name = lowercase(vstring_str(dict->fold_buf));
84 return (safe_getenv(name));
87 /* dict_env_close - close environment dictionary */
89 static void dict_env_close(DICT *dict)
91 if (dict->fold_buf)
92 vstring_free(dict->fold_buf);
93 dict_free(dict);
96 /* dict_env_open - make association with environment array */
98 DICT *dict_env_open(const char *name, int unused_flags, int dict_flags)
100 DICT *dict;
102 dict = dict_alloc(DICT_TYPE_ENVIRON, name, sizeof(*dict));
103 dict->lookup = dict_env_lookup;
104 dict->update = dict_env_update;
105 dict->close = dict_env_close;
106 dict->flags = dict_flags | DICT_FLAG_FIXED;
107 if (dict_flags & DICT_FLAG_FOLD_FIX)
108 dict->fold_buf = vstring_alloc(10);
109 return (DICT_DEBUG (dict));