~ubuntu-branches/ubuntu/utopic/gridengine/utopic

« back to all changes in this revision

Viewing changes to source/libs/uti/sge_stdlib.c

  • Committer: Bazaar Package Importer
  • Author(s): Mark Hymers
  • Date: 2008-06-25 22:36:13 UTC
  • Revision ID: james.westby@ubuntu.com-20080625223613-tvd9xlhuoct9kyhm
Tags: upstream-6.2~beta2
ImportĀ upstreamĀ versionĀ 6.2~beta2

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*___INFO__MARK_BEGIN__*/
 
2
/*************************************************************************
 
3
 *
 
4
 *  The Contents of this file are made available subject to the terms of
 
5
 *  the Sun Industry Standards Source License Version 1.2
 
6
 *
 
7
 *  Sun Microsystems Inc., March, 2001
 
8
 *
 
9
 *
 
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
 
16
 *
 
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.
 
23
 *
 
24
 *   The Initial Developer of the Original Code is: Sun Microsystems, Inc.
 
25
 *
 
26
 *   Copyright: 2001 by Sun Microsystems, Inc.
 
27
 *
 
28
 *   All Rights Reserved.
 
29
 *
 
30
 ************************************************************************/
 
31
/*___INFO__MARK_END__*/
 
32
 
 
33
#include <stdio.h>
 
34
#include <stdlib.h>
 
35
#include <string.h>
 
36
#include <limits.h>
 
37
#include <unistd.h>
 
38
 
 
39
#include "sge_stdlib.h"
 
40
#include "sge_dstring.h"
 
41
#include "sgermon.h"
 
42
#include "sge_log.h" 
 
43
#include "msg_utilib.h"
 
44
 
 
45
/****** uti/stdlib/sge_malloc() ***********************************************
 
46
*  NAME
 
47
*     sge_malloc() -- replacement for malloc() 
 
48
*
 
49
*  SYNOPSIS
 
50
*     char* sge_malloc(int size) 
 
51
*
 
52
*  FUNCTION
 
53
*     Allocates a memory block. Initilizes the block (0). Aborts in case
 
54
*     of error. 
 
55
*
 
56
*  INPUTS
 
57
*     int size - size in bytes 
 
58
*
 
59
*  RESULT
 
60
*     char* - pointer to memory block
 
61
*
 
62
*  NOTES
 
63
*     MT-NOTE: sge_malloc() is MT safe
 
64
******************************************************************************/
 
65
char *sge_malloc(int size) 
 
66
{
 
67
   char *cp = NULL;
 
68
 
 
69
   DENTER_(BASIS_LAYER, "sge_malloc");
 
70
 
 
71
   if (!size) {
 
72
      DRETURN_(NULL);
 
73
   }
 
74
 
 
75
   cp = (char *) malloc(size);
 
76
   if (!cp) {
 
77
      CRITICAL((SGE_EVENT, MSG_MEMORY_MALLOCFAILED));
 
78
      DEXIT_;
 
79
      abort();
 
80
   }
 
81
 
 
82
   DRETURN_(cp);
 
83
}   
 
84
 
 
85
/****** uti/stdlib/sge_realloc() **********************************************
 
86
*  NAME
 
87
*     sge_realloc() -- replacement for realloc 
 
88
*
 
89
*  SYNOPSIS
 
90
*     char* sge_realloc(char *ptr, int size, int abort) 
 
91
*
 
92
*  FUNCTION
 
93
*     Reallocates a memory block. Aborts in case of an error. 
 
94
*
 
95
*  INPUTS
 
96
*     char *ptr - pointer to a memory block
 
97
*     int size  - new size
 
98
*     int abort - do abort when realloc fails?
 
99
*
 
100
*  RESULT
 
101
*     char* - pointer to the (new) memory block
 
102
*
 
103
*  NOTES
 
104
*     MT-NOTE: sge_realloc() is MT safe
 
105
******************************************************************************/
 
106
void *sge_realloc(void *ptr, int size, int do_abort)
 
107
{
 
108
   void *cp = NULL;
 
109
 
 
110
   DENTER_(BASIS_LAYER, "sge_realloc");
 
111
 
 
112
   /* if new size is 0, just free the currently allocated memory */
 
113
   if (size == 0) {
 
114
      FREE(ptr);
 
115
      DRETURN(NULL);
 
116
   }
 
117
 
 
118
   cp = realloc(ptr, size);
 
119
   if (cp == NULL) {
 
120
      CRITICAL((SGE_EVENT, MSG_MEMORY_REALLOCFAILED));
 
121
      if (do_abort) {
 
122
         DEXIT;
 
123
         abort();
 
124
      } else {
 
125
         FREE(ptr);
 
126
      }
 
127
   }
 
128
 
 
129
   DRETURN_(cp);
 
130
}
 
131
 
 
132
/****** uti/stdlib/sge_free() *************************************************
 
133
*  NAME
 
134
*     sge_free() -- replacement for free 
 
135
*
 
136
*  SYNOPSIS
 
137
*     char* sge_free(char *cp) 
 
138
*
 
139
*  FUNCTION
 
140
*     Replacement for free(). Accepts NULL pointers.
 
141
*
 
142
*  INPUTS
 
143
*     char *cp - pointer to a memory block 
 
144
*
 
145
*  RESULT
 
146
*     char* - NULL
 
147
*
 
148
*  NOTES
 
149
*     MT-NOTE: sge_free() is MT safe
 
150
******************************************************************************/
 
151
char *sge_free(char *cp) 
 
152
{
 
153
   if (cp != NULL) {
 
154
      free(cp);
 
155
   }
 
156
   return NULL;
 
157
}  
 
