> How do I know where to get the configuration file?
Well if this is meant to be used by any user, you probably want to start by looking in the user's HOME directory.
Try this for finding out where the program exists. From there, you can create a relative path to the programs config
Code:
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <unistd.h>
#include <string.h>
/*
* Searches in several places for where the program may exist.
*/
void finder ( char *progname ) {
if ( progname == NULL || *progname == '\0' ) {
/* you would only get this from people playing games with exec() */
/* or unusual free-standing implementations, in which case all bets are off */
fprintf( stderr, "No program name specified\n" );
} else
if ( progname[0] == '.' ) {
/* relative to current dir */
char buff[PATH_MAX];
if ( getcwd(buff, sizeof buff) != NULL ) {
printf( "Rel Path=%s/%s\n", buff, progname );
} else {
fprintf( stderr, "Can't getcwd\n" );
}
} else
if ( progname[0] == '/' ) {
/* absolute path */
printf( "Abs Path=%s\n", progname );
} else {
/* search PATH environment variable */
char *env = getenv( "PATH" );
if ( env != NULL ) {
char buff[PATH_MAX];
int n;
while ( *env &&
sscanf( env, "%[^:]%n", buff, &n ) == 1 ) {
FILE *fp;
buff[n] = '\0';
strcat( buff, "/" );
strcat( buff, progname );
if ( (fp=fopen(buff,"r")) != NULL ) {
fclose(fp);
printf( "Env Path=%s\n", buff );
break;
}
env += n;
if ( *env == ':' ) env++;
}
} else {
fprintf( stderr, "Can't getenv\n" );
}
}
}
int main ( int argc, char *argv[] ) {
finder(argv[0]);
return 0;
}