Solved

unsetenv under SUN Solaris

Posted on 2002-03-13
7
1,009 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
What Should I Do With This Threat Intelligence?

Are you wondering if you actually need threat intelligence? The answer is yes. We explain the basics for creating useful threat intelligence.

 
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

Enabling OSINT in Activity Based Intelligence

Activity based intelligence (ABI) requires access to all available sources of data. Recorded Future allows analysts to observe structured data on the open, deep, and dark web.

Join & Write a Comment

Suggested Solutions

Introduction: Finishing the grid – keyboard support for arrow keys to manoeuvre, entering the numbers.  The PreTranslateMessage function is to be used to intercept and respond to keyboard events. Continuing from the fourth article about sudoku. …
Introduction: Database storage, where is the exe actually on the disc? Playing a game selected randomly (how to generate random numbers).  Error trapping with try..catch to help the code run even if something goes wrong. Continuing from the seve…
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.

706 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

19 Experts available now in Live!

Get 1:1 Help Now