]> granicus.if.org Git - postgresql/blobdiff - src/bin/initdb/initdb.c
Fix HAVE_OPTRESET to be HAVE_INT_OPTRESET. Typos spotted by Lorne Sunley.
[postgresql] / src / bin / initdb / initdb.c
index ab61221b1f985090326e29156e4ad6f646b05b84..a0b2f3357eb8358d5d9f25de3598dfce25ee5faa 100644 (file)
@@ -39,7 +39,7 @@
  * 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 $
  *
  *-------------------------------------------------------------------------
  */
@@ -60,7 +60,7 @@
 #include "mb/pg_wchar.h"
 #include "getopt_long.h"
 
-#ifndef HAVE_OPTRESET
+#ifndef HAVE_INT_OPTRESET
 int                    optreset;
 #endif
 
@@ -147,7 +147,7 @@ char                backend_exec[MAXPGPATH];
 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);
@@ -181,6 +181,7 @@ static void vacuum_db(void);
 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);
@@ -318,7 +319,7 @@ replace_token(char **lines, char *token, char *replacement)
  * a sort of poor man's grep -v
  *
  */
-#ifdef WIN32
+#ifndef HAVE_UNIX_SOCKETS
 static char **
 filter_lines_with_token(char **lines, char *token)
 {
@@ -406,6 +407,9 @@ readfile(char *path)
 
 /*
  * 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)
@@ -413,7 +417,7 @@ 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));
@@ -1099,16 +1103,19 @@ setup_config(void)
        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);
 
@@ -1124,10 +1131,10 @@ setup_config(void)
 
        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
@@ -1877,7 +1884,7 @@ trapsig(int signum)
  * call exit_nicely() if we got a signal, or else output "ok".
  */
 static void
-check_ok()
+check_ok(void)
 {
        if (caught_signal)
        {
@@ -1896,11 +1903,28 @@ check_ok()
        }
 }
 
+/*
+ * 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)
@@ -1969,7 +1993,7 @@ setlocales(void)
        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));
@@ -2002,10 +2026,10 @@ usage(const char *progname)
                         "                            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"));
@@ -2045,7 +2069,7 @@ main(int argc, char *argv[])
                {"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,
@@ -2169,14 +2193,14 @@ main(int argc, char *argv[])
 
        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";
        }
@@ -2199,7 +2223,7 @@ main(int argc, char *argv[])
                 * 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);
        }
 
@@ -2208,7 +2232,7 @@ main(int argc, char *argv[])
                 !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);
        }
 
@@ -2247,18 +2271,24 @@ main(int argc, char *argv[])
        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);
        }
 
@@ -2442,7 +2472,8 @@ main(int argc, char *argv[])
 
                        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
@@ -2463,7 +2494,8 @@ main(int argc, char *argv[])
 
                default:
                        /* Trouble accessing directory */
-                       perror(pg_data);
+                       fprintf(stderr, _("%s: could not access directory \"%s\": %s\n"),
+                                       progname, pg_data, strerror(errno));
                        exit_nicely();
        }