I am creating a C program to work with environment variables and I am having some problems. I have three choices to input from the command line: a.exe, a.exe <environment_variable_name>, or a.exe -a.
The 'a.exe -a' should display a list containing all environment variable names and assigned values, with one name and value per line.
If only 'a.exe' is entered then it will ask for an Environment variable to check against. If 'a.exe <environment_variable_name>' is entered it will output the name and assigned value for it.
Anyway the problem I have having is that I can get the first and second choice to work but when I put in 'a.exe -a' it keeps returning (null).
Also, when running my program I keep on getting these warnings in the command prompt (substituted actual file name with filename.c):
filename.c: In function 'main':
filename.c:16: warning: passing arg 1 of 'atexit' from incompatible pointer type
filename.c:23: warning: passing arg 1 of 'atexit' from incompatible pointer type
filename.c:110:21: warning: no newline at end of file
I am new to C and trying to test out these environment variables in a program to get a feel for it and cannot get it to work. Any help would be appreciated. BTW, I have Windows 7 Pro (64-bit) and I am using cygwin.
Thank you...code is below
**EDIT: I would like to say that this is an assignment for homework for my Operation Systems class but I am stuck and cannot get in touch with anyone for help so I thought I would come here for some ideas on how to solve this. Thank you.
#include <stdio.h>
#include <stdlib.h>
// Function prototypes
int oneArgument(int argc, char *argv[]);
int twoArguments(int argc, char *argv[]);
void exitHandler(void);
extern char **environ;
//####################################################################
int main(int argc, char *argv[])
{
int status;
status = atexit(oneArgument);
if(status != 0)
{
fprintf(stderr, "Error: Failed to install exit handler\n");
exit(1);
} // End if
status = atexit(twoArguments);
if(status != 0)
{
fprintf(stderr, "Error: Failed to install exit handler\n");
exit(1);
} // End if
// Check command line entries
if (argc == 1)
{
oneArgument(argc, argv);
} // End if
else if (argc == 2)
{
twoArguments(argc, argv);
} // End else if
else
{
fprintf(stderr, "\nUsage:\n a.out\n a.out <environment_variable_name>\n a.out -a\n");
return 0;
} // End else
exitHandler();
}
//#####################################################################
int oneArgument(int argc, char *argv[])
{
char *environmentVariable;
char *commandPtr;
printf("Enter the name of an environment variable: ");
scanf("%s", environmentVariable);
commandPtr = (char *) getenv(environmentVariable);
if(commandPtr == NULL)
{
printf("%s is not an existing environment variable name.\n");
} // End if
else
{
printf("%s : %s\n", environmentVariable, commandPtr);
} // End else
return 0;
}
//#####################################################################
int twoArguments(int argc, char *argv[])
{
if(argv[1] == "-a")
{
int i;
for(i=0; environ[i] != NULL; i++)
{
printf("%s\n", environ[i]);
} // End for
} // End if
else if(argv[1] != "-a")
{
char *commandPtr;
commandPtr = (char *) getenv(argv[1]);
if(commandPtr == NULL)
{
printf("%s is not an existing environment variable name\n", commandPtr);
} // End if
else
{
printf("%s : %s\n", argv[1], commandPtr);
} // End else
} // End else
return 0;
} // End twoArguments
//#######################################################################
void exitHandler(void)
{
printf("* * * * * * * * * * * * * * * * * * * * * * * * * *\n");
printf("Environment Variable Display Program (by Erik Lidnow)\n");
} // End exitHandler