aboutsummaryrefslogtreecommitdiff
path: root/src/bin/psql/common.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/bin/psql/common.c')
-rw-r--r--src/bin/psql/common.c64
1 files changed, 63 insertions, 1 deletions
diff --git a/src/bin/psql/common.c b/src/bin/psql/common.c
index 9d00e4ad80f..1e113ac49aa 100644
--- a/src/bin/psql/common.c
+++ b/src/bin/psql/common.c
@@ -3,7 +3,7 @@
*
* 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 "common.h"
@@ -814,3 +814,65 @@ session_username(void)
else
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;
+}