Go Premium for a chance to win a PS4. Enter to Win

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 332
  • Last Modified:

getenv()

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
eugene007
Asked:
eugene007
  • 3
1 Solution
 
mtmikeCommented:
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
 
eugene007Author Commented:
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
 
mtmikeCommented:
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
 
mtmikeCommented:
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
 
Fabien1212Commented:
No idea...
0

Featured Post

Efficient way to get backups off site to Azure

This user guide provides instructions on how to deploy and configure both a StoneFly Scale Out NAS Enterprise Cloud Drive virtual machine and Veeam Cloud Connect in the Microsoft Azure Cloud.

  • 3
Tackle projects and never again get stuck behind a technical roadblock.
Join Now