#include <stdio.h>
#include <stdlib.h>
#include <string.h>


int main(int argc, char **argv){
	FILE *fp;
	char *location;	

	size_t strlength = 0;
	strlength = strlen( getenv("HOME") ) + strlen("/.lsnet/lsnet.conf");
	location = (char *)malloc(strlength + 1);
	(void)memset( (void *)location, '\0', strlength );
	(void)strncpy(location, (const char *)getenv("HOME"), strlength );


	char test;

	test = '\n';

	printf("test%c", test); 

	if(test == '\n')
		printf("is a newline\n");


	if(location[strlen( getenv("HOME") )] == '\n'){
		printf("getenv is newline terminated!\n");
		location[strlen( getenv("HOME") )] = '\0';
	}

	(void)strncat(location, "/lsnet.conf", strlen("/lsnet.conf") );
	printf("%s\n", location);

	if( ( fp = fopen((const char *)location, "r") ) != NULL){
		if(fclose(fp) != 0){
			printf("Error: cannot close file %s , exiting!\n", location);
			exit(1);
		}
	}else{
		if( ( fp = fopen( location, "w" ) ) == NULL ) {
			printf("Error: cannot create file %s , exiting!\n", location);
			exit(1);
		}
		if(fprintf(fp, "### Restart lsnetd if changed, limit line length to 134 chars\n") < 0){
			printf("Error: cannot write to file %s , exiting!\n", location);
			exit(1);
		}
		if(fclose(fp) != 0){
			printf("Error: cannot close file %s , exiting!\n", location);
			exit(1);
		}
	}


	free(location);
	return 0;
}
