]> andersk Git - openssh.git/blobdiff - tildexpand.c
- markus@cvs.openbsd.org 2001/05/18 14:13:29
[openssh.git] / tildexpand.c
index 32266674015a259f32e57a8274233e191b76b8a6..46bdaae87bab69415db21bd0326ee519e4cafc9b 100644 (file)
@@ -1,70 +1,72 @@
 /*
-
-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
-
-*/
+ * Author: Tatu Ylonen <ylo@cs.hut.fi>
+ * Copyright (c) 1995 Tatu Ylonen <ylo@cs.hut.fi>, Espoo, Finland
+ *                    All rights reserved
+ *
+ * As far as I am concerned, the code I have written for this software
+ * can be used freely for any purpose.  Any derived versions of this
+ * software must be clearly marked as such, and if the derived work is
+ * incompatible with the protocol description in the RFC file, it must be
+ * called by a name other than "ssh" or "Secure Shell".
+ */
 
 #include "includes.h"
-RCSID("$Id$");
+RCSID("$OpenBSD: tildexpand.c,v 1.11 2001/02/08 19:30:53 itojun Exp $");
 
 #include "xmalloc.h"
-#include "ssh.h"
-
-/* Expands tildes in the file name.  Returns data allocated by xmalloc.
-   Warning: this calls getpw*. */
+#include "log.h"
+#include "tildexpand.h"
 
-char *tilde_expand_filename(const char *filename, uid_t my_uid)
+/*
+ * 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];
+       const char *cp;
+       u_int userlen;
+       char *expanded;
+       struct passwd *pw;
+       char user[100];
+       int len;
 
-  /* Return immediately if no tilde. */
-  if (filename[0] != '~')
-    return xstrdup(filename);
+       /* Return immediately if no tilde. */
+       if (filename[0] != '~')
+               return xstrdup(filename);
 
-  /* Skip the tilde. */
-  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);
-    }
+       /* Find where the username ends. */
+       cp = strchr(filename, '/');
+       if (cp)
+               userlen = cp - filename;        /* Something after username. */
+       else
+               userlen = strlen(filename);     /* Nothing 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);
+       }
+       if (!pw)
+               fatal("Unknown user %100s.", 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;
+       /* 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. */
+       len = strlen(pw->pw_dir) + strlen(cp + 1) + 2;
+       if (len > MAXPATHLEN)
+               fatal("Home directory too long (%d > %d", len-1, MAXPATHLEN-1);
+       expanded = xmalloc(len);
+       snprintf(expanded, len, "%s/%s", pw->pw_dir, cp + 1);
+       return expanded;
 }
This page took 0.034734 seconds and 4 git commands to generate.