7 /* clean up the environment
9 /* #include <clean_env.h>
11 /* void clean_env(preserve_list)
12 /* const char **preserve_list;
14 /* clean_env() reduces the process environment to the bare minimum.
15 /* The function takes a null-terminated list of arguments.
16 /* Each argument specifies the name of an environment variable
17 /* that should be preserved, or specifies a name=value that should
18 /* be entered into the new environment.
20 /* Fatal error: out of memory.
22 /* safe_getenv(3), guarded getenv()
26 /* The Secure Mailer license must be distributed with this software.
29 /* IBM T.J. Watson Research
31 /* Yorktown Heights, NY 10598, USA
41 /* Utility library. */
46 #include <clean_env.h>
48 /* clean_env - clean up the environment */
50 void clean_env(char **preserve_list
)
52 extern char **environ
;
59 * Preserve or specify selected environment variables.
61 #define STRING_AND_LENGTH(x, y) (x), (ssize_t) (y)
63 save_list
= argv_alloc(10);
64 for (cpp
= preserve_list
; *cpp
; cpp
++)
65 if ((eq
= strchr(*cpp
, '=')) != 0)
66 argv_addn(save_list
, STRING_AND_LENGTH(*cpp
, eq
- *cpp
),
67 STRING_AND_LENGTH(eq
+ 1, strlen(eq
+ 1)), (char *) 0);
68 else if ((value
= safe_getenv(*cpp
)) != 0)
69 argv_add(save_list
, *cpp
, value
, (char *) 0);
72 * Truncate the process environment, if available. On some systems
73 * (Ultrix!), environ can be a null pointer.
79 * Restore preserved environment variables.
81 for (cpp
= save_list
->argv
; *cpp
; cpp
+= 2)
82 if (setenv(cpp
[0], cpp
[1], 1))
83 msg_fatal("setenv(%s, %s): %m", cpp
[0], cpp
[1]);