Solved

unsetenv under SUN Solaris

Posted on 2002-03-13
7
1,022 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
Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

 
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

Is Your Active Directory as Secure as You Think?

More than 75% of all records are compromised because of the loss or theft of a privileged credential. Experts have been exploring Active Directory infrastructure to identify key threats and establish best practices for keeping data safe. Attend this month’s webinar to learn more.

Question has a verified solution.

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

Suggested Solutions

Introduction: Load and Save to file, Document-View interaction inside the SDI. Continuing from the second article about sudoku.   Open the project in visual studio. From the class view select CSudokuDoc and double click to open the header …
Introduction: The undo support, implementing a stack. Continuing from the eigth article about sudoku.   We need a mechanism to keep track of the digits entered so as to implement an undo mechanism.  This should be a ‘Last In First Out’ collec…
Learn several ways to interact with files and get file information from the bash shell. ls lists the contents of a directory: Using the -a flag displays hidden files: Using the -l flag formats the output in a long list: The file command gives us mor…
In a previous video, we went over how to export a DynamoDB table into Amazon S3.  In this video, we show how to load the export from S3 into a DynamoDB table.

863 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

Need Help in Real-Time?

Connect with top rated Experts

22 Experts available now in Live!

Get 1:1 Help Now