Solved

getenv()

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

Master Your Team's Linux and Cloud Stack!

The average business loses $13.5M per year to ineffective training (per 1,000 employees). Keep ahead of the competition and combine in-person quality with online cost and flexibility by training with Linux Academy.

Question has a verified solution.

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

Suggested Solutions

Title # Comments Views Activity
why should double free() crash? 7 33
shell script working with --daemon 18 94
centOS 7 GUI is now showing. 20 1,382
monitor and log every file access on redhat/linux 4 123
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 video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

810 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