src/backend/utils/misc/guc.c
bool
SelectConfigFiles(const char *userDoption, const char *progname)
{
	char	   *configdir = NULL;
	char	   *fname;
	struct stat stat_buf;

	/* configdir is -D option, or $PGDATA if no -D */
	if (userDoption)
		configdir = make_absolute_path(userDoption);
	else
		configdir = make_absolute_path(getenv("PGDATA"));

	/*
	 * Find the configuration file: if config_file was specified on the
	 * command line, use it, else use configdir/postgresql.conf.  In any case
	 * ensure the result is an absolute path, so that it will be interpreted
	 * the same way by future backends.
	 */
	if (ConfigFileName)
		fname = make_absolute_path(ConfigFileName);
	else if (configdir)
	{
		fname = guc_malloc(FATAL,
						   strlen(configdir) + strlen(CONFIG_FILENAME) + 2);
		sprintf(fname, "%s/%s", configdir, CONFIG_FILENAME);
	}
	else
	{
		write_stderr("%s does not know where to find the server configuration file.\n"
					 "You must specify the --config-file or -D invocation "
					 "option or set the PGDATA environment variable.\n",
					 progname);
		return false;
	}

	/*
	 * Set the ConfigFileName GUC variable to its final value, ensuring that
	 * it can't be overridden later.
	 */
	SetConfigOption("config_file", fname, PGC_POSTMASTER, PGC_S_OVERRIDE);
	free(fname);

	/*
	 * Now read the config file for the first time.
	 */
	if (stat(ConfigFileName, &stat_buf) != 0)
	{
		write_stderr("%s cannot access the server configuration file \"%s\": %s\n",
					 progname, ConfigFileName, strerror(errno));
		return false;
	}

	ProcessConfigFile(PGC_POSTMASTER);

	/*
	 * If the data_directory GUC variable has been set, use that as DataDir;
	 * otherwise use configdir if set; else punt.
	 *
	 * Note: SetDataDir will copy and absolute-ize its argument, so we don't
	 * have to.
	 */
	if (data_directory)
		SetDataDir(data_directory);
	else if (configdir)
		SetDataDir(configdir);
	else
	{
		write_stderr("%s does not know where to find the database system data.\n"
					 "This can be specified as \"data_directory\" in \"%s\", "
					 "or by the -D invocation option, or by the "
					 "PGDATA environment variable.\n",
					 progname, ConfigFileName);
		return false;
	}

	/*
	 * Reflect the final DataDir value back into the data_directory GUC var.
	 * (If you are wondering why we don't just make them a single variable,
	 * it's because the EXEC_BACKEND case needs DataDir to be transmitted to
	 * child backends specially.  XXX is that still true?  Given that we now
	 * chdir to DataDir, EXEC_BACKEND can read the config file without knowing
	 * DataDir in advance.)
	 */
	SetConfigOption("data_directory", DataDir, PGC_POSTMASTER, PGC_S_OVERRIDE);

	/*
	 * Figure out where pg_hba.conf is, and make sure the path is absolute.
	 */
	if (HbaFileName)
		fname = make_absolute_path(HbaFileName);
	else if (configdir)
	{
		fname = guc_malloc(FATAL,
						   strlen(configdir) + strlen(HBA_FILENAME) + 2);
		sprintf(fname, "%s/%s", configdir, HBA_FILENAME);
	}
	else
	{
		write_stderr("%s does not know where to find the \"hba\" configuration file.\n"
					 "This can be specified as \"hba_file\" in \"%s\", "
					 "or by the -D invocation option, or by the "
					 "PGDATA environment variable.\n",
					 progname, ConfigFileName);
		return false;
	}
	SetConfigOption("hba_file", fname, PGC_POSTMASTER, PGC_S_OVERRIDE);
	free(fname);

	/*
	 * Likewise for pg_ident.conf.
	 */
	if (IdentFileName)
		fname = make_absolute_path(IdentFileName);
	else if (configdir)
	{
		fname = guc_malloc(FATAL,
						   strlen(configdir) + strlen(IDENT_FILENAME) + 2);
		sprintf(fname, "%s/%s", configdir, IDENT_FILENAME);
	}
	else
	{
		write_stderr("%s does not know where to find the \"ident\" configuration file.\n"
					 "This can be specified as \"ident_file\" in \"%s\", "
					 "or by the -D invocation option, or by the "
					 "PGDATA environment variable.\n",
					 progname, ConfigFileName);
		return false;
	}
	SetConfigOption("ident_file", fname, PGC_POSTMASTER, PGC_S_OVERRIDE);
	free(fname);

	free(configdir);

	return true;
}
/*
 * Select the configuration files and data directory to be used, and
 * do the initial read of postgresql.conf.
 *
 * This is called after processing command-line switches.
 *		userDoption is the -D switch value if any (NULL if unspecified).
 *		progname is just for use in error messages.
 *
 * Returns true on success; on failure, prints a suitable error message
 * to stderr and returns false.
 */
