1 /* $NetBSD: dict_env.c,v 1.1.1.1 2009/06/23 10:08:59 tron Exp $ */ 2 3 /*++ 4 /* NAME 5 /* dict_env 3 6 /* SUMMARY 7 /* dictionary manager interface to environment variables 8 /* SYNOPSIS 9 /* #include <dict_env.h> 10 /* 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 /*--*/ 33 34 /* System library. */ 35 36 #include "sys_defs.h" 37 #include <stdio.h> /* sprintf() prototype */ 38 #include <stdlib.h> 39 #include <unistd.h> 40 #include <string.h> 41 42 /* Utility library. */ 43 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" 50 51 /* dict_env_update - update environment array */ 52 53 static void dict_env_update(DICT *dict, const char *name, const char *value) 54 { 55 56 /* 57 * Optionally fold the key. 58 */ 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)); 64 } 65 if (setenv(name, value, 1)) 66 msg_fatal("setenv: %m"); 67 } 68 69 /* dict_env_lookup - access environment array */ 70 71 static const char *dict_env_lookup(DICT *dict, const char *name) 72 { 73 dict_errno = 0; 74 75 /* 76 * Optionally fold the key. 77 */ 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)); 83 } 84 return (safe_getenv(name)); 85 } 86 87 /* dict_env_close - close environment dictionary */ 88 89 static void dict_env_close(DICT *dict) 90 { 91 if (dict->fold_buf) 92 vstring_free(dict->fold_buf); 93 dict_free(dict); 94 } 95 96 /* dict_env_open - make association with environment array */ 97 98 DICT *dict_env_open(const char *name, int unused_flags, int dict_flags) 99 { 100 DICT *dict; 101 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)); 110 } 111