Solved

getenv()

Posted on 2003-10-25
7
312 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

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

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…
Here's a very brief overview of the methods PRTG Network Monitor (https://www.paessler.com/prtg) offers for monitoring bandwidth, to help you decide which methods you´d like to investigate in more detail.  The methods are covered in more detail in o…
This demo shows you how to set up the containerized NetScaler CPX with NetScaler Management and Analytics System in a non-routable Mesos/Marathon environment for use with Micro-Services applications.

708 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

16 Experts available now in Live!

Get 1:1 Help Now