Select the configuration files and data directory to be used, and
do the initial read of postgresql.conf.

This is called after processing command-line switches.
userDoption is the -D switch value if any (NULL if unspecified).
progname is just for use in error messages.

Returns true on success; on failure, prints a suitable error message
to stderr and returns false.
Select the configuration files and data directory to be used and do the initial read of postgresql . conf . This is called after processing command - line switches . userDoption is the - D switch value if any ( NULL if unspecified ) . progname is just for use in error messages . Returns true on success ; on failure prints a suitable error message to stderr and returns false .
bool
SelectConfigFiles(const char *userDoption, const char *progname)
{
	char	   *configdir = NULL;
	char	   *fname;
	struct stat stat_buf;
	if (userDoption)
		configdir = make_absolute_path(userDoption);
	else
		configdir = make_absolute_path(getenv("PGDATA"));
	if (ConfigFileName)
		fname = make_absolute_path(ConfigFileName);
	else if (configdir)
	{
		fname = guc_malloc(FATAL,
						   strlen(configdir) + strlen(CONFIG_FILENAME) + 2);
		sprintf(fname, "%s/%s", configdir, CONFIG_FILENAME);
	}
	else
	{
		write_stderr("%s does not know where to find the server configuration file.\n"
					 "You must specify the --config-file or -D invocation "
					 "option or set the PGDATA environment variable.\n",
					 progname);
		return false;
	}
	SetConfigOption("config_file", fname, PGC_POSTMASTER, PGC_S_OVERRIDE);
	free(fname);
	if (stat(ConfigFileName, &stat_buf) != 0)
	{
		write_stderr("%s cannot access the server configuration file \"%s\": %s\n",
					 progname, ConfigFileName, strerror(errno));
		return false;
	}
	ProcessConfigFile(PGC_POSTMASTER);
	if (data_directory)
		SetDataDir(data_directory);
	else if (configdir)
		SetDataDir(configdir);
	else
	{
		write_stderr("%s does not know where to find the database system data.\n"
					 "This can be specified as \"data_directory\" in \"%s\", "
					 "or by the -D invocation option, or by the "
					 "PGDATA environment variable.\n",
					 progname, ConfigFileName);
		return false;
	}
	SetConfigOption("data_directory", DataDir, PGC_POSTMASTER, PGC_S_OVERRIDE);
	if (HbaFileName)
		fname = make_absolute_path(HbaFileName);
	else if (configdir)
	{
		fname = guc_malloc(FATAL,
						   strlen(configdir) + strlen(HBA_FILENAME) + 2);
		sprintf(fname, "%s/%s", configdir, HBA_FILENAME);
	}
	else
	{
		write_stderr("%s does not know where to find the \"hba\" configuration file.\n"
					 "This can be specified as \"hba_file\" in \"%s\", "
					 "or by the -D invocation option, or by the "
					 "PGDATA environment variable.\n",
					 progname, ConfigFileName);
		return false;
	}
	SetConfigOption("hba_file", fname, PGC_POSTMASTER, PGC_S_OVERRIDE);
	free(fname);
	if (IdentFileName)
		fname = make_absolute_path(IdentFileName);
	else if (configdir)
	{
		fname = guc_malloc(FATAL,
						   strlen(configdir) + strlen(IDENT_FILENAME) + 2);
		sprintf(fname, "%s/%s", configdir, IDENT_FILENAME);
	}
	else
	{
		write_stderr("%s does not know where to find the \"ident\" configuration file.\n"
					 "This can be specified as \"ident_file\" in \"%s\", "
					 "or by the -D invocation option, or by the "
					 "PGDATA environment variable.\n",
					 progname, ConfigFileName);
		return false;
	}
	SetConfigOption("ident_file", fname, PGC_POSTMASTER, PGC_S_OVERRIDE);
	free(fname);
	free(configdir);
	return true;
}
bool SelectConfigFiles ( const char * userDoption , const char * progname ) { char * configdir = NULL ; char * fname ; struct stat stat_buf ; if ( userDoption ) configdir = make_absolute_path ( userDoption ) ; else configdir = make_absolute_path ( getenv ( " " ) ) ; if ( ConfigFileName ) fname = make_absolute_path ( ConfigFileName ) ; else if ( configdir ) { fname = guc_malloc ( FATAL , strlen ( configdir ) + strlen ( CONFIG_FILENAME ) + 2 ) ; sprintf ( fname , " " , configdir , CONFIG_FILENAME ) ; } else { write_stderr ( " \n " " " " \n " , progname ) ; return false ; } SetConfigOption ( " " , fname , PGC_POSTMASTER , PGC_S_OVERRIDE ) ; free ( fname ) ; if ( stat ( ConfigFileName , & stat_buf ) != 0 ) { write_stderr ( " \" \" \n " , progname , ConfigFileName , strerror ( errno ) ) ; return false ; } ProcessConfigFile ( PGC_POSTMASTER ) ; if ( data_directory ) SetDataDir ( data_directory ) ; else if ( configdir ) SetDataDir ( configdir ) ; else { write_stderr ( " \n " " \" \" \" \" " " " " \n " , progname , ConfigFileName ) ; return false ; } SetConfigOption ( " " , DataDir , PGC_POSTMASTER , PGC_S_OVERRIDE ) ; if ( HbaFileName ) fname = make_absolute_path ( HbaFileName ) ; else if ( configdir ) { fname = guc_malloc ( FATAL , strlen ( configdir ) + strlen ( HBA_FILENAME ) + 2 ) ; sprintf ( fname , " " , configdir , HBA_FILENAME ) ; } else { write_stderr ( " \" \" \n " " \" \" \" \" " " " " \n " , progname , ConfigFileName ) ; return false ; } SetConfigOption ( " " , fname , PGC_POSTMASTER , PGC_S_OVERRIDE ) ; free ( fname ) ; if ( IdentFileName ) fname = make_absolute_path ( IdentFileName ) ; else if ( configdir ) { fname = guc_malloc ( FATAL , strlen ( configdir ) + strlen ( IDENT_FILENAME ) + 2 ) ; sprintf ( fname , " " , configdir , IDENT_FILENAME ) ; } else { write_stderr ( " \" \" \n " " \" \" \" \" " " " " \n " , progname , ConfigFileName ) ; return false ; } SetConfigOption ( " " , fname , PGC_POSTMASTER , PGC_S_OVERRIDE ) ; free ( fname ) ; free ( configdir ) ; return true ; }
Select the configuration files and data directory to be used, and
 do the initial read of postgresql.conf.
