From 232724af0d4b8077a48cdf370bdb343b299803ed Mon Sep 17 00:00:00 2001
From: Peter Eisentraut <peter_e@gmx.net>
Date: Mon, 7 May 2001 19:31:33 +0000
Subject: [PATCH] Add \cd command to psql.

---
 doc/src/sgml/ref/psql-ref.sgml | 19 ++++++++++++++-
 src/bin/psql/command.c         | 44 +++++++++++++++++++++++++++++++++-
 src/bin/psql/help.c            |  3 ++-
 src/bin/psql/tab-complete.c    |  9 +++----
 4 files changed, 68 insertions(+), 7 deletions(-)

diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml
index 06b0cbb2b17..c1f405f3f3e 100644
--- a/doc/src/sgml/ref/psql-ref.sgml
+++ b/doc/src/sgml/ref/psql-ref.sgml
@@ -1,5 +1,5 @@
 <!--
-$Header: /cvsroot/pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.49 2001/05/06 17:38:31 petere Exp $
+$Header: /cvsroot/pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.50 2001/05/07 19:31:33 petere Exp $
 Postgres documentation
 -->
 
@@ -217,6 +217,23 @@ testdb=>
         </listitem>
       </varlistentry>
 
+      <varlistentry>
+       <term><literal>\cd</literal> <optional><replaceable>directory</replaceable></optional></term>
+       <listitem>
+        <para>
+	 Change the current working directory to
+	 <replaceable>directory</replaceable>.  Without argument,
+	 change to the current user's home directory.
+        </para>
+
+	<tip>
+	 <para>
+	  To print your current working directory, use <literal>\!pwd</literal>.
+	 </para>
+	</tip>
+       </listitem>
+      </varlistentry>
+
       <varlistentry>
         <term><literal>\C</literal> [ <replaceable class="parameter">title</replaceable> ]</term>
         <listitem>
diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c
index eb4a2e406ec..fd3c9f8d92e 100644
--- a/src/bin/psql/command.c
+++ b/src/bin/psql/command.c
@@ -3,7 +3,7 @@
  *
  * Copyright 2000 by PostgreSQL Global Development Group
  *
- * $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.50 2001/05/06 21:15:51 petere Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.51 2001/05/07 19:31:33 petere Exp $
  */
 #include "postgres_fe.h"
 #include "command.h"
@@ -11,6 +11,9 @@
 #include <errno.h>
 #include <assert.h>
 #include <ctype.h>
+#ifdef HAVE_PWD_H
+#include <pwd.h>
+#endif
 #ifndef WIN32
 #include <sys/types.h>			/* for umask() */
 #include <sys/stat.h>			/* for stat() */
@@ -256,6 +259,45 @@ exec_command(const char *cmd,
 		free(opt2);
 	}
 
+	/* \cd */
+	else if (strcmp(cmd, "cd") == 0)
+	{
+		char   *opt = scan_option(&string, OT_NORMAL, NULL);
+		char   *dir;
+
+		if (opt)
+			dir = opt;
+		else
+		{
+#ifndef WIN32
+			struct passwd *pw;
+
+			pw = getpwuid(geteuid());
+			if (!pw)
+			{
+				psql_error("could not get home directory: %s\n", strerror(errno));
+				exit(EXIT_FAILURE);
+			}
+			dir = pw->pw_dir;
+#else /* WIN32 */
+			/* On Windows, 'cd' without arguments prints the current
+               directory, so if someone wants to code this here
+               instead... */
+			dir = "/";
+#endif /* WIN32 */
+		}
+
+		if (chdir(dir) == -1)
+		{
+			psql_error("\\%s: could not change directory to '%s': %s\n",
+					   cmd, dir, strerror(errno));
+			success = false;
+		}
+
+		if (opt)
+			free(opt);
+	}
+
 	/* \copy */
 	else if (strcasecmp(cmd, "copy") == 0)
 	{
diff --git a/src/bin/psql/help.c b/src/bin/psql/help.c
index 10392766512..d5f15b5ba77 100644
--- a/src/bin/psql/help.c
+++ b/src/bin/psql/help.c
@@ -3,7 +3,7 @@
  *
  * Copyright 2000 by PostgreSQL Global Development Group
  *
- * $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.37 2001/03/22 04:00:20 momjian Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.38 2001/05/07 19:31:33 petere Exp $
  */
 #include "postgres_fe.h"
 #include "help.h"
@@ -196,6 +196,7 @@ slashUsage(void)
 	fprintf(fout, " \\c[onnect] [dbname|- [user]]\n"
 			"                connect to new database (currently '%s')\n", PQdb(pset.db));
 	fprintf(fout, " \\C <title>     table title\n");
+	fprintf(fout, " \\cd [<dir>]    change the current working directory\n");
 	fprintf(fout, " \\copy ...      perform SQL COPY with data stream to the client machine\n");
 	fprintf(fout, " \\copyright     show PostgreSQL usage and distribution terms\n");
 	fprintf(fout, " \\d <table>     describe table (or view, index, sequence)\n");
diff --git a/src/bin/psql/tab-complete.c b/src/bin/psql/tab-complete.c
index d80465af592..ce2a6692b77 100644
--- a/src/bin/psql/tab-complete.c
+++ b/src/bin/psql/tab-complete.c
@@ -3,7 +3,7 @@
  *
  * Copyright 2000 by PostgreSQL Global Development Group
  *
- * $Header: /cvsroot/pgsql/src/bin/psql/tab-complete.c,v 1.30 2001/04/14 22:55:02 petere Exp $
+ * $Header: /cvsroot/pgsql/src/bin/psql/tab-complete.c,v 1.31 2001/05/07 19:31:33 petere Exp $
  */
 
 /*----------------------------------------------------------------------
@@ -725,12 +725,13 @@ psql_completion(char *text, int start, int end)
 
 		COMPLETE_WITH_LIST(my_list);
 	}
-	else if (strcmp(prev_wd, "\\e") == 0 || strcmp(prev_wd, "\\edit") == 0 ||
+	else if (strcmp(prev_wd, "\\cd") == 0 ||
+			 strcmp(prev_wd, "\\e") == 0 || strcmp(prev_wd, "\\edit") == 0 ||
 			 strcmp(prev_wd, "\\g") == 0 ||
 			 strcmp(prev_wd, "\\i") == 0 || strcmp(prev_wd, "\\include") == 0 ||
-		  strcmp(prev_wd, "\\o") == 0 || strcmp(prev_wd, "\\out") == 0 ||
+			 strcmp(prev_wd, "\\o") == 0 || strcmp(prev_wd, "\\out") == 0 ||
 			 strcmp(prev_wd, "\\s") == 0 ||
-		   strcmp(prev_wd, "\\w") == 0 || strcmp(prev_wd, "\\write") == 0
+			 strcmp(prev_wd, "\\w") == 0 || strcmp(prev_wd, "\\write") == 0
 		)
 		matches = completion_matches(text, filename_completion_function);
 
-- 
GitLab