Skip to content

Commit 55a9206

Browse files
committed
Allow psql to handle tilde user expansion for file names.
Zach Irmen
1 parent 38081fd commit 55a9206

File tree

4 files changed

+83
-4
lines changed

4 files changed

+83
-4
lines changed

src/bin/psql/command.c

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.108 2003/12/01 22:21:54 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.109 2004/01/09 21:12:20 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "command.h"
@@ -413,6 +413,7 @@ exec_command(const char *cmd,
413413
else
414414
{
415415
fname = scan_option(&string, OT_NORMAL, NULL, true);
416+
expand_tilde(&fname);
416417
status = do_edit(fname, query_buf) ? CMD_NEWEDIT : CMD_ERROR;
417418
free(fname);
418419
}
@@ -494,7 +495,10 @@ exec_command(const char *cmd,
494495
if (!fname)
495496
pset.gfname = NULL;
496497
else
498+
{
499+
expand_tilde(&fname);
497500
pset.gfname = xstrdup(fname);
501+
}
498502
free(fname);
499503
status = CMD_SEND;
500504
}
@@ -531,6 +535,7 @@ exec_command(const char *cmd,
531535
}
532536
else
533537
{
538+
expand_tilde(&fname);
534539
success = (process_file(fname) == EXIT_SUCCESS);
535540
free(fname);
536541
}
@@ -561,7 +566,10 @@ exec_command(const char *cmd,
561566
success = false;
562567
}
563568
else
569+
{
570+
expand_tilde(&opt2);
564571
success = do_lo_export(opt1, opt2);
572+
}
565573
}
566574

567575
else if (strcmp(cmd + 3, "import") == 0)
@@ -572,7 +580,10 @@ exec_command(const char *cmd,
572580
success = false;
573581
}
574582
else
583+
{
584+
expand_tilde(&opt1);
575585
success = do_lo_import(opt1, opt2);
586+
}
576587
}
577588

578589
else if (strcmp(cmd + 3, "list") == 0)
@@ -602,6 +613,7 @@ exec_command(const char *cmd,
602613
{
603614
char *fname = scan_option(&string, OT_FILEPIPE, NULL, true);
604615

616+
expand_tilde(&fname);
605617
success = setQFout(fname);
606618
free(fname);
607619
}
@@ -653,6 +665,7 @@ exec_command(const char *cmd,
653665
{
654666
char *fname = scan_option(&string, OT_NORMAL, NULL, true);
655667

668+
expand_tilde(&fname);
656669
success = saveHistory(fname ? fname : "/dev/tty");
657670

658671
if (success && !quiet && fname)
@@ -771,6 +784,7 @@ exec_command(const char *cmd,
771784
else
772785
{
773786
fname = scan_option(&string, OT_FILEPIPE, NULL, true);
787+
expand_tilde(&fname);
774788

775789
if (!fname)
776790
{

src/bin/psql/common.c

Lines changed: 63 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.78 2003/11/29 19:52:06 pgsql Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.79 2004/01/09 21:12:20 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "common.h"
@@ -814,3 +814,65 @@ session_username(void)
814814
else
815815
return PQuser(pset.db);
816816
}
817+
818+
819+
/* expand_tilde
820+
*
821+
* substitute '~' with HOME or '~username' with username's home dir
822+
*
823+
*/
824+
char *
825+
expand_tilde(char **filename)
826+
{
827+
if (!filename || !(*filename))
828+
return NULL;
829+
830+
/* MSDOS uses tilde for short versions of long file names, so skip it. */
831+
#ifndef WIN32
832+
833+
/* try tilde expansion */
834+
if (**filename == '~')
835+
{
836+
char *fn;
837+
char *home;
838+
char oldp,
839+
*p;
840+
struct passwd *pw;
841+
842+
fn = *filename;
843+
home = NULL;
844+
845+
p = fn + 1;
846+
while (*p != '/' && *p != '\0')
847+
p++;
848+
849+
oldp = *p;
850+
*p = '\0';
851+
852+
if (*(fn + 1) == '\0')
853+
home = getenv("HOME");
854+
else if ((pw = getpwnam(fn + 1)) != NULL)
855+
home = pw->pw_dir;
856+
857+
*p = oldp;
858+
if (home)
859+
{
860+
char *newfn;
861+
862+
newfn = malloc(strlen(home) + strlen(p) + 1);
863+
if (!newfn)
864+
{
865+
psql_error("out of memory\n");
866+
exit(EXIT_FAILURE);
867+
}
868+
strcpy(newfn, home);
869+
strcat(newfn, p);
870+
871+
free(fn);
872+
*filename = newfn;
873+
}
874+
}
875+
#endif
876+
877+
return *filename;
878+
}

src/bin/psql/common.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/common.h,v 1.31 2003/12/01 22:14:40 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/common.h,v 1.32 2004/01/09 21:12:20 momjian Exp $
77
*/
88
#ifndef COMMON_H
99
#define COMMON_H
@@ -58,4 +58,6 @@ extern char parse_char(char **buf);
5858
#define pclose(x) _pclose(x)
5959
#endif
6060

61+
extern char *expand_tilde(char **filename);
62+
6163
#endif /* COMMON_H */

src/bin/psql/copy.c

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@
33
*
44
* Copyright (c) 2000-2003, PostgreSQL Global Development Group
55
*
6-
* $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.35 2003/12/01 22:14:40 momjian Exp $
6+
* $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.36 2004/01/09 21:12:20 momjian Exp $
77
*/
88
#include "postgres_fe.h"
99
#include "copy.h"
@@ -221,6 +221,7 @@ parse_slash_copy(const char *args)
221221
result->file = NULL;
222222
else
223223
result->file = xstrdup(token);
224+
expand_tilde(&result->file);
224225

225226
token = strtokx(NULL, whitespace, NULL, NULL,
226227
0, false, pset.encoding);

0 commit comments

Comments
 (0)