Select the configuration files and data directory to be used and do the initial read of postgresql . conf .
/* configdir is -D option, or $PGDATA if no -D */ /*
	 * Find the configuration file: if config_file was specified on the
	 * command line, use it, else use configdir/postgresql.conf.  In any case
	 * ensure the result is an absolute path, so that it will be interpreted
	 * the same way by future backends.
	 */ /*
	 * Set the ConfigFileName GUC variable to its final value, ensuring that
	 * it can't be overridden later.
	 */ /*
	 * Now read the config file for the first time.
	 */ /*
	 * If the data_directory GUC variable has been set, use that as DataDir;
	 * otherwise use configdir if set; else punt.
	 *
	 * Note: SetDataDir will copy and absolute-ize its argument, so we don't
	 * have to.
	 */ /*
	 * Reflect the final DataDir value back into the data_directory GUC var.
	 * (If you are wondering why we don't just make them a single variable,
	 * it's because the EXEC_BACKEND case needs DataDir to be transmitted to
	 * child backends specially.  XXX is that still true?  Given that we now
	 * chdir to DataDir, EXEC_BACKEND can read the config file without knowing
	 * DataDir in advance.)
	 */ /*
	 * Figure out where pg_hba.conf is, and make sure the path is absolute.
	 */ /*
	 * Likewise for pg_ident.conf.
	 */