* Portions Copyright (c) 1994, Regents of the University of California
* Portions taken from FreeBSD.
*
- * $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.55 2004/10/06 09:01:18 momjian Exp $
+ * $PostgreSQL: pgsql/src/bin/initdb/initdb.c,v 1.68 2004/11/27 18:51:05 tgl Exp $
*
*-------------------------------------------------------------------------
*/
#include "mb/pg_wchar.h"
#include "getopt_long.h"
-#ifndef HAVE_OPTRESET
+#ifndef HAVE_INT_OPTRESET
int optreset;
#endif
static void *xmalloc(size_t size);
static char *xstrdup(const char *s);
static char **replace_token(char **lines, char *token, char *replacement);
-#ifdef WIN32
+#ifndef HAVE_UNIX_SOCKETS
static char **filter_lines_with_token(char **lines, char *token);
#endif
static char **readfile(char *path);
static void make_template0(void);
static void trapsig(int signum);
static void check_ok(void);
+static char *escape_quotes(const char *src);
static bool chklocale(const char *locale);
static void setlocales(void);
static void usage(const char *progname);
* a sort of poor man's grep -v
*
*/
-#ifdef WIN32
+#ifndef HAVE_UNIX_SOCKETS
static char **
filter_lines_with_token(char **lines, char *token)
{
/*
* write an array of lines to a file
+ *
+ * This is only used to write text files. Use fopen "w" not PG_BINARY_W
+ * so that the resulting configuration files are nicely editable on Windows.
*/
static void
writefile(char *path, char **lines)
FILE *out_file;
char **line;
- if ((out_file = fopen(path, PG_BINARY_W)) == NULL)
+ if ((out_file = fopen(path, "w")) == NULL)
{
fprintf(stderr, _("%s: could not open file \"%s\" for writing: %s\n"),
progname, path, strerror(errno));
snprintf(repltok, sizeof(repltok), "shared_buffers = %d", n_buffers);
conflines = replace_token(conflines, "#shared_buffers = 1000", repltok);
+ lc_messages = escape_quotes(lc_messages);
snprintf(repltok, sizeof(repltok), "lc_messages = '%s'", lc_messages);
conflines = replace_token(conflines, "#lc_messages = 'C'", repltok);
+ lc_monetary = escape_quotes(lc_monetary);
snprintf(repltok, sizeof(repltok), "lc_monetary = '%s'", lc_monetary);
conflines = replace_token(conflines, "#lc_monetary = 'C'", repltok);
+ lc_numeric = escape_quotes(lc_numeric);
snprintf(repltok, sizeof(repltok), "lc_numeric = '%s'", lc_numeric);
-
conflines = replace_token(conflines, "#lc_numeric = 'C'", repltok);
+ lc_time = escape_quotes(lc_time);
snprintf(repltok, sizeof(repltok), "lc_time = '%s'", lc_time);
conflines = replace_token(conflines, "#lc_time = 'C'", repltok);
conflines = readfile(hba_file);
-#ifdef WIN32
- conflines = filter_lines_with_token(conflines,"@remove-line-for-win32@");
+#ifndef HAVE_UNIX_SOCKETS
+ conflines = filter_lines_with_token(conflines,"@remove-line-for-nolocal@");
#else
- conflines = replace_token(conflines,"@remove-line-for-win32@","");
+ conflines = replace_token(conflines,"@remove-line-for-nolocal@","");
#endif
#ifndef HAVE_IPV6
* call exit_nicely() if we got a signal, or else output "ok".
*/
static void
-check_ok()
+check_ok(void)
{
if (caught_signal)
{
}
}
+/*
+ * Escape any single quotes or backslashes in given string
+ */
+static char *
+escape_quotes(const char *src)
+{
+ int len = strlen(src),
+ i, j;
+ char *result = xmalloc(len * 2 + 1);
+
+ for (i = 0, j = 0; i < len; i++)
+ {
+ if (src[i] == '\'' || src[i] == '\\')
+ result[j++] = '\\';
+ result[j++] = src[i];
+ }
+ result[j] = '\0';
+ return result;
+}
/*
* check if given string is a valid locale specifier
- * based on some code given to me by Peter Eisentraut
- * (but I take responsibility for it :-)
*/
static bool
chklocale(const char *locale)
if (strlen(lc_monetary) == 0 || !chklocale(lc_monetary))
lc_monetary = xstrdup(setlocale(LC_MONETARY, NULL));
if (strlen(lc_messages) == 0 || !chklocale(lc_messages))
-#ifdef LC_MESSAGES
+#if defined(LC_MESSAGES) && !defined(WIN32)
{
/* when available get the current locale setting */
lc_messages = xstrdup(setlocale(LC_MESSAGES, NULL));
" in the respective category (default taken from\n"
" environment)\n"));
printf(_(" --no-locale equivalent to --locale=C\n"));
- printf(_(" -A, --auth=method default authentication method for local connections\n"));
+ printf(_(" -A, --auth=METHOD default authentication method for local connections\n"));
printf(_(" -U, --username=NAME database superuser name\n"));
printf(_(" -W, --pwprompt prompt for a password for the new superuser\n"));
- printf(_(" --pwfile=filename read password for the new superuser from file\n"));
+ printf(_(" --pwfile=FILE read password for the new superuser from file\n"));
printf(_(" -?, --help show this help, then exit\n"));
printf(_(" -V, --version output version information, then exit\n"));
printf(_("\nLess commonly used options:\n"));
{"debug", no_argument, NULL, 'd'},
{"show", no_argument, NULL, 's'},
{"noclean", no_argument, NULL, 'n'},
- {0, 0, 0, 0}
+ {NULL, 0, NULL, 0}
};
int c,
if (pwprompt && pwfilename)
{
- fprintf(stderr, _("%s: you cannot specify both password prompt and password file\n"), progname);
+ fprintf(stderr, _("%s: password prompt and password file may not be specified together\n"), progname);
exit(1);
}
if (authmethod == NULL || !strlen(authmethod))
{
- authwarning = _("\nWARNING: enabling \"trust\" authentication for local connections.\n"
- "You can change this by editing pg_hba.conf or using the -A flag the\n"
+ authwarning = _("\nWARNING: enabling \"trust\" authentication for local connections\n"
+ "You can change this by editing pg_hba.conf or using the -A option the\n"
"next time you run initdb.\n");
authmethod = "trust";
}
* local connections and are rejected in hba.c
*/
{
- fprintf(stderr, _("%s: unknown authentication method \"%s\".\n"), progname, authmethod);
+ fprintf(stderr, _("%s: unrecognized authentication method \"%s\"\n"), progname, authmethod);
exit(1);
}
!strcmp(authmethod, "password")) &&
!(pwprompt || pwfilename))
{
- fprintf(stderr, _("%s: you need to specify a password for the superuser to enable %s authentication.\n"), progname, authmethod);
+ fprintf(stderr, _("%s: must specify a password for the superuser to enable %s authentication\n"), progname, authmethod);
exit(1);
}
if ((ret = find_other_exec(argv[0], "postgres", PG_VERSIONSTR,
backend_exec)) < 0)
{
+ char full_path[MAXPGPATH];
+
+ if (find_my_exec(argv[0], full_path) < 0)
+ StrNCpy(full_path, progname, MAXPGPATH);
+
if (ret == -1)
fprintf(stderr,
_("The program \"postgres\" is needed by %s "
- "but was not found in the same directory as \"%s\".\n"
+ "but was not found in the\n"
+ "same directory as \"%s\".\n"
"Check your installation.\n"),
- progname, progname);
+ progname, full_path);
else
fprintf(stderr,
- _("The program \"postgres\" was found by %s "
- "but was not the same version as \"%s\".\n"
+ _("The program \"postgres\" was found by \"%s\"\n"
+ "but was not the same version as %s.\n"
"Check your installation.\n"),
- progname, progname);
+ full_path, progname);
exit(1);
}
if (chmod(pg_data, 0700) != 0)
{
- perror(pg_data);
+ fprintf(stderr, _("%s: could not change permissions of directory \"%s\": %s\n"),
+ progname, pg_data, strerror(errno));
exit_nicely();
}
else
default:
/* Trouble accessing directory */
- perror(pg_data);
+ fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"),
+ progname, pg_data, strerror(errno));
exit_nicely();
}