[BACK]Return to tildexpand.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / ssh

Annotation of src/usr.bin/ssh/tildexpand.c, Revision 1.1

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"
        !            15: RCSID("$Id: tildexpand.c,v 1.2 1999/05/04 11:59:24 bg Exp $");
        !            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:
        !            23: char *tilde_expand_filename(const char *filename, uid_t my_uid)
        !            24: {
        !            25:   const char *cp;
        !            26:   unsigned int userlen;
        !            27:   char *expanded;
        !            28:   struct passwd *pw;
        !            29:   char user[100];
        !            30:
        !            31:   /* Return immediately if no tilde. */
        !            32:   if (filename[0] != '~')
        !            33:     return xstrdup(filename);
        !            34:
        !            35:
        !            36:   /* Skiop 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:     {
        !            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);
        !            53:       user[userlen] = 0;
        !            54:       pw = getpwnam(user);
        !            55:     }
        !            56:
        !            57:   /* Check that we found the user. */
        !            58:   if (!pw)
        !            59:     fatal("Unknown user %100s.", user);
        !            60:
        !            61:   /* If referring to someones home directory, return it now. */
        !            62:   if (!cp)
        !            63:     { /* Only home directory specified */
        !            64:       return xstrdup(pw->pw_dir);
        !            65:     }
        !            66:
        !            67:   /* Build a path combining the specified directory and path. */
        !            68:   expanded = xmalloc(strlen(pw->pw_dir) + strlen(cp + 1) + 2);
        !            69:   sprintf(expanded, "%s/%s", pw->pw_dir, cp + 1);
        !            70:   return expanded;
        !            71: }