2 * Author: Tatu Ylonen <ylo@cs.hut.fi>
3 * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
6 * As far as I am concerned, the code I have written for this software
7 * can be used freely for any purpose. Any derived versions of this
8 * software must be clearly marked as such, and if the derived work is
9 * incompatible with the protocol description in the RFC file, it must be
10 * called by a name other than "ssh" or "Secure Shell".
14 RCSID("$OpenBSD: tildexpand.c,v 1.9 2000/12/19 23:17:59 markus Exp $");
20 * Expands tildes in the file name. Returns data allocated by xmalloc.
21 * Warning: this calls getpw*.
24 tilde_expand_filename(const char *filename, uid_t my_uid)
33 /* Return immediately if no tilde. */
34 if (filename[0] != '~')
35 return xstrdup(filename);
40 /* Find where the username ends. */
41 cp = strchr(filename, '/');
43 userlen = cp - filename; /* Something after username. */
45 userlen = strlen(filename); /* Nothing after username. */
47 pw = getpwuid(my_uid); /* Own home directory. */
49 /* Tilde refers to someone elses home directory. */
50 if (userlen > sizeof(user) - 1)
51 fatal("User name after tilde too long.");
52 memcpy(user, filename, userlen);
57 fatal("Unknown user %100s.", user);
59 /* If referring to someones home directory, return it now. */
61 /* Only home directory specified */
62 return xstrdup(pw->pw_dir);
64 /* Build a path combining the specified directory and path. */
65 len = strlen(pw->pw_dir) + strlen(cp + 1) + 2;
67 fatal("Home directory too long (%d > %d", len-1, MAXPATHLEN-1);
68 expanded = xmalloc(len);
69 snprintf(expanded, len, "%s/%s", pw->pw_dir, cp + 1);