All about programming in GNU/LINUX

A simple Menu driven program to print few of the Environment Variables using GETENV call

//This code works only under GNU Linux Operating systems
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
void getenvironment(int *);
void environment();
int errorcheck(char *ptr);
int main(int argc,int **argv)
{


    getenvironment(argv[0]);
    return 0;

}
void getenvironment(int *name)
{
    int choice;
    for(;;)
    {
        printf("\n1.Print Environment Variables\n2.Exit");
        printf("\nEnter the operation: ");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:environment(name);
                   break;
            default:
             exit(0);
        }
    }
}
void environment(char *name)
{
    int choice;
    char *ptr,*getenv();
    for(;;)
    {
        printf("\n\n");
        printf("\n1.The Path of the compiler executable\n2.The Shell you are curently using\n ");
        printf("3.The User Name \n4.The current directory\n5.The name of the program\n6.MAIN MENU");
        printf("\nEnter the Environment varialbes you wish to print: ");
        scanf("%d",&choice);
        switch(choice)
        {
            case 1:ptr=getenv("SUDO_COMMAND");
                   if(errorcheck(ptr))
                    printf("\nERROR");
                   else
                    printf("\n%s",ptr);
                   break ;
            case 2:ptr=getenv("SHELL");
                   if(errorcheck(ptr))
                    printf("\nERROR");
                   else
                    printf("\n%s",ptr);
                  break ;
            case 3:ptr=getenv("SUDO_USER");
                  if(errorcheck(ptr))
                    printf("\nERROR");
                   else
                    printf("\n%s",ptr);
                    break ;
            case 4:ptr=getenv("PWD");
                   if(errorcheck(ptr))
                    printf("\nERROR");
                   else
                   printf("\n%s",ptr);
                   break;
            case 5: printf("\n\n");
                    printf("The name of the program:%s",name);
                    printf("\n\n");
                    break ;
            case 6:return ;
            default:
                    printf("\nWrong selection");
        }

    }








}
int errorcheck(char *ptr)
{
    if(ptr==NULL)
      return 1;
    else
     return 0;
}
Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s