Annotation of src/usr.bin/ssh/tildexpand.c, Revision 1.3
1.1 deraadt 1: /*
2:
3: tildexpand.c
4:
5: Author: Tatu Ylonen <ylo@cs.hut.fi>
6:
7: Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
8: All rights reserved
9:
10: Created: Wed Jul 12 01:07:36 1995 ylo
11:
12: */
13:
14: #include "includes.h"
1.3 ! markus 15: RCSID("$Id: tildexpand.c,v 1.2 1999/09/30 16:48:48 deraadt Exp $");
1.1 deraadt 16:
17: #include "xmalloc.h"
18: #include "ssh.h"
19:
20: /* Expands tildes in the file name. Returns data allocated by xmalloc.
21: Warning: this calls getpw*. */
22:
1.3 ! markus 23: char *
! 24: tilde_expand_filename(const char *filename, uid_t my_uid)
1.1 deraadt 25: {
1.3 ! markus 26: const char *cp;
! 27: unsigned int userlen;
! 28: char *expanded;
! 29: struct passwd *pw;
! 30: char user[100];
! 31:
! 32: /* Return immediately if no tilde. */
! 33: if (filename[0] != '~')
! 34: return xstrdup(filename);
! 35:
! 36: /* Skip the tilde. */
! 37: filename++;
! 38:
! 39: /* Find where the username ends. */
! 40: cp = strchr(filename, '/');
! 41: if (cp)
! 42: userlen = cp - filename; /* Have something after username. */
! 43: else
! 44: userlen = strlen(filename); /* Nothign after username. */
! 45: if (userlen == 0)
! 46: pw = getpwuid(my_uid); /* Own home directory. */
! 47: else {
! 48: /* Tilde refers to someone elses home directory. */
! 49: if (userlen > sizeof(user) - 1)
! 50: fatal("User name after tilde too long.");
! 51: memcpy(user, filename, userlen);
! 52: user[userlen] = 0;
! 53: pw = getpwnam(user);
! 54: }
! 55: /* Check that we found the user. */
! 56: if (!pw)
! 57: fatal("Unknown user %100s.", user);
! 58:
! 59: /* If referring to someones home directory, return it now. */
! 60: if (!cp) { /* Only home directory specified */
! 61: return xstrdup(pw->pw_dir);
! 62: }
! 63: /* Build a path combining the specified directory and path. */
! 64: expanded = xmalloc(strlen(pw->pw_dir) + strlen(cp + 1) + 2);
! 65: sprintf(expanded, "%s/%s", pw->pw_dir, cp + 1);
! 66: return expanded;
1.1 deraadt 67: }