Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

unsetenv under SUN Solaris

Posted on 2002-03-13
7
Medium Priority
?
1,077 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 4
  • 2
7 Comments
 
LVL 86

Accepted Solution

by:
jkr earned 200 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
Survive A High-Traffic Event with Percona

Your application or website rely on your database to deliver information about products and services to your customers. You can’t afford to have your database lose performance, lose availability or become unresponsive – even for just a few minutes.

 
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

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

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 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.
Suggested Courses

715 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