Solved

getenv()

Posted on 2003-10-25
7
315 Views
Last Modified: 2010-04-22
int main(int argc, char *argv[])
{
         FILE *fptr;
         char buffer[100];
                         
         if(strcmp(argv[1] ,"-f"))
         {
                 printf("The first argument should be -f\n");
                 exit(0);
         }                          

         fptr = fopen(argv[2],"r");

         if(fptr == NULL)
         {
                 printf("Error opening the file\n");
                 exit(0);
         }
         else
         {
                 while(fgets(buffer,100,fptr))
                 {
                        if(getenv(buffer) != NULL)
                        {
                               printf("%s = %s\n",buffer,getenv(buffer));
                        }
                        else
                        {
                               printf("Error: There is no %s\n", buffer);
                        }              
                  }
          }
}


This retrieves the values from the text file. The text file contains:

pwd
hostname
nutsvariable
shell

it will retrive the value and if the environment variable exist, it will display the contents of the environment variable, or else it would display an error msg.

Problem: My program retrieves the value from the text file, however
when evaluated, it display an error msg, even thou the environment
variable exist.

How do I solve this problem.

Your help is kindly appreciated.

Regards
Eugene
                           
0
Comment
Question by:eugene007
  • 3
7 Comments
 
LVL 5

Accepted Solution

by:
mtmike earned 300 total points
ID: 9622238
Environment variables are usually upper case so you want to convert the buffer to upper case first. Also, the fgets() function reads in a whole line, including the trailing newline.

Here's an example function that converts a string to upper case and terminates it at the first non-printable character:

void convert(char *s)
{
      int i;

      for (i = 0; isprint(s[i]); i++) {
            s[i] = toupper(s[i]);
      }
      s[i] = 0;
}

The isprint() and toupper() are defined in <ctype.h>

You can use it in your program like this

while(fgets(buffer,100,fptr))
{
      convert(buffer);
0
 

Author Comment

by:eugene007
ID: 9622261
even when I specify the contents of the text file as uppercase:

PWD
HOSTNAME
NUTSVARIABLE
SHELL

This part of code:

while(fgets(buffer,100,fptr))
{
           if(getenv(buffer) != NULL)
           {
                   printf("%s = %s\n",buffer,getenv(buffer));
           }
           else
           {
                  printf("Error: There is no %s\n", buffer);
           }              
}

still displays using printf("Error: There is no %s\n", buffer);
cause the return value of getenv(buffer) is always (null).
Will the code u have specified, help to overcome this problem.

Regards
Eugene


0
 
LVL 5

Expert Comment

by:mtmike
ID: 9622281
That's because the buffer read with fgets() also holds the trailing newline. The convert() function makes that a nul. The function I gave is very simplistic though as it doesn't strip leading and trailing spaces. So it will work (I tested it), but not if the input file contains extra spaces. You can make it far more robust.
0
 
LVL 5

Expert Comment

by:mtmike
ID: 9709772
Hi Eugene,

I believe my first comment gives you the answer to your question.

I am aware that you have asked the same question in the C Programming TA and accepted an answer similar to my comment. However, that answer was posted nearly a day after I commented.

http://www.experts-exchange.com/Programming/Programming_Languages/C/Q_20778239.html
0
 

Expert Comment

by:Fabien1212
ID: 9716174
No idea...
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

Title # Comments Views Activity
Downgrade centos 6.5 to 6.4? 7 1,000
Boot Linux vm into a menu system 2 82
LocalRepositoryNotAccessible error when running maven 1 65
Replication of Nagios surveilance data 2 91
The purpose of this article is to fix the unknown display problem in Linux Mint operating system. After installing the OS if you see Display monitor is not recognized then we can install "MESA" utilities to fix this problem or we can install additio…
The purpose of this article is to demonstrate how we can upgrade Python from version 2.7.6 to Python 2.7.10 on the Linux Mint operating system. I am using an Oracle Virtual Box where I have installed Linux Mint operating system version 17.2. Once yo…
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Migrating to Microsoft Office 365 is becoming increasingly popular for organizations both large and small. If you have made the leap to Microsoft’s cloud platform, you know that you will need to create a corporate email signature for your Office 365…

910 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

17 Experts available now in Live!

Get 1:1 Help Now