Working directory



Hi,

I've found that some applications (such as eclipse - http://www.eclipse.org) expect to have their working directory be set to the directory that the executable resides in. This behaviour is also good for launching files (e.g. if I use nautilus to open ~/music/song.mp3, nautilus should open my mp3 player with working directory ~/music), so I've attached a diff that attempts to do this.

Warning: I haven't tried to compile this, and I don't know if this is the correct way to go about it (in short, I have no clue what I'm doing).

The way it works is it modifies nautilus_launch_application and nautilus_launch_application_from_command in libnautilus-private/nautilus-program-choosing.c:

nautilus_launch_application prepends a "cd [file's directory]; " to the command passed to nautilus_launch_application_from_command, and the latter prepends "cd [executable's directory]; " if no arguments were passed to it.

Is this even remotely correct?

Thanks,
Yury


_________________________________________________________________

--- nautilus/libnautilus-private/nautilus-program-choosing.c 2002-11-07 14:20:40.000000000 -0500 +++ nautilus/libnautilus-private/new-nautilus-program-choosing.c 2003-03-28 17:01:49.000000000 -0500
@@ -543,10 +543,30 @@
{
	GdkScreen *screen;
	char      *parameter;
+	char      *command;
	char      *uri_scheme, *uri;

+	uri = NULL;
+	command = application->command;
	uri_scheme = nautilus_file_get_uri_scheme (file);

+	if (eel_strcasecmp (uri_scheme, "file") != 0) {
+		char *local_file;
+		char *working_directory;
+		char *quoted_working_directory;
+
+		uri = nautilus_file_get_uri (file);
+		local_file = gnome_vfs_get_local_path_from_uri (uri);
+		working_directory = g_path_get_dirname (local_file);
+		quoted_working_directory = g_shell_quote (working_directory);
+
+ command = g_strconcat ("cd ", quoted_working_directory, "; ", command, NULL);
+
+		g_free (quoted_working_directory);
+		g_free (working_directory);
+		g_free (local_file);
+	}
+
	/* If the program can open URIs, always use a URI. This
	 * prevents any possible ambiguity for cases where a path
	 * would looks like a URI.
@@ -567,9 +587,8 @@
		}
		parameter = nautilus_file_get_uri (file);
	} else {
-		uri = nautilus_file_get_uri (file);
+		if (!uri) uri = nautilus_file_get_uri (file);
		parameter = gnome_vfs_get_local_path_from_uri (uri);
-		g_free (uri);

		if (parameter == NULL) {
			/* This application can't deal with this URI,
@@ -587,16 +606,20 @@
			return;
		}
	}
+	if (uri) g_free (uri)
	g_free (uri_scheme);

	screen = gtk_window_get_screen (parent_window);

	nautilus_launch_application_from_command (screen,
						  application->name,
-						  application->command,
+						  command,
						  parameter,
						  application->requires_terminal);

+	if (command != application->command)
+		g_free (command);
+
	g_free (parameter);
}

@@ -620,16 +643,29 @@
					  gboolean use_terminal)
{
	char *full_command;
-	char *quoted_parameter;
+	char *quoted_parameter;
+	char *working_directory;
+	char *quoted_working_directory;

	if (parameter != NULL) {
		quoted_parameter = g_shell_quote (parameter);
		full_command = g_strconcat (command_string, " ", quoted_parameter, NULL);
		g_free (quoted_parameter);
	} else {
-		full_command = g_strdup (command_string);
+		char **command_string_parts;
+
+		command_string_parts = g_strsplit (command_string, " ", 2);
+		working_directory = g_path_get_dirname (command_string_parts[0]);
+		g_strfreev (command_string_parts);
+
+		quoted_working_directory = g_shell_quote (working_directory);
+
+ full_command = g_strconcat ("cd ", quoted_working_directory, "; ", command_string, NULL);
	}

+	g_free (working_directory);
+	g_free (quoted_working_directory);
+
	if (use_terminal) {
		eel_gnome_open_terminal_on_screen (full_command, screen);
	} else {



[Date Prev][Date Next]   [Thread Prev][Thread Next]   [Thread Index] [Date Index] [Author Index]