1
/*___INFO__MARK_BEGIN__*/
2
/*************************************************************************
4
* The Contents of this file are made available subject to the terms of
5
* the Sun Industry Standards Source License Version 1.2
7
* Sun Microsystems Inc., March, 2001
10
* Sun Industry Standards Source License Version 1.2
11
* =================================================
12
* The contents of this file are subject to the Sun Industry Standards
13
* Source License Version 1.2 (the "License"); You may not use this file
14
* except in compliance with the License. You may obtain a copy of the
15
* License at http://gridengine.sunsource.net/Gridengine_SISSL_license.html
17
* Software provided under this License is provided on an "AS IS" basis,
18
* WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED OR IMPLIED, INCLUDING,
19
* WITHOUT LIMITATION, WARRANTIES THAT THE SOFTWARE IS FREE OF DEFECTS,
20
* MERCHANTABLE, FIT FOR A PARTICULAR PURPOSE, OR NON-INFRINGING.
21
* See the License for the specific provisions governing your rights and
22
* obligations concerning the Software.
24
* The Initial Developer of the Original Code is: Sun Microsystems, Inc.
26
* Copyright: 2001 by Sun Microsystems, Inc.
28
* All Rights Reserved.
30
************************************************************************/
31
/*___INFO__MARK_END__*/
39
#include "sge_stdlib.h"
40
#include "sge_dstring.h"
43
#include "msg_utilib.h"
45
/****** uti/stdlib/sge_malloc() ***********************************************
47
* sge_malloc() -- replacement for malloc()
50
* char* sge_malloc(int size)
53
* Allocates a memory block. Initilizes the block (0). Aborts in case
57
* int size - size in bytes
60
* char* - pointer to memory block
63
* MT-NOTE: sge_malloc() is MT safe
64
******************************************************************************/
65
char *sge_malloc(int size)
69
DENTER_(BASIS_LAYER, "sge_malloc");
75
cp = (char *) malloc(size);
77
CRITICAL((SGE_EVENT, MSG_MEMORY_MALLOCFAILED));
85
/****** uti/stdlib/sge_realloc() **********************************************
87
* sge_realloc() -- replacement for realloc
90
* char* sge_realloc(char *ptr, int size, int abort)
93
* Reallocates a memory block. Aborts in case of an error.
96
* char *ptr - pointer to a memory block
98
* int abort - do abort when realloc fails?
101
* char* - pointer to the (new) memory block
104
* MT-NOTE: sge_realloc() is MT safe
105
******************************************************************************/
106
void *sge_realloc(void *ptr, int size, int do_abort)
110
DENTER_(BASIS_LAYER, "sge_realloc");
112
/* if new size is 0, just free the currently allocated memory */
118
cp = realloc(ptr, size);
120
CRITICAL((SGE_EVENT, MSG_MEMORY_REALLOCFAILED));
132
/****** uti/stdlib/sge_free() *************************************************
134
* sge_free() -- replacement for free
137
* char* sge_free(char *cp)
140
* Replacement for free(). Accepts NULL pointers.
143
* char *cp - pointer to a memory block
149
* MT-NOTE: sge_free() is MT safe
150
******************************************************************************/
151
char *sge_free(char *cp)
159
/****** uti/stdlib/sge_getenv() ***********************************************
161
* sge_getenv() -- get an environment variable
164
* const char* sge_getenv(const char *env_str)
167
* The function searches the environment list for a
168
* string that matches the string pointed to by 'env_str'.
171
* const char *env_str - name of env. varibale
174
* const char* - value
177
* uti/stdlib/sge_putenv()
178
* uti/stdlib/sge_setenv()
181
* MT-NOTE: sge_getenv() is MT safe
182
******************************************************************************/
183
const char *sge_getenv(const char *env_str)
187
DENTER_(BASIS_LAYER, "sge_getenv");
189
cp = (char *) getenv(env_str);
194
/****** uti/stdlib/sge_putenv() ***********************************************
196
* sge_putenv() -- put an environment variable to environment
199
* static int sge_putenv(const char *var)
202
* Duplicates the given environment variable and calls the system call
206
* const char *var - variable to put in the form <name>=<value>
209
* static int - 1 on success, else 0
212
* uti/stdlib/sge_setenv()
213
* uti/stdlib/sge_getenv()
216
* MT-NOTE: sge_putenv() is MT safe
217
*******************************************************************************/
218
int sge_putenv(const char *var)
226
duplicate = strdup(var);
228
if(duplicate == NULL) {
232
if(putenv(duplicate) != 0) {
239
/****** uti/stdlib/sge_setenv() ***********************************************
241
* sge_setenv() -- Change or add an environment variable
244
* int sge_setenv(const char *name, const char *value)
247
* Change or add an environment variable
250
* const char *name - variable name
251
* const char *value - new value
259
* uti/stdlib/sge_putenv()
260
* uti/stdlib/sge_getenv()
264
* MT-NOTE: sge_setenv() is MT safe
265
*******************************************************************************/
266
int sge_setenv(const char *name, const char *value)
270
if (name != NULL && value != NULL) {
271
dstring variable = DSTRING_INIT;
273
sge_dstring_sprintf(&variable, "%s=%s", name, value);
274
ret = sge_putenv(sge_dstring_get_string(&variable));
275
sge_dstring_free(&variable);
281
/****** sge_env/sge_unsetenv() *************************************************
283
* sge_unsetenv() -- unset environment variable
286
* void sge_unsetenv(const char* varName)
289
* Some architectures doesn't support unsetenv(), sge_unsetenv() is used
290
* to unset an environment variable.
293
* const char* varName - name of envirionment variable
296
* void - no return value
299
* MT-NOTE: sge_unsetenv() is not MT safe
300
*******************************************************************************/
301
void sge_unsetenv(const char* varName) {
302
#ifdef USE_SGE_UNSETENV
303
extern char **environ;
304
char* searchString = NULL;
306
if (varName != NULL) {
307
size_t length = (strlen(varName) + 2) * sizeof(char);
308
searchString = malloc(length);
309
if (searchString != NULL) {
312
snprintf(searchString, length, "%s=", varName);
314
/* At first we have to search the index of varName */
315
for (i=0; i < ARG_MAX && environ[i] != NULL; i++) {
316
if (strstr(environ[i],searchString) != NULL) {
322
/* At second we remove varName by copying varName+1 to varName */
324
for (; i < ARG_MAX-1 && environ[i] != NULL; i++) {
325
environ[i] = environ[i+1];