>From b3bd465357f96ebf1953b3a98f25fb51bac5eb26 Mon Sep 17 00:00:00 2001
From: Abhijit Menon-Sen <ams@2ndQuadrant.com>
Date: Wed, 24 Sep 2014 16:26:00 +0530
Subject: Make pg_controldata ignore a -D before DataDir

---
 doc/src/sgml/ref/pg_controldata.sgml    |  5 +++--
 src/bin/pg_controldata/pg_controldata.c | 13 +++++++++++--
 2 files changed, 14 insertions(+), 4 deletions(-)

diff --git a/doc/src/sgml/ref/pg_controldata.sgml b/doc/src/sgml/ref/pg_controldata.sgml
index fbf40fc..b4be660 100644
--- a/doc/src/sgml/ref/pg_controldata.sgml
+++ b/doc/src/sgml/ref/pg_controldata.sgml
@@ -40,8 +40,9 @@ PostgreSQL documentation
   <para>
    This utility can only be run by the user who initialized the cluster because
    it requires read access to the data directory.
-   You can specify the data directory on the command line, or use
-   the environment variable <envar>PGDATA</>.  This utility supports the options
+   You can specify the data directory on the command line, with or without
+   <option>-D</> or use the environment variable <envar>PGDATA</>.
+   This utility supports the options
    <option>-V</> and <option>--version</>, which print the
    <application>pg_controldata</application> version and exit.  It also
    supports options <option>-?</> and <option>--help</>, which output the
diff --git a/src/bin/pg_controldata/pg_controldata.c b/src/bin/pg_controldata/pg_controldata.c
index f815024..cfa6911 100644
--- a/src/bin/pg_controldata/pg_controldata.c
+++ b/src/bin/pg_controldata/pg_controldata.c
@@ -31,14 +31,19 @@
 static void
 usage(const char *progname)
 {
+	const char *pgdata;
+
+	pgdata = getenv("PGDATA");
+	if (!pgdata)
+		pgdata = "";
+
 	printf(_("%s displays control information of a PostgreSQL database cluster.\n\n"), progname);
 	printf(_("Usage:\n"));
 	printf(_("  %s [OPTION] [DATADIR]\n"), progname);
 	printf(_("\nOptions:\n"));
+	printf(_("  -D DATADIR     data directory (default: \"%s\")\n"), pgdata);
 	printf(_("  -V, --version  output version information, then exit\n"));
 	printf(_("  -?, --help     show this help, then exit\n"));
-	printf(_("\nIf no data directory (DATADIR) is specified, "
-			 "the environment variable PGDATA\nis used.\n\n"));
 	printf(_("Report bugs to <pgsql-bugs@postgresql.org>.\n"));
 }
 
@@ -120,7 +125,11 @@ main(int argc, char *argv[])
 	}
 
 	if (argc > 1)
+	{
 		DataDir = argv[1];
+		if (strcmp(DataDir, "-D") == 0 && argc > 2)
+			DataDir = argv[2];
+	}
 	else
 		DataDir = getenv("PGDATA");
 	if (DataDir == NULL)
-- 
1.9.1

