Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content

Commit 232724a

Browse files
committed
Add \cd command to psql.
1 parent ab420e2 commit 232724a

File tree

4 files changed

+68
-7
lines changed

4 files changed

+68
-7
lines changed

doc/src/sgml/ref/psql-ref.sgml

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
<!--
2-
$Header: /cvsroot/pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.49 2001/05/06 17:38:31 petere Exp $
2+
$Header: /cvsroot/pgsql/doc/src/sgml/ref/psql-ref.sgml,v 1.50 2001/05/07 19:31:33 petere Exp $
33
Postgres documentation
44
-->
55

@@ -217,6 +217,23 @@ testdb=>
217217
</listitem>
218218
</varlistentry>
219219

220+
<varlistentry>
221+
<term><literal>\cd</literal> <optional><replaceable>directory</replaceable></optional></term>
222+
<listitem>
223+
<para>
224+
Change the current working directory to
225+
<replaceable>directory</replaceable>. Without argument,
226+
change to the current user's home directory.
227+
</para>
228+
229+
<tip>
230+
<para>
231+
To print your current working directory, use <literal>\!pwd</literal>.
232+
</para>
233+
</tip>
234+
</listitem>
235+
</varlistentry>
236+
220237
<varlistentry>
221238
<term><literal>\C</literal> [ <replaceable class="parameter">title</replaceable> ]</term>
222239
<listitem>

src/bin/psql/command.c

Lines changed: 43 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,14 +3,17 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.50 2001/05/06 21:15:51 petere Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/command.c,v 1.51 2001/05/07 19:31:33 petere Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "command.h"
1010

1111
#include <errno.h>
1212
#include <assert.h>
1313
#include <ctype.h>
14+
#ifdef HAVE_PWD_H
15+
#include <pwd.h>
16+
#endif
1417
#ifndef WIN32
1518
#include <sys/types.h> /* for umask() */
1619
#include <sys/stat.h> /* for stat() */
@@ -256,6 +259,45 @@ exec_command(const char *cmd,
256259
free(opt2);
257260
}
258261

262+
/* \cd */
263+
else if (strcmp(cmd, "cd") == 0)
264+
{
265+
char *opt = scan_option(&string, OT_NORMAL, NULL);
266+
char *dir;
267+
268+
if (opt)
269+
dir = opt;
270+
else
271+
{
272+
#ifndef WIN32
273+
struct passwd *pw;
274+
275+
pw = getpwuid(geteuid());
276+
if (!pw)
277+
{
278+
psql_error("could not get home directory: %s\n", strerror(errno));
279+
exit(EXIT_FAILURE);
280+
}
281+
dir = pw->pw_dir;
282+
#else /* WIN32 */
283+
/* On Windows, 'cd' without arguments prints the current
284+
directory, so if someone wants to code this here
285+
instead... */
286+
dir = "/";
287+
#endif /* WIN32 */
288+
}
289+
290+
if (chdir(dir) == -1)
291+
{
292+
psql_error("\\%s: could not change directory to '%s': %s\n",
293+
cmd, dir, strerror(errno));
294+
success = false;
295+
}
296+
297+
if (opt)
298+
free(opt);
299+
}
300+
259301
/* \copy */
260302
else if (strcasecmp(cmd, "copy") == 0)
261303
{

src/bin/psql/help.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.37 2001/03/22 04:00:20 momjian Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/help.c,v 1.38 2001/05/07 19:31:33 petere Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "help.h"
@@ -196,6 +196,7 @@ slashUsage(void)
196196
fprintf(fout, " \\c[onnect] [dbname|- [user]]\n"
197197
" connect to new database (currently '%s')\n", PQdb(pset.db));
198198
fprintf(fout, " \\C <title> table title\n");
199+
fprintf(fout, " \\cd [<dir>] change the current working directory\n");
199200
fprintf(fout, " \\copy ... perform SQL COPY with data stream to the client machine\n");
200201
fprintf(fout, " \\copyright show PostgreSQL usage and distribution terms\n");
201202
fprintf(fout, " \\d <table> describe table (or view, index, sequence)\n");

src/bin/psql/tab-complete.c

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright 2000 by PostgreSQL Global Development Group
55
*
6-
* $Header: /cvsroot/pgsql/src/bin/psql/tab-complete.c,v 1.30 2001/04/14 22:55:02 petere Exp $
6+
* $Header: /cvsroot/pgsql/src/bin/psql/tab-complete.c,v 1.31 2001/05/07 19:31:33 petere Exp $
77
*/
88

99
/*----------------------------------------------------------------------
@@ -725,12 +725,13 @@ psql_completion(char *text, int start, int end)
725725

726726
COMPLETE_WITH_LIST(my_list);
727727
}
728-
else if (strcmp(prev_wd, "\\e") == 0 || strcmp(prev_wd, "\\edit") == 0 ||
728+
else if (strcmp(prev_wd, "\\cd") == 0 ||
729+
strcmp(prev_wd, "\\e") == 0 || strcmp(prev_wd, "\\edit") == 0 ||
729730
strcmp(prev_wd, "\\g") == 0 ||
730731
strcmp(prev_wd, "\\i") == 0 || strcmp(prev_wd, "\\include") == 0 ||
731-
strcmp(prev_wd, "\\o") == 0 || strcmp(prev_wd, "\\out") == 0 ||
732+
strcmp(prev_wd, "\\o") == 0 || strcmp(prev_wd, "\\out") == 0 ||
732733
strcmp(prev_wd, "\\s") == 0 ||
733-
strcmp(prev_wd, "\\w") == 0 || strcmp(prev_wd, "\\write") == 0
734+
strcmp(prev_wd, "\\w") == 0 || strcmp(prev_wd, "\\write") == 0
734735
)
735736
matches = completion_matches(text, filename_completion_function);
736737

0 commit comments

Comments
 (0)