GNOME Bugzilla – Bug 705902
g_get_current_dir() should check PWD env var and return it if it's correct
Last modified: 2013-12-09 18:31:24 UTC
get_current_dir_name() is a GNU extension, so it is not available on system without glibc.
get_current_dir_name() is used for a reason (as stated in the comment immediately before its use in the source code), so you'll have to furnish a reimplementation of it for non-glibc systems.
I found get_current_dir_name() is in io/getdirname.c of glibc source.
Got a patch?
g_get_current_dir()?
/* We use get_current_dir_name() here instead of getcwd / g_get_current_dir() * because we want to use the value from PWD (if it is correct). * See bug 502146. I see. Maybe we could modify g_get_current_dir(), then.
Note: the code from glibc is this: char * get_current_dir_name (void) { char *pwd; struct stat64 dotstat, pwdstat; pwd = getenv ("PWD"); if (pwd != NULL && stat64 (".", &dotstat) == 0 && stat64 (pwd, &pwdstat) == 0 && pwdstat.st_dev == dotstat.st_dev && pwdstat.st_ino == dotstat.st_ino) /* The PWD value is correct. Use it. */ return __strdup (pwd); return __getcwd ((char *) NULL, 0); } We could just call g_get_current_dir() instead of __getcwd() as the last line there (since getcwd(NULL) is not portable).
Created attachment 263779 [details] [review] g_get_current_dir(): consult PWD first Check if the current directory is the same as $PWD. This matches the behaviour of the get_current_dir_name() function in glibc.
-> glib If this gets in, I'll fix g-t to use g_get_current_dir().
I only uploaded this patch for discussion. Although, fwiw, I'd like it if GApplication worked this way too...
Review of attachment 263779 [details] [review]: Should update the docs to say: Since GLib 2.40, this function will honor the "PWD" environment variable if it is set and appears correct. While annotate will link to this bug, I'd say it's worth a comment too. One thing we should do to be good citizens is clear PWD if we've detected it's wrong when we spawn a child process in execve(). PWD is really kind of a historical hack - the goal is to record for the user how they got to a directory, not the result of realpath() or the like. Which matters pretty much only for Unix shells. Sure you *can* spawn a program from Nautilus, but graphical programs don't have a visible representation of their PWD.
Comment on attachment 263779 [details] [review] g_get_current_dir(): consult PWD first Attachment 263779 [details] pushed as a22f777 - g_get_current_dir(): consult PWD first g-t patch on the way.
Created attachment 263826 [details] [review] Use g_get_current_dir() get_current_dir_name() is a non-portable GNU extension. Use g_get_current_dir() instead, now that it does the same thing (ie: respecting the value of PWD).
Comment on attachment 263826 [details] [review] Use g_get_current_dir() Please also bump the glib version req in configure. Thanks!
Attachment 263826 [details] pushed as d52fe59 - Use g_get_current_dir()