/[pdpsoft]/trunk/grid-mw-security/cgul/environ/environ.c
ViewVC logotype

Diff of /trunk/grid-mw-security/cgul/environ/environ.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

revision 1451 by msalle, Tue Feb 2 02:35:46 2010 UTC revision 1452 by msalle, Tue Feb 2 19:09:23 2010 UTC
# Line 77  int getvarname(char *varname,const int m Line 77  int getvarname(char *varname,const int m
77   * Finds index in **src matching variable name, in case of no match (including   * Finds index in **src matching variable name, in case of no match (including
78   * env==NULL) returns -1   * env==NULL) returns -1
79   */   */
80  int getvarindex(const char **src, const char *varname)   {  int getvarindex(const env_t src, const char *varname)   {
81      int i,len;      int i,len;
82    
83      if (src==NULL || varname==NULL)      if (src==NULL || varname==NULL)
# Line 98  int getvarindex(const char **src, const Line 98  int getvarindex(const char **src, const
98   * Finds the value of varname in src list. Returns NULL when no match, incl.   * Finds the value of varname in src list. Returns NULL when no match, incl.
99   * NULL valued src or varname.   * NULL valued src or varname.
100   */   */
101  char *getenv_src(const char **src, char *varname)       {  char *getenv_src(const env_t src, char *varname)        {
102      int i,len;      int i,len;
103    
104      /* Don't use getvarindex, since this saves us one strlen (-: */      /* Don't use getvarindex, since this saves us one strlen (-: */
# Line 167  int clear_env_pattern(const char *patter Line 167  int clear_env_pattern(const char *patter
167    
168    
169  /**  /**
170   * Returns a duplicate of src. In case dst!=NULL it will be used and should be   * Creates a duplicate of src. In case *dst!=NULL it will be used and should be
171   * big enough. Otherwise space is malloced.   * big enough. Otherwise space is malloc-ed. dst is only updated upon success.
172     * Returns -1 upon error, 0 on success.
173   */   */
174  char **copy_env(char **dst,const char **src)    {  int copy_env(env_t *dst,const env_t src)        {
175      int i,j,envlen;      int i,j,envlen;
176      char **newenv;      env_t newenv=*dst;
177    
178      /* If no dst exists, malloc it. */      /* If no dst exists, malloc it. */
179      if (dst==NULL)  {      if (newenv==NULL)  {
180          envlen=strarrlen(src);          envlen=strarrlen((const char**)src);
181          if ( (newenv=(char **)malloc((envlen+1)*sizeof(char*)))==NULL )          newenv=(env_t)malloc((envlen+1)*sizeof(char*));
182              return NULL;          if (newenv==NULL)
183      } else              return -1;
184          newenv=dst;      }
185    
186      /* Copy src to newenv */      /* Copy src to newenv */
187      for (i=0; src[i]; i++)  {      for (i=0; src[i]; i++)  {
# Line 188  char **copy_env(char **dst,const char ** Line 189  char **copy_env(char **dst,const char **
189              /* Error: cleanup memory and reset */              /* Error: cleanup memory and reset */
190              for (j=0; j<i; j++)              for (j=0; j<i; j++)
191                  free(newenv[j]);                  free(newenv[j]);
192              if (dst==NULL)              if (*dst==NULL)
193                  free(newenv);                  free(newenv);
194              return NULL;              return -1;
195          }          }
196      }      }
197      /* Finalize the new environ */      /* Finalize the new environ */
198      newenv[i]=NULL;      newenv[i]=NULL;
199        if (*dst==NULL)
200            *dst=newenv;
201    
202      return newenv;      return 0;
203  }  }
204    
205  /**  /**
206   * Same as copy_env() but first unsets all environment variables starting with   * Same as copy_env() but first unsets all environment variables starting with
207   * MALLOC, case INsensitive.   * MALLOC, case INsensitive. dst is again only updated upon success.
208     * Returns -1 upon error, 0 on success.
209   */   */
210  char **safe_copy_env(char **dst,const char **src) {  int safe_copy_env(env_t *dst,const env_t src) {
211      /* Clean all MALLOC* variables */      /* Clean all MALLOC* variables */
212      if (clear_env_pattern("MALLOC"))      if (clear_env_pattern("MALLOC"))
213          return NULL;          return -1;
214    
215      return copy_env(dst,src);      return copy_env(dst,src);
216  }  }
# Line 217  char **safe_copy_env(char **dst,const ch Line 221  char **safe_copy_env(char **dst,const ch
221   * using dst==NULL, but this is more efficiently coded. All new entries are   * using dst==NULL, but this is more efficiently coded. All new entries are
222   * strdup-ed from those in src.   * strdup-ed from those in src.
223   */   */
224  char **copy_src_list(const char **src,const char **list)    {  env_t copy_src_list(const env_t src,const char *list[])    {
     /* Create target space */  
225      int i,j,src_idx,match=0,listlen=strarrlen(list);      int i,j,src_idx,match=0,listlen=strarrlen(list);
226      char **newenv;      env_t newenv;
227    
228      if ((newenv=(char **)malloc((listlen+1)*sizeof(char*)))==NULL)      /* Malloc space */
229        newenv=(env_t)malloc((listlen+1)*sizeof(char*));
230        if (newenv==NULL)
231          return NULL;          return NULL;
232        
233      /* IMPORTANT: list must be NULL terminated! */      /* IMPORTANT: list must be NULL terminated! */
234      newenv[0]=NULL;      newenv[0]=NULL;
235    
236      /* Only do when list exists...*/      /* Only do when list exists, otherwise return empty list, but not NO list!
237      if (!list)       * Note it's not an error, just an empty list. */
238        if (src==NULL || list==NULL)
239          return newenv;          return newenv;
240    
241      /* loop over list: Note we can have unmatched entries: match counts */      /* loop over list: Note we can have unmatched entries: match counts */
242      for (i=0; list[i]; i++)   {      for (i=0; list[i]; i++)   {
243          /* Look if list[i] is in src */          /* Look if list[i] is in src */
244          if ((src_idx=getvarindex(src,list[i]))>=0)      {          if ( (src_idx=getvarindex(src,list[i])) >= 0)  {
245              /* Don't! check for duplicates... */              /* Don't! check for duplicates... */
246              /* substitute existing: first try strdup */              /* substitute existing: first try strdup */
247              if ((newenv[match]=strdup(src[src_idx]))==NULL)   {              if ((newenv[match]=strdup(src[src_idx]))==NULL)   {
# Line 256  char **copy_src_list(const char **src,co Line 263  char **copy_src_list(const char **src,co
263  /**  /**
264   * Adds to dst the var=value pairs from src which have varname in list.   * Adds to dst the var=value pairs from src which have varname in list.
265   * All entries will be strdup-ed from those in src.   * All entries will be strdup-ed from those in src.
266   * If dst is NULL, it will create a new one. dst will be realloc-ed. If it fails   * If *dst is NULL, it will be malloc-ed, otherwise it will be realloc-ed.
267   * NULL is returned, when failure occurs later on, dst will be returned, being   * Upon success returns 0. On error -1 is returned, and dst stays unchanged.
  * equal to the original src but at a different memory location.  
268   * See also copy_src_list() which is more efficient when dst is NULL !   * See also copy_src_list() which is more efficient when dst is NULL !
269   */   */
270  char **add_src_list(char **dst,const char **src,const char **list)      {  int add_src_list(env_t *dst,const env_t src,const char **list)  {
271      int i,j,src_idx,dst_idx,match=0;      int i,j,src_idx,dst_idx,match=0;
272      int dstlen=strarrlen((const char**)dst),listlen=strarrlen(list);      env_t newenv=*dst;
273      char **newenv,*dummy;      int dstlen,listlen;
274        char *dummy;
275    
276        dstlen=strarrlen((const char **)*dst);
277        listlen=strarrlen(list);
278      /* Create space for combined array, might be too much if list is not fully      /* Create space for combined array, might be too much if list is not fully
279       * used or partially replaces old entries, but more efficient (single       * used or partially replaces old entries, but more efficient (single
280       * realloc() call) */       * realloc() call).
281      if (dst==NULL)  {       * Note that we check for empty list AFTER the malloc, and BEFORE the
282          newenv=(char **)malloc((listlen+1)*sizeof(char*));       * realloc */
283        if (newenv==NULL)  {
284            newenv=(env_t)malloc((listlen+1)*sizeof(char*));
285          if (newenv==NULL)          if (newenv==NULL)
286              return NULL;              return -1;
287            *dst=newenv; /* update *dst after malloc */
288          /* IMPORTANT: list must be NULL terminated! */          /* IMPORTANT: list must be NULL terminated! */
289          newenv[0]=NULL;          newenv[0]=NULL;
290            /* Only continue when list and src exist, do alloc in this case, since
291             * dst shouldn't return empty. Not an error. */
292            if (list==NULL || src==NULL)
293                return 0;
294      } else {      } else {
295          newenv=(char **)realloc(dst,(dstlen+listlen+1)*sizeof(char*));          /* Only do when list and src exist, no need to realloc, not an error. */
296            if (list==NULL || src==NULL)
297                return 0;
298            newenv=(env_t)realloc(newenv,(dstlen+listlen+1)*sizeof(char*));
299          if (newenv==NULL)          if (newenv==NULL)
300              return NULL;              return -1;
301            *dst=newenv; /* update *dst after realloc */
302      }      }
303    
     /* Only do when list exists...*/  
     if (!list)  
         return newenv;  
   
304      /* loop over list: Note we can have unmatched entries: match counts */      /* loop over list: Note we can have unmatched entries: match counts */
305      for (i=0; list[i]; i++)   {      for (i=0; list[i]; i++)   {
306          /* Look if list[i] is in src */          /* Look if list[i] is in src */
307          if ((src_idx=getvarindex(src,list[i]))>=0)      {          if ( (src_idx=getvarindex(src,list[i])) >= 0)   {
308              /* Look for list[i] in dst/newenv: we might have it already. Note              /* Look for list[i] in newenv: we might have it already. Note
309               * that this is not checked when using copy_src_list() */               * that this is not checked when using copy_src_list() */
310              if ((dst_idx=getvarindex((const char**)newenv,list[i]))>=0) {              if ( (dst_idx=getvarindex((const env_t)newenv,list[i])) >= 0) {
311                  /* substitute existing: first try strdup */                  /* substitute existing: first try strdup */
312                  if ((dummy=strdup(src[src_idx]))==NULL)   { /* error: cleanup */                  if ( (dummy=strdup(src[src_idx])) == NULL) { /* error: cleanup*/
313                      for (j=dstlen; j<dstlen+match; j++) {                      for (j=dstlen; j<dstlen+match; j++) {
314                          free(newenv[j]); newenv[j]=NULL;                          free(newenv[j]); newenv[j]=NULL;
315                      }                      }
316                      return newenv;                      return -1;
317                  }                  }
318                  /* Now substitute: free old entry in newenv and link to new                  /* Now substitute: free old entry in newenv and link to new
319                   * entry */                   * entry */
# Line 310  char **add_src_list(char **dst,const cha Line 326  char **add_src_list(char **dst,const cha
326                      for (j=dstlen; j<dstlen+match; j++) {                      for (j=dstlen; j<dstlen+match; j++) {
327                          free(newenv[j]); newenv[j]=NULL;                          free(newenv[j]); newenv[j]=NULL;
328                      }                      }
329                      return newenv;                      return -1;
330                  }                  }
331                  /* We created a new entry, increase match */                  /* We created a new entry, increase match */
332                  match++;                  match++;
# Line 319  char **add_src_list(char **dst,const cha Line 335  char **add_src_list(char **dst,const cha
335              }              }
336          }          }
337      }      }
338      return newenv;      return 0;
339  }  }
340    
341  /**  /**
342   * adds var=value pair to dst. If the variable already exists, it's entry is   * adds var=value pair to dst. If the variable already exists, it's entry is
343   * replaced. Otherwise dst is realloc-ed and a new entry is added, the resulting   * replaced. Otherwise *dst is realloc-ed and a new entry is added, the
344   * array is returned. In case of error NULL is returned and dst is unchanged.   * resulting array is returned.
345   * namevalue is strdup-ed into dst and hence can be freed by the caller (unlike   * Upon success returns 0. In case of error -1 is returned and dst remains
346   * putenv(3p) )   * unchanged. namevalue is strdup-ed into dst and hence can be freed by the
347     * caller (unlike putenv(3p) )
348   */   */
349  char **add_namevalue(char **dst,const char *namevalue)  {  int add_namevalue(env_t *dst,const char *namevalue)     {
350      int i,namelen,dstlen;      int i,namelen,dstlen;
351      char *namevaluecopy, **newenv;      char *namevaluecopy;
352        env_t newenv=*dst;
353    
354      /* Look for length of name part (no = means no name) */      /* Look for length of name part (no = means no name) */
355      if ((namelen=getvarnamelen(namevalue))<0)      if ( (namelen=getvarnamelen(namevalue)) < 0)
356          return NULL;          return -1;
357      /* Make already a copy, we need it in any case, unless realloc fails */      /* Make already a copy, we need it in any case, unless realloc fails */
358      if ((namevaluecopy=strdup(namevalue))==NULL)      if ( (namevaluecopy=strdup(namevalue)) == NULL)
359          return NULL;          return -1;
360      /* look for existing entry */      /* look for existing entry */
361      for (i=0; dst[i]; i++)  {      for (i=0; newenv[i]; i++)  {
362          if (strncmp(namevalue,dst[i],namelen)==0 && dst[i][namelen]=='=')   {          if (strncmp(namevalue,newenv[i],namelen)==0 && newenv[i][namelen]=='=')
363              free(dst[i]);          {
364              dst[i]=namevaluecopy;              free(newenv[i]);
365              return dst;              newenv[i]=namevaluecopy;
366                return 0;
367          }          }
368      }      }
369      /* No old match, make a new entry, add two, because strarrlen doesn't count      /* No old match, make a new entry, add two, because strarrlen doesn't count
370       * the NULL string at the end. First create new entry, since that's easier       * the NULL string at the end. First create new entry, since that's easier
371       * to undo. */       * to undo. */
372      dstlen=strarrlen((const char **)dst);      dstlen=strarrlen((const char**)newenv);
373      if ((newenv=(char **)realloc(dst,(dstlen+2)*sizeof(char*)))==NULL)  {      newenv=(env_t)realloc(newenv,(dstlen+2)*sizeof(char*));
374        if (newenv==NULL)   {
375          free(namevaluecopy);          free(namevaluecopy);
376          return NULL;          return -1;
377      }      }
378        *dst=newenv; /* update *dst after (re/m)alloc */
379      newenv[dstlen]=namevaluecopy;      newenv[dstlen]=namevaluecopy;
380      newenv[dstlen+1]=NULL;      newenv[dstlen+1]=NULL;
381    
382      return newenv;      return 0;
383  }  }
384    
385  /**  /**
386   * adds name=value pair to dst, as in setenv() with a 'external environment' .   * adds name=value pair to dst, as in setenv() with a 'external environment' .
387   * If the variable already exists, it's entry is replaced. Otherwise dst is   * If the variable already exists, it's entry is replaced. Otherwise *dst is
388   * realloc-ed and a new entry is added, the resulting array is returned. In case   * realloc-ed and a new entry is added.
389   * of error NULL is returned and dst is unchanged. name and value can be freed   * Upon success returns 0. In case of error -1 is returned and dst remains
390   * by the caller (unlike putenv(3p) )   * unchanged. name and value can be freed by the caller (unlike putenv(3p) )
391   */   */
392  char **setenv_dst(char **dst,const char *name, const char *value)       {  int setenv_dst(env_t *dst,const char *name, const char *value)  {
393      int i,namelen,valuelen,dstlen;      int i,namelen,valuelen,dstlen;
394      char *namevalue,**newenv;      char *namevalue;
395        env_t newenv=*dst;
396    
397      /* Check arguments */      /* Check arguments */
398      if (name==NULL)      if (name==NULL)
399          return NULL;          return -1;
400      if (value==NULL)    {      if (value==NULL)    {
401          value="";          value="";
402          valuelen=0;          valuelen=0;
# Line 383  char **setenv_dst(char **dst,const char Line 405  char **setenv_dst(char **dst,const char
405      namelen=strlen(name);      namelen=strlen(name);
406      /* Add two: = and \0 */      /* Add two: = and \0 */
407      if ((namevalue=(char*)malloc((namelen+valuelen+2)*sizeof(char)))==NULL)      if ((namevalue=(char*)malloc((namelen+valuelen+2)*sizeof(char)))==NULL)
408          return NULL;          return -1;
409      sprintf(namevalue,"%s=%s",name,value);      sprintf(namevalue,"%s=%s",name,value);
410    
411      /* look for existing entry */      /* look for existing entry */
412      for (i=0; dst[i]; i++)  {      for (i=0; newenv[i]; i++)  {
413          if (strncmp(name,dst[i],namelen)==0 && dst[i][namelen]=='=')   {          if (strncmp(name,newenv[i],namelen)==0 && newenv[i][namelen]=='=')   {
414              free(dst[i]);              free(newenv[i]);
415              dst[i]=namevalue;              newenv[i]=namevalue;
416              return dst;              return 0;
417          }          }
418      }      }
419      /* No old match, make a new entry, add two, because strarrlen doesn't count      /* No old match, make a new entry, add two, because strarrlen doesn't count
420       * the NULL string at the end. */       * the NULL string at the end. */
421      dstlen=strarrlen((const char **)dst);      dstlen=strarrlen((const char **)newenv);
422      if ((newenv=(char **)realloc(dst,(dstlen+2)*sizeof(char*)))==NULL)  {      newenv=(env_t)realloc(newenv,(dstlen+2)*sizeof(char*));
423        if (newenv==NULL)   {
424          free(namevalue);          free(namevalue);
425          return NULL;          return -1;
426      }      }
427        *dst=newenv; /* update *dst after (re/m)alloc */
428      newenv[dstlen]=namevalue;      newenv[dstlen]=namevalue;
429      newenv[dstlen+1]=NULL;      newenv[dstlen+1]=NULL;
430    
431      return newenv;      return 0;
432  }  }
433    
434  /**  /**
# Line 413  char **setenv_dst(char **dst,const char Line 437  char **setenv_dst(char **dst,const char
437   * NOTE: putenv doesn't reserve new memory so src should NOT be   * NOTE: putenv doesn't reserve new memory so src should NOT be
438   * free()d.   * free()d.
439   */   */
440  int putenv_src_list(char **src,const char **list)       {  int putenv_src_list(env_t src,const char **list)        {
441      int i,src_idx,match=0;      int i,src_idx,match=0;
442    
443      /* Only do when list exists...*/      /* Only do when list exists...*/
# Line 422  int putenv_src_list(char **src,const cha Line 446  int putenv_src_list(char **src,const cha
446    
447      /* loop over list */      /* loop over list */
448      for (i=0; list[i]; i++)   {      for (i=0; list[i]; i++)   {
449          if ((src_idx=getvarindex((const char **)src,list[i]))>=0)       {          if ((src_idx=getvarindex((const env_t)src,list[i]))>=0) {
450              /* Found match */              /* Found match */
451              if (putenv(src[src_idx]))              if (putenv(src[src_idx]))
452                  return -1;                  return -1;
# Line 438  int putenv_src_list(char **src,const cha Line 462  int putenv_src_list(char **src,const cha
462   * NOTE: putenv doesn't reserve new memory so src should NOT be   * NOTE: putenv doesn't reserve new memory so src should NOT be
463   * free()d.   * free()d.
464   */   */
465  int putenv_src_pattern(char **src,const char *pattern)  {  int putenv_src_pattern(env_t src,const char *pattern)   {
466      int i,len=strlen(pattern),match=0;      int i,len=strlen(pattern),match=0;
467    
468      /* loop over src environ */      /* loop over src environ */

Legend:
Removed from v.1451  
changed lines
  Added in v.1452

grid.support@nikhef.nl
ViewVC Help
Powered by ViewVC 1.1.28