--- ssh-method.c.orig	Wed Sep 17 17:16:54 2003
+++ ssh-method.c	Sat Sep 20 14:38:11 2003
@@ -60,7 +60,7 @@
 	GnomeVFSFileInfoOptions info_opts;
 } SshHandle;
 
-static GnomeVFSResult ssh_read_error	(int error_fd,
+static GnomeVFSResult ssh_read		(gint fd,
 					 gpointer buffer,
 					 GnomeVFSFileSize num_bytes,
 					 GnomeVFSFileSize *bytes_read);
@@ -186,26 +186,17 @@
 
 	/* We do support ssh:/// as ssh://localhost/ */
 	hostname = gnome_vfs_uri_get_host_name (uri);
-	if (hostname == NULL) {
-		hostname = "localhost";
-	}
 
 	username = gnome_vfs_uri_get_user_name(uri);
-	if (username == NULL) {
-		username = g_get_user_name();
-	}
 
-	host_port = g_strdup_printf("%d", gnome_vfs_uri_get_host_port(uri) ?
-			gnome_vfs_uri_get_host_port(uri) : 22);
+	host_port = gnome_vfs_uri_get_host_port(uri) ?
+		g_strdup_printf("%d",gnome_vfs_uri_get_host_port(uri)) : NULL;
 
-	command_line  = g_strconcat ("ssh -oBatchMode=yes -x -l ", 
-				     username,
-				     " -p ", host_port,
+	command_line  = g_strconcat ("ssh -oBatchMode=yes -x", 
+				     (username ? " -l ", username : ""),
+				     (host_port ? " -p ", host_port : ""),
 				     " ", hostname,
-				     " ",
-				     "\"LC_ALL=C; echo READY > /dev/stderr;",
-				     command, "; echo DONE > /dev/stderr\"",
-				     NULL);
+				     " ", command, NULL);
 	g_free (host_port);
 
 	D(("ssh_connect () command: %s\n", command_line));
@@ -224,8 +215,10 @@
 	g_spawn_async_with_pipes (NULL, argv, NULL, 
 				  G_SPAWN_SEARCH_PATH,
 				  NULL, NULL,
-				  (gint *)&handle->pid, &handle->write_fd, 
-				  &handle->read_fd, &handle->error_fd, 
+				  (gint *)&handle->pid,
+				  &handle->write_fd, 
+				  &handle->read_fd,
+				  &handle->error_fd, 
 				  &gerror);
 	g_strfreev (argv);
 
@@ -241,13 +234,10 @@
 
 	/* You can add more error checking here */
 	memset (buffer, '\0', LINE_LENGTH);
-	res = ssh_read_error (handle->error_fd, &buffer,
-			LINE_LENGTH, &bytes_read);
+	res = ssh_read (handle->error_fd, &buffer, LINE_LENGTH, &bytes_read);
 
 	if (bytes_read != 0 && res == GNOME_VFS_OK) {
-		if (strncmp ("READY", buffer, strlen ("READY")) == 0) {
-			res = GNOME_VFS_OK;
-		} else if (strncmp ("Permission denied", buffer,
+		if (strncmp ("Permission denied", buffer,
 					strlen("Permission denied")) == 0) {
 			res = GNOME_VFS_ERROR_LOGIN_FAILED;
 		} else if (strncmp ("Host key verification failed", buffer,
@@ -276,63 +266,35 @@
 }
 
 static GnomeVFSResult
-ssh_read (SshHandle *handle,
-	   gpointer buffer,
-	   GnomeVFSFileSize num_bytes,
-	   GnomeVFSFileSize *bytes_read)
-{
+ssh_read (gint fd,
+	      gpointer buffer,
+	      GnomeVFSFileSize num_bytes,
+	      GnomeVFSFileSize *bytes_read)
+{
+	int retval;
+	fd_set fds;
+	struct timeval tv = {5, 0};
 	GnomeVFSFileSize my_read;
 
-	my_read = (GnomeVFSFileSize) read (handle->read_fd, buffer, 
-					   (size_t) num_bytes);
+	FD_ZERO(&fds);
+	FD_SET(fd, &fds);
+	retval = select(fd + 1, &fds, NULL, NULL, &tv);
 
-	if (my_read == -1) {
-		return gnome_vfs_result_from_errno ();
-	}
+	*bytes_read = 0;
 
-	*bytes_read = my_read;
-
-	return GNOME_VFS_OK;
-}
-
-static GnomeVFSResult
-ssh_read_error (int error_fd,
-		gpointer buffer,
-		GnomeVFSFileSize num_bytes,
-		GnomeVFSFileSize *bytes_read)
-{
-	GnomeVFSFileSize my_read;
-
-	my_read = (GnomeVFSFileSize) read (error_fd, buffer,
-			(size_t) num_bytes);
-
-	if (my_read == -1) {
+	if (retval < 0)
 		return gnome_vfs_result_from_errno ();
-	}
-
-	*bytes_read = my_read;
-
-	return GNOME_VFS_OK;
-}
-
-static GnomeVFSResult
-ssh_check_for_done (SshHandle *handle)
-{
-	char buffer[LINE_LENGTH];
-	GnomeVFSResult res;
-	GnomeVFSFileSize bytes_read;
 
-	memset (buffer, '\0', LINE_LENGTH);
-	res = ssh_read_error (handle->error_fd, &buffer,
-			LINE_LENGTH, &bytes_read);
-
-	if (bytes_read != 0 && res == GNOME_VFS_OK) {
-		if (strncmp ("DONE", buffer, strlen ("DONE")) == 0) {
-			return res;
+	if (retval) {
+		my_read = (GnomeVFSFileSize) read (fd, buffer,
+						   (size_t) num_bytes);
+		if (my_read == -1) {
+			return gnome_vfs_result_from_errno ();
 		}
+		*bytes_read = my_read;
 	}
 
-	return GNOME_VFS_ERROR_GENERIC;
+	return GNOME_VFS_OK;
 }
 
 static GnomeVFSResult
@@ -407,7 +369,7 @@
 	 GnomeVFSOpenMode mode,
 	 GnomeVFSContext *context)
 {
-	GnomeVFSResult result = GNOME_VFS_OK;
+	GnomeVFSResult result;
 	char *cmd;
 	SshHandle *handle = NULL;
 
@@ -502,20 +464,8 @@
 {
 	GnomeVFSResult result, res_secondary;
 
-	result =  ssh_read ((SshHandle *)method_handle, buffer, num_bytes,
-			bytes_read);
-	if (result != GNOME_VFS_OK) {
-		return result;
-	}
-
-	if (*bytes_read == 0) {	
-		res_secondary = ssh_check_for_done ((SshHandle *)method_handle);
-		if (res_secondary != GNOME_VFS_OK) {
-			result = res_secondary;
-		} else {
-			result = GNOME_VFS_ERROR_EOF;
-		}
-	}
+	result =  ssh_read (((SshHandle *)method_handle)->read_fd, buffer,
+			    num_bytes, bytes_read);
 	return result;
 }
 
@@ -626,7 +576,7 @@
                      return;
              }               
              
-             result = ssh_read (handle, &c, 1, &bytes_read);
+             result = ssh_read (handle->read_fd, &c, 1, &bytes_read);
              if ((bytes_read > 0) && (c == '0')) {
                      file_info->permissions |= params[i].perm;
              } else {
@@ -669,21 +619,12 @@
 		bytes_read = 0;
 
 		while (i<LINE_LENGTH) {
-			result = ssh_read (handle, &c,
-					   sizeof(char), &bytes_read);
+			result = ssh_read (handle->read_fd, &c,
+					   1, &bytes_read);
 			if (bytes_read == 0 || c == '\r' || c == '\n') {
 				break;
 			}
 
-			if (result != GNOME_VFS_OK) {
-				res_secondary = ssh_check_for_done (handle);
-				if (res_secondary != GNOME_VFS_OK) {
-					result = res_secondary;
-				} else {
-					return result;
-				}
-			}
-
 			line[i] = c;
 			i++;
 		}
@@ -774,7 +715,6 @@
 		*/
 		break;
 	}
-
 	return result;
 }
 
@@ -799,9 +739,9 @@
 	quoted_name = g_shell_quote (name);
 
 	if (*name != '\0') {
-		cmd = g_strdup_printf ("ls -ld %s 2>&1", quoted_name);
+		cmd = g_strdup_printf ("ls -ld %s", quoted_name);
 	} else {
-		cmd = g_strdup_printf ("ls -ld '/' 2>&1");
+		cmd = g_strdup_printf ("ls -ld '/'");
 	}
 
 	result = ssh_connect (&handle, uri, cmd);
@@ -1065,7 +1005,7 @@
 			char c;
 			GnomeVFSResult res;
 			GnomeVFSFileSize bytes_read;
-			res = ssh_read (handle, &c, 1, &bytes_read);
+			res = ssh_read (handle->read_fd, &c, 1, &bytes_read);
 			if (bytes_read == 0 || res != GNOME_VFS_OK)
 				break;
 		}
