3 * Copyright (C) 2004-2008, Parrot Foundation.
10 config\gen\platform\win32\env.c
14 On Windows there are two ways to access the environment. Either through the
15 Windows environment block, using GetEnvironmentVariable,
16 SetEnvironmentVariable and GetEnvironmentStrings, or the C runtime using
17 _getenv, _putenv and _environ.
19 Changes through the C runtime are reflected in the environment block, but
20 changes in the environment block are NOT reflected in the C runtime!
22 To keep both in sync we always update environment variables through the C
23 runtime. Getting an environment variable can be done either way,
24 whichever is more convenient.
36 =item C<void Parrot_setenv(PARROT_INTERP, STRING *str_name, STRING *str_value)>
38 Sets the environment variable C<str_name> to the value C<str_value>. Creates the
39 environment variable if it does not exist, and silently overwrite a variable if
47 Parrot_setenv(PARROT_INTERP
, STRING
*str_name
, STRING
*str_value
)
49 char * const name
= Parrot_str_to_cstring(interp
, str_name
);
50 char * const value
= Parrot_str_to_cstring(interp
, str_value
);
52 assert(value
!= NULL
);
55 const int name_len
= strlen(name
);
56 const int value_len
= strlen(value
);
59 char * const envstring
= (char * const)mem_internal_allocate(
62 + value_len
/* value */
63 + 1); /* string terminator */
65 /* Save a bit of time, by using the fact we already have the
66 lengths, avoiding strcat */
67 strcpy(envstring
, name
);
68 strcpy(envstring
+ name_len
, "=");
69 strcpy(envstring
+ name_len
+ 1, value
);
71 Parrot_str_free_cstring(name
);
72 Parrot_str_free_cstring(value
);
74 if (_putenv(envstring
) == 0) {
76 mem_sys_free(envstring
);
79 mem_sys_free(envstring
);
80 exit_fatal(1, "Unable to set environment variable %s=%s",
89 =item C<char * Parrot_getenv(PARROT_INTERP, STRING *str_name)>
91 Gets the environment variable C<str_name>, if it exists. Returns the contents
92 of the environment variable in a C<malloc>'d memory location that needs to be
100 Parrot_getenv(PARROT_INTERP
, ARGIN(STRING
*str_name
))
102 char * const name
= Parrot_str_to_cstring(interp
, str_name
);
103 const DWORD size
= GetEnvironmentVariable(name
, NULL
, 0);
107 Parrot_str_free_cstring(name
);
110 buffer
= (char *)mem_sys_allocate(size
);
111 GetEnvironmentVariable(name
, buffer
, size
);
112 Parrot_str_free_cstring(name
);
119 =item C<void Parrot_unsetenv(PARROT_INTERP, STRING *name)>
121 Deletes an environment variable by assigning an empty string to the specified variable.
128 Parrot_unsetenv(PARROT_INTERP
, STRING
*name
)
130 /* You can remove a variable from the environment by specifying an empty
131 string -- in other words, by specifying only varname=.
132 -- _putenv, _wputenv (CRT) documentation
134 Parrot_setenv(interp
, name
, Parrot_str_new(interp
, "", 0));
147 * c-file-style: "parrot"
149 * vim: expandtab shiftwidth=4: