mirror of
				https://github.com/postgres/postgres.git
				synced 2025-10-29 22:49:41 +03:00 
			
		
		
		
	Allow psql to handle tilde user expansion for file names.
Zach Irmen
This commit is contained in:
		| @@ -3,7 +3,7 @@ | |||||||
|  * |  * | ||||||
|  * Copyright (c) 2000-2003, PostgreSQL Global Development Group |  * Copyright (c) 2000-2003, PostgreSQL Global Development Group | ||||||
|  * |  * | ||||||
|  * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.108 2003/12/01 22:21:54 momjian Exp $ |  * $PostgreSQL: pgsql/src/bin/psql/command.c,v 1.109 2004/01/09 21:12:20 momjian Exp $ | ||||||
|  */ |  */ | ||||||
| #include "postgres_fe.h" | #include "postgres_fe.h" | ||||||
| #include "command.h" | #include "command.h" | ||||||
| @@ -413,6 +413,7 @@ exec_command(const char *cmd, | |||||||
| 		else | 		else | ||||||
| 		{ | 		{ | ||||||
| 			fname = scan_option(&string, OT_NORMAL, NULL, true); | 			fname = scan_option(&string, OT_NORMAL, NULL, true); | ||||||
|  | 			expand_tilde(&fname); | ||||||
| 			status = do_edit(fname, query_buf) ? CMD_NEWEDIT : CMD_ERROR; | 			status = do_edit(fname, query_buf) ? CMD_NEWEDIT : CMD_ERROR; | ||||||
| 			free(fname); | 			free(fname); | ||||||
| 		} | 		} | ||||||
| @@ -494,7 +495,10 @@ exec_command(const char *cmd, | |||||||
| 		if (!fname) | 		if (!fname) | ||||||
| 			pset.gfname = NULL; | 			pset.gfname = NULL; | ||||||
| 		else | 		else | ||||||
|  | 		{ | ||||||
|  | 			expand_tilde(&fname); | ||||||
| 			pset.gfname = xstrdup(fname); | 			pset.gfname = xstrdup(fname); | ||||||
|  | 		} | ||||||
| 		free(fname); | 		free(fname); | ||||||
| 		status = CMD_SEND; | 		status = CMD_SEND; | ||||||
| 	} | 	} | ||||||
| @@ -531,6 +535,7 @@ exec_command(const char *cmd, | |||||||
| 		} | 		} | ||||||
| 		else | 		else | ||||||
| 		{ | 		{ | ||||||
|  | 			expand_tilde(&fname); | ||||||
| 			success = (process_file(fname) == EXIT_SUCCESS); | 			success = (process_file(fname) == EXIT_SUCCESS); | ||||||
| 			free(fname); | 			free(fname); | ||||||
| 		} | 		} | ||||||
| @@ -561,8 +566,11 @@ exec_command(const char *cmd, | |||||||
| 				success = false; | 				success = false; | ||||||
| 			} | 			} | ||||||
| 			else | 			else | ||||||
|  | 			{ | ||||||
|  | 				expand_tilde(&opt2); | ||||||
| 				success = do_lo_export(opt1, opt2); | 				success = do_lo_export(opt1, opt2); | ||||||
| 			} | 			} | ||||||
|  | 		} | ||||||
|  |  | ||||||
| 		else if (strcmp(cmd + 3, "import") == 0) | 		else if (strcmp(cmd + 3, "import") == 0) | ||||||
| 		{ | 		{ | ||||||
| @@ -572,8 +580,11 @@ exec_command(const char *cmd, | |||||||
| 				success = false; | 				success = false; | ||||||
| 			} | 			} | ||||||
| 			else | 			else | ||||||
|  | 			{ | ||||||
|  | 				expand_tilde(&opt1); | ||||||
| 				success = do_lo_import(opt1, opt2); | 				success = do_lo_import(opt1, opt2); | ||||||
| 			} | 			} | ||||||
|  | 		} | ||||||
|  |  | ||||||
| 		else if (strcmp(cmd + 3, "list") == 0) | 		else if (strcmp(cmd + 3, "list") == 0) | ||||||
| 			success = do_lo_list(); | 			success = do_lo_list(); | ||||||
| @@ -602,6 +613,7 @@ exec_command(const char *cmd, | |||||||
| 	{ | 	{ | ||||||
| 		char	   *fname = scan_option(&string, OT_FILEPIPE, NULL, true); | 		char	   *fname = scan_option(&string, OT_FILEPIPE, NULL, true); | ||||||
|  |  | ||||||
|  | 		expand_tilde(&fname); | ||||||
| 		success = setQFout(fname); | 		success = setQFout(fname); | ||||||
| 		free(fname); | 		free(fname); | ||||||
| 	} | 	} | ||||||
| @@ -653,6 +665,7 @@ exec_command(const char *cmd, | |||||||
| 	{ | 	{ | ||||||
| 		char	   *fname = scan_option(&string, OT_NORMAL, NULL, true); | 		char	   *fname = scan_option(&string, OT_NORMAL, NULL, true); | ||||||
|  |  | ||||||
|  | 		expand_tilde(&fname); | ||||||
| 		success = saveHistory(fname ? fname : "/dev/tty"); | 		success = saveHistory(fname ? fname : "/dev/tty"); | ||||||
|  |  | ||||||
| 		if (success && !quiet && fname) | 		if (success && !quiet && fname) | ||||||
| @@ -771,6 +784,7 @@ exec_command(const char *cmd, | |||||||
| 		else | 		else | ||||||
| 		{ | 		{ | ||||||
| 			fname = scan_option(&string, OT_FILEPIPE, NULL, true); | 			fname = scan_option(&string, OT_FILEPIPE, NULL, true); | ||||||
|  | 			expand_tilde(&fname); | ||||||
|  |  | ||||||
| 			if (!fname) | 			if (!fname) | ||||||
| 			{ | 			{ | ||||||
|   | |||||||
| @@ -3,7 +3,7 @@ | |||||||
|  * |  * | ||||||
|  * Copyright (c) 2000-2003, PostgreSQL Global Development Group |  * Copyright (c) 2000-2003, PostgreSQL Global Development Group | ||||||
|  * |  * | ||||||
|  * $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.78 2003/11/29 19:52:06 pgsql Exp $ |  * $PostgreSQL: pgsql/src/bin/psql/common.c,v 1.79 2004/01/09 21:12:20 momjian Exp $ | ||||||
|  */ |  */ | ||||||
| #include "postgres_fe.h" | #include "postgres_fe.h" | ||||||
| #include "common.h" | #include "common.h" | ||||||
| @@ -814,3 +814,65 @@ session_username(void) | |||||||
| 	else | 	else | ||||||
| 		return PQuser(pset.db); | 		return PQuser(pset.db); | ||||||
| } | } | ||||||
|  |  | ||||||
|  |  | ||||||
|  | /* expand_tilde | ||||||
|  |  * | ||||||
|  |  * substitute '~' with HOME or '~username' with username's home dir | ||||||
|  |  * | ||||||
|  |  */ | ||||||
|  | char * | ||||||
|  | expand_tilde(char **filename) | ||||||
|  | { | ||||||
|  | 	if (!filename || !(*filename)) | ||||||
|  | 		return NULL; | ||||||
|  |  | ||||||
|  | 	/* MSDOS uses tilde for short versions of long file names, so skip it. */ | ||||||
|  | #ifndef WIN32 | ||||||
|  |  | ||||||
|  | 	/* try tilde expansion */ | ||||||
|  | 	if (**filename == '~') | ||||||
|  | 	{ | ||||||
|  | 		char	   *fn; | ||||||
|  | 		char	   *home; | ||||||
|  | 		char		oldp, | ||||||
|  | 				   *p; | ||||||
|  | 		struct passwd *pw; | ||||||
|  |  | ||||||
|  | 		fn = *filename; | ||||||
|  | 		home = NULL; | ||||||
|  |  | ||||||
|  | 		p = fn + 1; | ||||||
|  | 		while (*p != '/' && *p != '\0') | ||||||
|  | 			p++; | ||||||
|  |  | ||||||
|  | 		oldp = *p; | ||||||
|  | 		*p = '\0'; | ||||||
|  |  | ||||||
|  | 		if (*(fn + 1) == '\0') | ||||||
|  | 			home = getenv("HOME"); | ||||||
|  | 		else if ((pw = getpwnam(fn + 1)) != NULL) | ||||||
|  | 			home = pw->pw_dir; | ||||||
|  |  | ||||||
|  | 		*p = oldp; | ||||||
|  | 		if (home) | ||||||
|  | 		{ | ||||||
|  | 			char	   *newfn; | ||||||
|  |  | ||||||
|  | 			newfn = malloc(strlen(home) + strlen(p) + 1); | ||||||
|  | 			if (!newfn) | ||||||
|  | 			{ | ||||||
|  | 				psql_error("out of memory\n"); | ||||||
|  | 				exit(EXIT_FAILURE); | ||||||
|  | 			} | ||||||
|  | 			strcpy(newfn, home); | ||||||
|  | 			strcat(newfn, p); | ||||||
|  |  | ||||||
|  | 			free(fn); | ||||||
|  | 			*filename = newfn; | ||||||
|  | 		} | ||||||
|  | 	} | ||||||
|  | #endif | ||||||
|  |  | ||||||
|  | 	return *filename; | ||||||
|  | } | ||||||
|   | |||||||
| @@ -3,7 +3,7 @@ | |||||||
|  * |  * | ||||||
|  * Copyright (c) 2000-2003, PostgreSQL Global Development Group |  * Copyright (c) 2000-2003, PostgreSQL Global Development Group | ||||||
|  * |  * | ||||||
|  * $PostgreSQL: pgsql/src/bin/psql/common.h,v 1.31 2003/12/01 22:14:40 momjian Exp $ |  * $PostgreSQL: pgsql/src/bin/psql/common.h,v 1.32 2004/01/09 21:12:20 momjian Exp $ | ||||||
|  */ |  */ | ||||||
| #ifndef COMMON_H | #ifndef COMMON_H | ||||||
| #define COMMON_H | #define COMMON_H | ||||||
| @@ -58,4 +58,6 @@ extern char parse_char(char **buf); | |||||||
| #define pclose(x) _pclose(x) | #define pclose(x) _pclose(x) | ||||||
| #endif | #endif | ||||||
|  |  | ||||||
|  | extern char *expand_tilde(char **filename); | ||||||
|  |  | ||||||
| #endif   /* COMMON_H */ | #endif   /* COMMON_H */ | ||||||
|   | |||||||
| @@ -3,7 +3,7 @@ | |||||||
|  * |  * | ||||||
|  * Copyright (c) 2000-2003, PostgreSQL Global Development Group |  * Copyright (c) 2000-2003, PostgreSQL Global Development Group | ||||||
|  * |  * | ||||||
|  * $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.35 2003/12/01 22:14:40 momjian Exp $ |  * $PostgreSQL: pgsql/src/bin/psql/copy.c,v 1.36 2004/01/09 21:12:20 momjian Exp $ | ||||||
|  */ |  */ | ||||||
| #include "postgres_fe.h" | #include "postgres_fe.h" | ||||||
| #include "copy.h" | #include "copy.h" | ||||||
| @@ -221,6 +221,7 @@ parse_slash_copy(const char *args) | |||||||
| 		result->file = NULL; | 		result->file = NULL; | ||||||
| 	else | 	else | ||||||
| 		result->file = xstrdup(token); | 		result->file = xstrdup(token); | ||||||
|  | 	expand_tilde(&result->file); | ||||||
|  |  | ||||||
| 	token = strtokx(NULL, whitespace, NULL, NULL, | 	token = strtokx(NULL, whitespace, NULL, NULL, | ||||||
| 					0, false, pset.encoding); | 					0, false, pset.encoding); | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user