*** a/src/backend/utils/init/miscinit.c --- b/src/backend/utils/init/miscinit.c *************** *** 117,194 **** ChangeToDataDir(void) DataDir))); } - /* - * If the given pathname isn't already absolute, make it so, interpreting - * it relative to the current working directory. - * - * Also canonicalizes the path. The result is always a malloc'd copy. - * - * Note: interpretation of relative-path arguments during postmaster startup - * should happen before doing ChangeToDataDir(), else the user will probably - * not like the results. - */ - char * - make_absolute_path(const char *path) - { - char *new; - - /* Returning null for null input is convenient for some callers */ - if (path == NULL) - return NULL; - - if (!is_absolute_path(path)) - { - char *buf; - size_t buflen; - - buflen = MAXPGPATH; - for (;;) - { - buf = malloc(buflen); - if (!buf) - ereport(FATAL, - (errcode(ERRCODE_OUT_OF_MEMORY), - errmsg("out of memory"))); - - if (getcwd(buf, buflen)) - break; - else if (errno == ERANGE) - { - free(buf); - buflen *= 2; - continue; - } - else - { - free(buf); - elog(FATAL, "could not get current working directory: %m"); - } - } - - new = malloc(strlen(buf) + strlen(path) + 2); - if (!new) - ereport(FATAL, - (errcode(ERRCODE_OUT_OF_MEMORY), - errmsg("out of memory"))); - sprintf(new, "%s/%s", buf, path); - free(buf); - } - else - { - new = strdup(path); - if (!new) - ereport(FATAL, - (errcode(ERRCODE_OUT_OF_MEMORY), - errmsg("out of memory"))); - } - - /* Make sure punctuation is canonical, too */ - canonicalize_path(new); - - return new; - } - - /* ---------------------------------------------------------------- * User ID state * --- 117,122 ---- *** a/src/bin/pg_ctl/pg_ctl.c --- b/src/bin/pg_ctl/pg_ctl.c *************** *** 1326,1332 **** pgwin32_CommandLine(bool registration) register_servicename); if (pg_config) ! appendPQExpBuffer(cmdLine, " -D \"%s\"", pg_config); if (registration && do_wait) appendPQExpBuffer(cmdLine, " -w"); --- 1326,1345 ---- register_servicename); if (pg_config) ! { ! char *dataDir; ! ! if ((dataDir = make_absolute_path(pg_config)) == NULL) ! { ! write_stderr(_("%s: could not identify current directory\n"), ! progname); ! exit(1); ! } ! ! make_native_path(dataDir); ! appendPQExpBuffer(cmdLine, " -D \"%s\"", dataDir); ! free(dataDir); ! } if (registration && do_wait) appendPQExpBuffer(cmdLine, " -w"); *** a/src/include/miscadmin.h --- b/src/include/miscadmin.h *************** *** 296,302 **** extern void SetCurrentRoleId(Oid roleid, bool is_superuser); extern void SetDataDir(const char *dir); extern void ChangeToDataDir(void); - extern char *make_absolute_path(const char *path); /* in utils/misc/superuser.c */ extern bool superuser(void); /* current user is superuser */ --- 296,301 ---- *** a/src/include/port.h --- b/src/include/port.h *************** *** 60,65 **** extern void get_man_path(const char *my_exec_path, char *ret_path); --- 60,67 ---- extern bool get_home_path(char *ret_path); extern void get_parent_directory(char *path); + extern char *make_absolute_path(const char *path); + /* port/dirmod.c */ extern char **pgfnames(const char *path); extern void pgfnames_cleanup(char **filenames); *** a/src/port/path.c --- b/src/port/path.c *************** *** 13,19 **** *------------------------------------------------------------------------- */ ! #include "c.h" #include #include --- 13,23 ---- *------------------------------------------------------------------------- */ ! #ifndef FRONTEND ! #include "postgres.h" ! #else ! #include "postgres_fe.h" ! #endif #include #include *************** *** 757,759 **** trim_trailing_separator(char *path) --- 761,867 ---- for (p--; p > path && IS_DIR_SEP(*p); p--) *p = '\0'; } + + /* + * make_absolute_path + * + * If the given pathname isn't already absolute, make it so, interpreting + * it relative to the current working directory. + * + * Also canonicalizes the path. The result is always a malloc'd copy. + * + * Note: interpretation of relative-path arguments during postmaster startup + * should happen before doing ChangeToDataDir(), else the user will probably + * not like the results. + */ + char * + make_absolute_path(const char *path) + { + char *new; + + /* Returning null for null input is convenient for some callers */ + if (path == NULL) + return NULL; + + if (!is_absolute_path(path)) + { + char *buf; + size_t buflen; + + buflen = MAXPGPATH; + for (;;) + { + buf = malloc(buflen); + if (!buf) + { + #ifndef FRONTEND + ereport(FATAL, + (errcode(ERRCODE_OUT_OF_MEMORY), + errmsg("out of memory"))); + #else + fprintf(stderr, _("out of memory\n")); + return NULL; + #endif + } + + if (getcwd(buf, buflen)) + break; + else if (errno == ERANGE) + { + free(buf); + buflen *= 2; + continue; + } + else + { + free(buf); + #ifndef FRONTEND + elog(FATAL, "could not get current working directory: %m"); + #else + fprintf(stderr, _("could not get current working directory: %s\n"), + strerror(errno)); + return NULL; + #endif + + } + } + + new = malloc(strlen(buf) + strlen(path) + 2); + if (!new) + { + free(buf); + #ifndef FRONTEND + ereport(FATAL, + (errcode(ERRCODE_OUT_OF_MEMORY), + errmsg("out of memory"))); + #else + fprintf(stderr, _("out of memory\n")); + return NULL; + #endif + } + + sprintf(new, "%s/%s", buf, path); + } + else + { + new = strdup(path); + if (!new) + { + #ifndef FRONTEND + ereport(FATAL, + (errcode(ERRCODE_OUT_OF_MEMORY), + errmsg("out of memory"))); + #else + fprintf(stderr, _("out of memory\n")); + return NULL; + #endif + } + + } + + /* Make sure punctuation is canonical, too */ + canonicalize_path(new); + + return new; + } + *** a/src/test/regress/pg_regress.c --- b/src/test/regress/pg_regress.c *************** *** 1832,1864 **** create_role(const char *rolename, const _stringlist * granted_dbs) } } - static char * - make_absolute_path(const char *in) - { - char *result; - - if (is_absolute_path(in)) - result = strdup(in); - else - { - static char cwdbuf[MAXPGPATH]; - - if (!cwdbuf[0]) - { - if (!getcwd(cwdbuf, sizeof(cwdbuf))) - { - fprintf(stderr, _("could not get current working directory: %s\n"), strerror(errno)); - exit(2); - } - } - - result = psprintf("%s/%s", cwdbuf, in); - } - - canonicalize_path(result); - return result; - } - static void help(void) { --- 1832,1837 ----