From: Peter Eisentraut Date: Mon, 7 May 2001 19:31:33 +0000 (+0000) Subject: Add \cd command to psql. X-Git-Tag: REL7_2_BETA1~1394 X-Git-Url: https://granicus.if.org/sourcecode?a=commitdiff_plain;h=232724af0d4b8077a48cdf370bdb343b299803ed;p=postgresql Add \cd command to psql. --- diff --git a/doc/src/sgml/ref/psql-ref.sgml b/doc/src/sgml/ref/psql-ref.sgml index 06b0cbb2b1..c1f405f3f3 100644 --- a/doc/src/sgml/ref/psql-ref.sgml +++ b/doc/src/sgml/ref/psql-ref.sgml @@ -1,5 +1,5 @@ @@ -217,6 +217,23 @@ testdb=> + + \cd directory + + + Change the current working directory to + directory. Without argument, + change to the current user's home directory. + + + + + To print your current working directory, use \!pwd. + + + + + \C [ title ] diff --git a/src/bin/psql/command.c b/src/bin/psql/command.c index eb4a2e406e..fd3c9f8d92 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 #include #include +#ifdef HAVE_PWD_H +#include +#endif #ifndef WIN32 #include /* for umask() */ #include /* 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 1039276651..d5f15b5ba7 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 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 d80465af59..ce2a6692b7 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);