Guido van Rossum | 31affb2 | 1995-06-14 22:31:38 +0000 | [diff] [blame^] | 1 | /* |
| 2 | # Copyright 1995, InfoSeek Corporation |
| 3 | # All rights reserved. |
| 4 | # Written by Andy Bensky |
| 5 | # |
| 6 | # Permission to use, copy, modify, and distribute this Python software |
| 7 | # and its associated documentation for any purpose (subject to the |
| 8 | # restriction in the following sentence) without fee is hereby granted, |
| 9 | # provided that the above copyright notice appears in all copies, and |
| 10 | # that both that copyright notice and this permission notice appear in |
| 11 | # supporting documentation, and that the name of InfoSeek not be used in |
| 12 | # advertising or publicity pertaining to distribution of the software |
| 13 | # without specific, prior written permission. This permission is |
| 14 | # explicitly restricted to the copying and modification of the software |
| 15 | # to remain in Python, compiled Python, or other languages (such as C) |
| 16 | # wherein the modified or derived code is exclusively imported into a |
| 17 | # Python module. |
| 18 | # |
| 19 | # INFOSEEK CORPORATION DISCLAIMS ALL WARRANTIES WITH REGARD TO THIS |
| 20 | # SOFTWARE, INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND |
| 21 | # FITNESS. IN NO EVENT SHALL INFOSEEK CORPORATION BE LIABLE FOR ANY |
| 22 | # DIRECT, SPECIAL, INDIRECT OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES |
| 23 | # WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN |
| 24 | # AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING |
| 25 | # OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE, |
| 26 | # EVEN IF INFOSEEK SHALL HAVE BEEN MADE AWARE OF THE POSSIBILITY OF SUCH |
| 27 | # DAMAGES. |
| 28 | */ |
| 29 | |
| 30 | /* Hooks to call the Unix putenv() to modify the environment |
| 31 | */ |
| 32 | |
| 33 | #include "allobjects.h" |
| 34 | #include <stdlib.h> |
| 35 | #include <assert.h> |
| 36 | #include <string.h> |
| 37 | |
| 38 | /* Error conditions that can be raised */ |
| 39 | |
| 40 | /* Headers for functions accessible from Python as module methods */ |
| 41 | static object *put_environ( object *self, object *args ); |
| 42 | |
| 43 | static struct methodlist environ_methods[] = { |
| 44 | {"putenv", put_environ}, |
| 45 | {NULL, NULL} |
| 46 | }; |
| 47 | |
| 48 | |
| 49 | /* |
| 50 | * Name: initenvironment |
| 51 | * Description: |
| 52 | * Initialzation function that Python will use to establish callbacks to |
| 53 | * the methods of this module. |
| 54 | * |
| 55 | * Returns: |
| 56 | * void - |
| 57 | * |
| 58 | * Notes: |
| 59 | */ |
| 60 | void initenvironment() |
| 61 | { |
| 62 | object *m, *d; |
| 63 | |
| 64 | m = initmodule("environment", environ_methods); |
| 65 | d = getmoduledict(m); |
| 66 | } |
| 67 | |
| 68 | /* |
| 69 | * Name: put_environ |
| 70 | * Description: |
| 71 | * accepts 2 string objects as arguments and forms a string of the |
| 72 | * form string1=string2 that can be passed to the putenv() system call. |
| 73 | * |
| 74 | * Returns: |
| 75 | * None object if successfull, otherwise raises a SystemError exception |
| 76 | * |
| 77 | * |
| 78 | * Notes: |
| 79 | */ |
| 80 | static object *put_environ( object *self, object *args ) |
| 81 | { |
| 82 | char *string1, *string2; |
| 83 | char *set_str; |
| 84 | object *return_object = None; |
| 85 | |
| 86 | if (args && getargs(args, "(ss)", &string1, &string2)) |
| 87 | { |
| 88 | set_str = malloc(strlen(string1) + strlen(string2) + 2); |
| 89 | assert( set_str ); |
| 90 | (void) sprintf(set_str, "%s=%s", string1, string2); |
| 91 | if ( putenv( set_str ) ) |
| 92 | { |
| 93 | err_setstr(SystemError, "Error in system putenv call."); |
| 94 | return_object = 0; |
| 95 | } |
| 96 | } |
| 97 | else |
| 98 | { |
| 99 | err_setstr(TypeError, "Usage: putenv(string1, string2)"); |
| 100 | return_object = 0; |
| 101 | } |
| 102 | |
| 103 | return( return_object ); |
| 104 | } |