fix codetest failure - ASSERT_ARGS does not have a ; after and
[parrot.git] / config / gen / platform / win32 / env.c
blobf2b9ed29b1aca36d5eeb281a603ed8ce2fbc34b0
1 /*
2 * $Id$
3 * Copyright (C) 2004-2008, Parrot Foundation.
4 */
6 /*
8 =head1 NAME
10 config\gen\platform\win32\env.c
12 =head1 DESCRIPTION
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.
26 =head2 Functions
28 =over 4
30 =cut
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
40 it does exist.
42 =cut
46 void
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);
51 assert(name != NULL);
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(
60 name_len /* name */
61 + 1 /* '=' */
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) {
75 /* success */
76 mem_sys_free(envstring);
78 else {
79 mem_sys_free(envstring);
80 exit_fatal(1, "Unable to set environment variable %s=%s",
81 name, value);
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
93 freed later.
95 =cut
99 char *
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);
104 char *buffer = NULL;
106 if (size == 0) {
107 Parrot_str_free_cstring(name);
108 return NULL;
110 buffer = (char *)mem_sys_allocate(size);
111 GetEnvironmentVariable(name, buffer, size);
112 Parrot_str_free_cstring(name);
114 return buffer;
119 =item C<void Parrot_unsetenv(PARROT_INTERP, STRING *name)>
121 Deletes an environment variable by assigning an empty string to the specified variable.
123 =cut
127 void
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));
139 =back
141 =cut
146 * Local variables:
147 * c-file-style: "parrot"
148 * End:
149 * vim: expandtab shiftwidth=4: