Solved

unsetenv under SUN Solaris

Posted on 2002-03-13
7
1,034 Views
Last Modified: 2013-12-26
C compiler under SUN solaris reports it can't find unsetenv function.

does anybody know how to unset an environment variable from inside a C program under SUN Solaris?
0
Comment
Question by:mliberi
  • 4
  • 2
7 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 50 total points
ID: 6861742
There is none - see e.g. http://groups.google.com/groups?hl=de&selm=5rtbgc%24556%40tooting.netapp.com

However, you could always create your own, e.g.

#include <stdlib.h>
void unsetenv(char *env_name) {
  extern char **environ;
  char **cc;
  int l;
  l=strlen(env_name);
  for (cc=environ;*cc!=NULL;cc++) {
    if (strncmp(env_name,*cc,l)==0 && ((*cc)[l]=='='||(*cc)[l]=='\0')) break;
  } for (; *cc != NULL; cc++) *cc=cc[1];
}
0
 
LVL 3

Author Comment

by:mliberi
ID: 6864194
jkr,

thank you for the chunk of C code you submitted.
It is actually very similar to the solution I self built and tested a few days ago: the main difference is that I applied free() system call to the line just unsetted thus causing a coredump.

man page of putenv() states that new environment variables are going to be allocated using malloc(), so why the free() fails?

I know it is not a great damage to leave it allocated even if unused, but I would like to know why it can't be allocated. Under AIX free() works without causing a coredump.

In a few days I'm going to test your code: if it works as expected I'll grade your reply.
0
 
LVL 86

Expert Comment

by:jkr
ID: 6864855
>>man page of putenv() states that new environment
>>variables are going to be allocated using malloc(),
>>so why the free() fails?

Well, 'getenv()' might not return the buffer that was 'malloc()'ed, but a different one...
0
Live: Real-Time Solutions, Start Here

Receive instant 1:1 support from technology experts, using our real-time conversation and whiteboard interface. Your first 5 minutes are always free.

 
LVL 5

Expert Comment

by:ecw
ID: 6868141
"putenv states that new environment variables are going to be allocated using malloc()",  it doesn't say anything about old environment variables, ie. ones inherited from the execing process.  Typically these are part of the initial stack frame.
0
 
LVL 86

Expert Comment

by:jkr
ID: 6868161
ecw is right - and, if the PEB is

"var1=test1\0var2=test2\0var3=test3\0"

and you get a pointer to "var2=test2", 'free()' wouldn't really make sense anyway...
0
 
LVL 86

Expert Comment

by:jkr
ID: 6886210
mliberi - anything new?
0
 
LVL 3

Author Comment

by:mliberi
ID: 6890044
thank you all for your comments
0

Featured Post

Gigs: Get Your Project Delivered by an Expert

Select from freelancers specializing in everything from database administration to programming, who have proven themselves as experts in their field. Hire the best, collaborate easily, pay securely and get projects done right.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Introduction: Hints for the grid button.  Nested classes, templated collections.  Squash that darned bug! Continuing from the sixth article about sudoku.   Open the project in visual studio. First we will finish with the SUD_SETVALUE messa…
Java performance on Solaris - Managing CPUs There are various resource controls in operating system which directly/indirectly influence the performance of application. one of the most important resource controls is "CPU".   In a multithreaded…
Learn how to navigate the file tree with the shell. Use pwd to print the current working directory: Use ls to list a directory's contents: Use cd to change to a new directory: Use wildcards instead of typing out long directory names: Use ../ to move…
This video will show you how to get GIT to work in Eclipse.   It will walk you through how to install the EGit plugin in eclipse and how to checkout an existing repository.

786 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question