158
 
 
159
/****** uti/stdlib/sge_getenv() ***********************************************
 
160
*  NAME
 
161
*     sge_getenv() -- get an environment variable 
 
162
*
 
163
*  SYNOPSIS
 
164
*     const char* sge_getenv(const char *env_str) 
 
165
*
 
166
*  FUNCTION
 
167
*     The function searches the environment list for a
 
168
*     string that matches the string pointed to by 'env_str'.
 
169
*
 
170
*  INPUTS
 
171
*     const char *env_str - name of env. varibale 
 
172
*
 
173
*  RESULT
 
174
*     const char* - value
 
175
*
 
176
*  SEE ALSO
 
177
*     uti/stdlib/sge_putenv()
 
178
*     uti/stdlib/sge_setenv() 
 
179
*
 
180
*  NOTES
 
181
*     MT-NOTE: sge_getenv() is MT safe
 
182
******************************************************************************/
 
183
const char *sge_getenv(const char *env_str) 
 
184
{
 
185
   const char *cp=NULL;
 
186
 
 
187
   DENTER_(BASIS_LAYER, "sge_getenv");
 
188
 
 
189
   cp = (char *) getenv(env_str);
 
190
 
 
191
   DRETURN_(cp);
 
192
}    
 
193
 
 
194
/****** uti/stdlib/sge_putenv() ***********************************************
 
195
*  NAME
 
196
*     sge_putenv() -- put an environment variable to environment
 
197
*
 
198
*  SYNOPSIS
 
199
*     static int sge_putenv(const char *var) 
 
200
*
 
201
*  FUNCTION
 
202
*     Duplicates the given environment variable and calls the system call
 
203
*     putenv.
 
204
*
 
205
*  INPUTS
 
206
*     const char *var - variable to put in the form <name>=<value>
 
207
*
 
208
*  RESULT
 
209
*     static int - 1 on success, else 0
 
210
*
 
211
*  SEE ALSO
 
212
*     uti/stdlib/sge_setenv() 
 
213
*     uti/stdlib/sge_getenv()
 
214
*
 
215
*  NOTES
 
216
*     MT-NOTE: sge_putenv() is MT safe
 
217
*******************************************************************************/
 
218
int sge_putenv(const char *var)
 
219
{
 
220
   char *duplicate;
 
221
 
 
222
   if(var == NULL) {
 
223
      return 0;
 
224
   }
 
225
 
 
226
   duplicate = strdup(var);
 
227
 
 
228
   if(duplicate == NULL) {
 
229
      return 0;
 
230
   }
 
231
 
 
232
   if(putenv(duplicate) != 0) {
 
233
      return 0;
 
234
   }
 
235
 
 
236
   return 1;
 
237
}
 
238
 
 
239
/****** uti/stdlib/sge_setenv() ***********************************************
 
240
*  NAME
 
241
*     sge_setenv() -- Change or add an environment variable 
 
242
*
 
243
*  SYNOPSIS
 
244
*     int sge_setenv(const char *name, const char *value) 
 
245
*
 
246
*  FUNCTION
 
247
*     Change or add an environment variable 
 
248
*
 
249
*  INPUTS
 
250
*     const char *name  - variable name 
 
251
*     const char *value - new value 
 
252
*
 
253
*  RESULT
 
254
*     int - error state
 
255
*         1 - success
 
256
*         0 - error 
 
257
*
 
258
*  SEE ALSO
 
259
*     uti/stdlib/sge_putenv() 
 
260
*     uti/stdlib/sge_getenv()
 
261
*     uti/stdio/addenv()
 
262
*
 
263
*  NOTES
 
264
*     MT-NOTE: sge_setenv() is MT safe
 
265
*******************************************************************************/
 
266
int sge_setenv(const char *name, const char *value)
 
267
{
 
268
   int ret = 0;
 
269
 
 
270
   if (name != NULL && value != NULL) {
 
271
      dstring variable = DSTRING_INIT;
 
272
 
 
273
      sge_dstring_sprintf(&variable, "%s=%s", name, value);
 
274
      ret = sge_putenv(sge_dstring_get_string(&variable));
 
275
      sge_dstring_free(&variable);
 
276
   }
 
277
   return ret;
 
278
}
 
279
 
 
280
 
 
281
/****** sge_env/sge_unsetenv() *************************************************
 
282
*  NAME
 
283
*     sge_unsetenv() -- unset environment variable
 
284
*
 
285
*  SYNOPSIS
 
286
*     void sge_unsetenv(const char* varName) 
 
287
*
 
288
*  FUNCTION
 
289
*     Some architectures doesn't support unsetenv(), sge_unsetenv() is used
 
290
*     to unset an environment variable. 
 
291
*
 
292
*  INPUTS
 
293
*     const char* varName - name of envirionment variable
 
294
*
 
295
*  RESULT
 
296
*     void - no return value
 
297
*
 
298
*  NOTES
 
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;
 
305
 
 
306
   if (varName != NULL) {
 
307
      size_t length = (strlen(varName) + 2) * sizeof(char);
 
308
      searchString = malloc(length);
 
309
      if (searchString != NULL) {
 
310
         bool found = false;
 
311
         int i;
 
312
         snprintf(searchString, length, "%s=", varName);
 
313
         
 
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) {
 
317
               found = true;
 
318
               break;
 
319
            }
 
320
         }
 
321
        
 
322
         /* At second we remove varName by copying varName+1 to varName */ 
 
323
         if (found == true) {
 
324
            for (; i < ARG_MAX-1 && environ[i] != NULL; i++) {
 
325
               environ[i] = environ[i+1];
 
326
            }
 
327
            environ[i] = NULL; 
 
328
         }
 
329
         FREE(searchString);
 
330
      }
 
331
   }
 
332
#else
 
333
   unsetenv(varName);
 
334
#endif
 
335
}
 
336
 
 
337
 
 
338