4343 * Portions Copyright (c) 1994, Regents of the University of California
4444 * Portions taken from FreeBSD.
4545 *
46- * $Header: /cvsroot/pgsql/src/bin/initdb/initdb.c,v 1.12 2003/11/2321:41:30 petere Exp $
46+ * $Header: /cvsroot/pgsql/src/bin/initdb/initdb.c,v 1.13 2003/11/2322:17:59 petere Exp $
4747 *
4848 *-------------------------------------------------------------------------
4949 */
5656#include <unistd.h>
5757#include <locale.h>
5858#include <signal.h>
59+ #include <errno.h>
5960
6061#include "libpq/pqsignal.h"
6162#include "mb/pg_wchar.h"
@@ -94,8 +95,6 @@ char *username = "";
9495bool pwprompt = false;
9596bool debug = false;
9697bool noclean = false;
97- bool show_help = false;
98- bool show_version = false;
9998bool show_setting = false;
10099
101100
@@ -250,7 +249,7 @@ xmalloc(size_t size)
250249result = malloc (size );
251250if (!result )
252251{
253- fputs ( _ ("malloc failure - bailing out \n" ),stderr );
252+ fprintf ( stderr , _ ("%s: out of memory \n" ),progname );
254253exit (1 );
255254}
256255return result ;
@@ -264,7 +263,7 @@ xstrdup(const char *s)
264263result = strdup (s );
265264if (!result )
266265{
267- fputs ( _ ("strdup failure - bailing out \n" ),stderr );
266+ fprintf ( stderr , _ ("%s: out of memory \n" ),progname );
268267exit (1 );
269268}
270269return result ;
@@ -395,7 +394,8 @@ readfile(char *path)
395394
396395if ((infile = fopen (path ,"r" ))== NULL )
397396{
398- fprintf (stderr ,_ ("could not read %s\n" ),path );
397+ fprintf (stderr ,_ ("%s: could not open file \"%s\" for reading: %s\n" ),
398+ progname ,path ,strerror (errno ));
399399exit_nicely ();
400400}
401401
@@ -453,7 +453,8 @@ writefile(char *path, char **lines)
453453;
454454if ((out_file = fopen (path ,PG_BINARY_W ))== NULL )
455455{
456- fprintf (stderr ,_ ("could not write %s\n" ),path );
456+ fprintf (stderr ,_ ("%s: could not open file \"%s\" for writing: %s\n" ),
457+ progname ,path ,strerror (errno ));
457458exit_nicely ();
458459}
459460for (line = lines ;* line != NULL ;line ++ )
@@ -2009,23 +2010,23 @@ usage(const char *progname)
20092010printf (_ (" [-D, --pgdata=]DATADIR location for this database cluster\n" ));
20102011printf (_ (" -E, --encoding=ENCODING set default encoding for new databases\n" ));
20112012printf (_ (" --locale=LOCALE initialize database cluster with given locale\n" ));
2012- printf (_ (" --lc-collate, --lc-ctype, --lc-messages=LOCALE\n" ));
2013- printf ( _ ( " --lc-monetary, --lc-numeric, --lc-time=LOCALE\n" ));
2014- printf ( _ ( " initialize database cluster with given locale\n" ));
2015- printf ( _ ( " in the respective category (default taken from\n" ));
2016- printf ( _ ( " environment)\n" ));
2013+ printf (_ (" --lc-collate, --lc-ctype, --lc-messages=LOCALE\n"
2014+ " --lc-monetary, --lc-numeric, --lc-time=LOCALE\n"
2015+ " initialize database cluster with given locale\n"
2016+ " in the respective category (default taken from\n"
2017+ " environment)\n" ));
20172018printf (_ (" --no-locale equivalent to --locale=C\n" ));
20182019printf (_ (" -U, --username=NAME database superuser name\n" ));
20192020printf (_ (" -W, --pwprompt prompt for a password for the new superuser\n" ));
20202021printf (_ (" -?, --help show this help, then exit\n" ));
20212022printf (_ (" -V, --version output version information, then exit\n" ));
2022- printf (_ ("\nLess commonly used options: \n" ));
2023+ printf (_ ("\nLess commonly used options:\n" ));
20232024printf (_ (" -d, --debug generate lots of debugging output\n" ));
20242025printf (_ (" -s, --show show internal settings\n" ));
20252026printf (_ (" -L DIRECTORY where to find the input files\n" ));
20262027printf (_ (" -n, --noclean do not clean up after errors\n" ));
2027- printf (_ ("\nIf the data directory is not specified, the environment variable PGDATA\n" ));
2028- printf ( _ ( "is used.\n" ));
2028+ printf (_ ("\nIf the data directory is not specified, the environment variable PGDATA\n"
2029+ "is used.\n" ));
20292030printf (_ ("\nReport bugs to <pgsql-bugs@postgresql.org>.\n" ));
20302031}
20312032
@@ -2115,20 +2116,24 @@ main(int argc, char *argv[])
21152116self_path = NULL ;
21162117}
21172118
2119+ if (argc > 1 )
2120+ {
2121+ if (strcmp (argv [1 ],"--help" )== 0 || strcmp (argv [1 ],"-?" )== 0 )
2122+ {
2123+ usage (progname );
2124+ exit (0 );
2125+ }
2126+ if (strcmp (argv [1 ],"--version" )== 0 || strcmp (argv [1 ],"-V" )== 0 )
2127+ {
2128+ puts ("initdb (PostgreSQL) " PG_VERSION );
2129+ exit (0 );
2130+ }
2131+ }
2132+
21182133/* process command-line options */
21192134
2120- while (1 )
2135+ while (( c = getopt_long ( argc , argv , "dD:E:L:nU:W" , long_options , & option_index )) != - 1 )
21212136{
2122- /*
2123- * a : as the first option char here lets us use ? as a short
2124- * option
2125- */
2126- c = getopt_long (argc ,argv ,":D:E:WU:?sVdnL:" ,
2127- long_options ,& option_index );
2128-
2129- if (c == -1 )
2130- break ;
2131-
21322137switch (c )
21332138{
21342139case 'D' :
@@ -2178,20 +2183,14 @@ main(int argc, char *argv[])
21782183case 8 :
21792184locale = "C" ;
21802185break ;
2181- case '?' :
2182- show_help = true;
2183- break ;
21842186case 's' :
21852187show_setting = true;
21862188break ;
2187- case 'V' :
2188- show_version = true;
2189- break ;
21902189default :
2191- show_help = true;
2192- printf (_ ("Unrecognized option: %c\n" ),c );
2190+ fprintf (stderr ,_ ("Try \"%s --help\" for more information.\n" ),
2191+ progname );
2192+ exit (1 );
21932193}
2194-
21952194}
21962195
21972196/* Non-option argument specifies data directory */
@@ -2202,19 +2201,11 @@ main(int argc, char *argv[])
22022201}
22032202
22042203if (optind < argc )
2205- show_help = true;
2206-
2207- if (show_version )
22082204{
2209- /* hard coded name here, in case they rename executable */
2210- printf ("initdb (PostgreSQL) %s\n" ,PG_VERSION );
2211- exit (0 );
2212- }
2213-
2214- if (show_help )
2215- {
2216- usage (progname );
2217- exit (0 );
2205+ fprintf (stderr ,_ ("%s: too many command-line arguments (first is \"%s\")\n" ),
2206+ progname ,argv [optind + 1 ]);
2207+ fprintf (stderr ,_ ("Try \"%s --help\" for more information.\n" ),
2208+ progname );
22182209}
22192210
22202211if (strlen (pg_data )== 0 )
@@ -2229,11 +2220,9 @@ main(int argc, char *argv[])
22292220{
22302221fprintf (stderr ,
22312222_ ("%s: no data directory specified\n"
2232- "You must identify the directory where the data "
2233- "for this database system\n"
2234- "will reside. Do this with either the invocation "
2235- "option -D or the\n"
2236- "environment variable PGDATA.\n" ),
2223+ "You must identify the directory where the data for this database system\n"
2224+ "will reside. Do this with either the invocation option -D or the\n"
2225+ "environment variable PGDATA.\n" ),
22372226progname );
22382227exit (1 );
22392228}