summaryrefslogtreecommitdiffstats
path: root/tildexpand.c
diff options
context:
space:
mode:
authorDamien Miller <djm@mindrot.org>1999-10-27 13:42:43 +1000
committerDamien Miller <djm@mindrot.org>1999-10-27 13:42:43 +1000
commitd4a8b7e34dd619a4debf9a206c81db26d1402ea6 (patch)
treea47d770a2f790f40d18b0982d4e55fa7cfb1fa3b /tildexpand.c
Initial revision
Diffstat (limited to 'tildexpand.c')
-rw-r--r--tildexpand.c70
1 files changed, 70 insertions, 0 deletions
diff --git a/tildexpand.c b/tildexpand.c
new file mode 100644
index 00000000..e4b57091
--- /dev/null
+++ b/tildexpand.c
@@ -0,0 +1,70 @@
+/*
+
+tildexpand.c
+
+Author: Tatu Ylonen <ylo@cs.hut.fi>
+
+Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
+ All rights reserved
+
+Created: Wed Jul 12 01:07:36 1995 ylo
+
+*/
+
+#include "includes.h"
+RCSID("$Id: tildexpand.c,v 1.1 1999/10/27 03:42:46 damien Exp $");
+
+#include "xmalloc.h"
+#include "ssh.h"
+
+/* Expands tildes in the file name. Returns data allocated by xmalloc.
+ Warning: this calls getpw*. */
+
+char *tilde_expand_filename(const char *filename, uid_t my_uid)
+{
+ const char *cp;
+ unsigned int userlen;
+ char *expanded;
+ struct passwd *pw;
+ char user[100];
+
+ /* Return immediately if no tilde. */
+ if (filename[0] != '~')
+ return xstrdup(filename);
+
+ /* Skip the tilde. */
+ filename++;
+
+ /* Find where the username ends. */
+ cp = strchr(filename, '/');
+ if (cp)
+ userlen = cp - filename; /* Have something after username. */
+ else
+ userlen = strlen(filename); /* Nothign after username. */
+ if (userlen == 0)
+ pw = getpwuid(my_uid); /* Own home directory. */
+ else
+ {
+ /* Tilde refers to someone elses home directory. */
+ if (userlen > sizeof(user) - 1)
+ fatal("User name after tilde too long.");
+ memcpy(user, filename, userlen);
+ user[userlen] = 0;
+ pw = getpwnam(user);
+ }
+
+ /* Check that we found the user. */
+ if (!pw)
+ fatal("Unknown user %100s.", user);
+
+ /* If referring to someones home directory, return it now. */
+ if (!cp)
+ { /* Only home directory specified */
+ return xstrdup(pw->pw_dir);
+ }
+
+ /* Build a path combining the specified directory and path. */
+ expanded = xmalloc(strlen(pw->pw_dir) + strlen(cp + 1) + 2);
+ sprintf(expanded, "%s/%s", pw->pw_dir, cp + 1);
+ return expanded;
+}