Solved

getenv()

Posted on 2003-10-25
7
321 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
[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
  • 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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Suggested Solutions

Have you ever been frustrated by having to click seven times in order to retrieve a small bit of information from the web, always the same seven clicks, scrolling down and down until you reach your target? When you know the benefits of the command l…
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…
Although Jacob Bernoulli (1654-1705) has been credited as the creator of "Binomial Distribution Table", Gottfried Leibniz (1646-1716) did his dissertation on the subject in 1666; Leibniz you may recall is the co-inventor of "Calculus" and beat Isaac…
In an interesting question (https://www.experts-exchange.com/questions/29008360/) here at Experts Exchange, a member asked how to split a single image into multiple images. The primary usage for this is to place many photographs on a flatbed scanner…

740 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