Solved

unsetenv under SUN Solaris

Posted on 2002-03-13
7
1,045 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
Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
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

Free Tool: Subnet Calculator

The subnet calculator helps you design networks by taking an IP address and network mask and returning information such as network, broadcast address, and host range.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

I promised to write further about my project, and here I am.  First, I needed to setup the Primary Server.  You can read how in this article: Setup FreeBSD Server with full HDD encryption (http://www.experts-exchange.com/OS/Unix/BSD/FreeBSD/A_3660-S…
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.

856 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