The other utilities should do the same processing of the hostname
[u/mdw/putty] / psftp.c
diff --git a/psftp.c b/psftp.c
index ebfb8ef..6439de3 100644 (file)
--- a/psftp.c
+++ b/psftp.c
@@ -130,6 +130,30 @@ char *canonify(char *name)
     }
 }
 
+/*
+ * Return a pointer to the portion of str that comes after the last
+ * slash (or backslash or colon, if `local' is TRUE).
+ */
+static char *stripslashes(char *str, int local)
+{
+    char *p;
+
+    if (local) {
+        p = strchr(str, ':');
+        if (p) str = p+1;
+    }
+
+    p = strrchr(str, '/');
+    if (p) str = p+1;
+
+    if (local) {
+       p = strrchr(str, '\\');
+       if (p) str = p+1;
+    }
+
+    return str;
+}
+
 /* ----------------------------------------------------------------------
  * Actual sftp commands.
  */
@@ -276,6 +300,15 @@ int sftp_cmd_cd(struct sftp_command *cmd)
 }
 
 /*
+ * Print current directory. Easy as pie.
+ */
+int sftp_cmd_pwd(struct sftp_command *cmd)
+{
+    printf("Remote directory is %s\n", pwd);
+    return 0;
+}
+
+/*
  * Get a file and save it at the local end. We have two very
  * similar commands here: `get' and `reget', which differ in that
  * `reget' checks for the existence of the destination file and
@@ -298,7 +331,8 @@ int sftp_general_get(struct sftp_command *cmd, int restart)
        printf("%s: %s\n", cmd->words[1], fxp_error());
        return 0;
     }
-    outfname = (cmd->nwords == 2 ? cmd->words[1] : cmd->words[2]);
+    outfname = (cmd->nwords == 2 ?
+               stripslashes(cmd->words[1], 0) : cmd->words[2]);
 
     fh = fxp_open(fname, SSH_FXF_READ);
     if (!fh) {
@@ -397,7 +431,8 @@ int sftp_general_put(struct sftp_command *cmd, int restart)
     }
 
     fname = cmd->words[1];
-    origoutfname = (cmd->nwords == 2 ? cmd->words[1] : cmd->words[2]);
+    origoutfname = (cmd->nwords == 2 ?
+                   stripslashes(cmd->words[1], 1) : cmd->words[2]);
     outfname = canonify(origoutfname);
     if (!outfname) {
        printf("%s: %s\n", origoutfname, fxp_error());
@@ -919,6 +954,12 @@ static struct sftp_cmd_lookup {
            sftp_cmd_put
     },
     {
+       "pwd", "print your remote working directory",
+           "\n"
+           "  Print the current remote working directory for your SFTP session.\n",
+           sftp_cmd_pwd
+    },
+    {
        "quit", "bye", NULL,
            sftp_cmd_quit
     },
@@ -1354,7 +1395,7 @@ void logevent(char *string)
 {
 }
 
-void ldisc_send(char *buf, int len)
+void ldisc_send(char *buf, int len, int interactive)
 {
     /*
      * This is only here because of the calls to ldisc_send(NULL,
@@ -1669,6 +1710,32 @@ int main(int argc, char *argv[])
        cfg.port = 22;
     }
 
+    /*
+     * Trim leading whitespace off the hostname if it's there.
+     */
+    {
+       int space = strspn(cfg.host, " \t");
+       memmove(cfg.host, cfg.host+space, 1+strlen(cfg.host)-space);
+    }
+
+    /* See if host is of the form user@host */
+    if (cfg.host[0] != '\0') {
+       char *atsign = strchr(cfg.host, '@');
+       /* Make sure we're not overflowing the user field */
+       if (atsign) {
+           if (atsign - cfg.host < sizeof cfg.username) {
+               strncpy(cfg.username, cfg.host, atsign - cfg.host);
+               cfg.username[atsign - cfg.host] = '\0';
+           }
+           memmove(cfg.host, atsign + 1, 1 + strlen(atsign + 1));
+       }
+    }
+
+    /*
+     * Trim a colon suffix off the hostname if it's there.
+     */
+    cfg.host[strcspn(cfg.host, ":")] = '\0';
+
     /* Set username */
     if (user != NULL && user[0] != '\0') {
        strncpy(cfg.username, user, sizeof(cfg.username) - 1);
@@ -1695,6 +1762,15 @@ int main(int argc, char *argv[])
     /* SFTP uses SSH2 by default always */
     cfg.sshprot = 2;
 
+    /*
+     * Disable scary things which shouldn't be enabled for simple
+     * things like SCP and SFTP: agent forwarding, port forwarding,
+     * X forwarding.
+     */
+    cfg.x11_forward = 0;
+    cfg.agentfwd = 0;
+    cfg.portfwd[0] = cfg.portfwd[1] = '\0';
+
     /* Set up subsystem name. */
     strcpy(cfg.remote_cmd, "sftp");
     cfg.ssh_subsys = TRUE;