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

I am also getting various stackdump error messages:

16 [main] a 3732 _cygtls::handle_exceptions: Exception: STATUS_ACCESS_VIOLATION
685 [main] a 3732 open_stackdumpfile: Dumping stack trace to a.exe.stackdump

I have never seen these before.

You're new and you're messing with atexit? Why?

[edit]The argument passed to atexit is:

int atexit(void (*function)(void));

[edit=2]And that oneargument/twoargument function stuff -- are you going out of your way to over-complicate things?

Assignment for Operating Systems class. The prof gave this assignment so that we could get used to using environment variables and exit handlers and this was one of the requirements for the assignment. I would usually ask fellow students or the prof but I can't get a hold of any of them for some reason.

You need to back up and learn how to take user input, among other things.

char *environmentVariable;
	char *commandPtr;
	
	printf("Enter the name of an environment variable: ");
	scanf("%s", environmentVariable);

[edit]And how to compare strings.[/edit]

Take a look at that man page I linked and get some idea of how an exit handler is used.

Thank you Dave for you help. I was able to figure everything out and the program is working as needed.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.