23 |
#include <unistd.h> |
#include <unistd.h> |
24 |
#include <stdlib.h> |
#include <stdlib.h> |
25 |
#include <string.h> |
#include <string.h> |
26 |
|
#include <stdio.h> |
27 |
|
|
28 |
#include "environ.h" |
#include "environ.h" |
29 |
|
|
226 |
return NULL; |
return NULL; |
227 |
/* IMPORTANT: list must be NULL terminated! */ |
/* IMPORTANT: list must be NULL terminated! */ |
228 |
newenv[0]=NULL; |
newenv[0]=NULL; |
229 |
|
|
230 |
|
/* Only do when list exists...*/ |
231 |
|
if (!list) |
232 |
|
return newenv; |
233 |
|
|
234 |
/* loop over list: Note we can have unmatched entries: match counts */ |
/* loop over list: Note we can have unmatched entries: match counts */ |
235 |
for (i=0; list[i]; i++) { |
for (i=0; list[i]; i++) { |
236 |
/* Look if list[i] is in src */ |
/* Look if list[i] is in src */ |
281 |
return NULL; |
return NULL; |
282 |
} |
} |
283 |
|
|
284 |
|
/* Only do when list exists...*/ |
285 |
|
if (!list) |
286 |
|
return newenv; |
287 |
|
|
288 |
/* loop over list: Note we can have unmatched entries: match counts */ |
/* loop over list: Note we can have unmatched entries: match counts */ |
289 |
for (i=0; list[i]; i++) { |
for (i=0; list[i]; i++) { |
290 |
/* Look if list[i] is in src */ |
/* Look if list[i] is in src */ |
331 |
*/ |
*/ |
332 |
char **add_namevalue(char **dst,const char *namevalue) { |
char **add_namevalue(char **dst,const char *namevalue) { |
333 |
int i,namelen,dstlen; |
int i,namelen,dstlen; |
334 |
char *dummy, **newenv; |
char *namevaluecopy, **newenv; |
335 |
|
|
336 |
/* Look for length of name part (no = means no name) */ |
/* Look for length of name part (no = means no name) */ |
337 |
if ((namelen=getvarnamelen(namevalue))<0) |
if ((namelen=getvarnamelen(namevalue))<0) |
338 |
return NULL; |
return NULL; |
339 |
|
/* Make already a copy, we need it in any case, unless realloc fails */ |
340 |
|
if ((namevaluecopy=strdup(namevalue))==NULL) |
341 |
|
return NULL; |
342 |
/* look for existing entry */ |
/* look for existing entry */ |
343 |
for (i=0; dst[i]; i++) { |
for (i=0; dst[i]; i++) { |
344 |
if (strncmp(namevalue,dst[i],namelen)==0 && dst[i][namelen]=='=') { |
if (strncmp(namevalue,dst[i],namelen)==0 && dst[i][namelen]=='=') { |
|
if ((dummy=strdup(namevalue))==NULL) |
|
|
return NULL; |
|
345 |
free(dst[i]); |
free(dst[i]); |
346 |
dst[i]=dummy; |
dst[i]=namevaluecopy; |
347 |
return dst; |
return dst; |
348 |
} |
} |
349 |
} |
} |
350 |
/* 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 |
351 |
* 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 |
352 |
* to undo. */ |
* to undo. */ |
353 |
if ((dummy=strdup(namevalue))==NULL) |
dstlen=strarrlen((const char **)dst); |
354 |
|
if ((newenv=(char **)realloc(dst,(dstlen+2)*sizeof(char*)))==NULL) { |
355 |
|
free(namevaluecopy); |
356 |
return NULL; |
return NULL; |
357 |
|
} |
358 |
|
newenv[dstlen]=namevaluecopy; |
359 |
|
newenv[dstlen+1]=NULL; |
360 |
|
|
361 |
|
return newenv; |
362 |
|
} |
363 |
|
|
364 |
|
/** |
365 |
|
* adds name=value pair to dst, as in setenv() with a 'external environment' . |
366 |
|
* If the variable already exists, it's entry is replaced. Otherwise dst is |
367 |
|
* realloc-ed and a new entry is added, the resulting array is returned. In case |
368 |
|
* of error NULL is returned and dst is unchanged. name and value can be freed |
369 |
|
* by the caller (unlike putenv(3p) ) |
370 |
|
*/ |
371 |
|
char **setenv_dst(char **dst,const char *name, const char *value) { |
372 |
|
int i,namelen,valuelen,dstlen; |
373 |
|
char *namevalue,**newenv; |
374 |
|
|
375 |
|
/* Check arguments */ |
376 |
|
if (name==NULL) |
377 |
|
return NULL; |
378 |
|
if (value==NULL) { |
379 |
|
value=""; |
380 |
|
valuelen=0; |
381 |
|
} else |
382 |
|
valuelen=strlen(value); |
383 |
|
namelen=strlen(name); |
384 |
|
/* Add two: = and \0 */ |
385 |
|
if ((namevalue=(char*)malloc((namelen+valuelen+2)*sizeof(char)))==NULL) |
386 |
|
return NULL; |
387 |
|
sprintf(namevalue,"%s=%s",name,value); |
388 |
|
|
389 |
|
/* look for existing entry */ |
390 |
|
for (i=0; dst[i]; i++) { |
391 |
|
if (strncmp(name,dst[i],namelen)==0 && dst[i][namelen]=='=') { |
392 |
|
free(dst[i]); |
393 |
|
dst[i]=namevalue; |
394 |
|
return dst; |
395 |
|
} |
396 |
|
} |
397 |
|
/* No old match, make a new entry, add two, because strarrlen doesn't count |
398 |
|
* the NULL string at the end. */ |
399 |
dstlen=strarrlen((const char **)dst); |
dstlen=strarrlen((const char **)dst); |
400 |
if ((newenv=(char **)realloc(dst,(dstlen+2)*sizeof(char*)))==NULL) |
if ((newenv=(char **)realloc(dst,(dstlen+2)*sizeof(char*)))==NULL) { |
401 |
|
free(namevalue); |
402 |
return NULL; |
return NULL; |
403 |
newenv[dstlen]=dummy; |
} |
404 |
|
newenv[dstlen]=namevalue; |
405 |
newenv[dstlen+1]=NULL; |
newenv[dstlen+1]=NULL; |
406 |
|
|
407 |
return newenv; |
return newenv; |
416 |
int putenv_src_list(char **src,const char **list) { |
int putenv_src_list(char **src,const char **list) { |
417 |
int i,src_idx,match=0; |
int i,src_idx,match=0; |
418 |
|
|
419 |
|
/* Only do when list exists...*/ |
420 |
|
if (!list) |
421 |
|
return 0; |
422 |
|
|
423 |
/* loop over list */ |
/* loop over list */ |
424 |
for (i=0; list[i]; i++) { |
for (i=0; list[i]; i++) { |
425 |
if ((src_idx=getvarindex((const char **)src,list[i]))>=0) { |
if ((src_idx=getvarindex((const char **)src,list[i]))>=0) { |