From: Paul Eggert Date: Sun, 8 Oct 2017 05:56:29 +0000 (-0700) Subject: Improve test for unreachable dirs X-Git-Tag: emacs-26.0.90~43 X-Git-Url: http://git.eshelyaron.com/gitweb/?a=commitdiff_plain;h=7c2c117c91;p=emacs.git Improve test for unreachable dirs * src/sysdep.c (get_current_dir_name_or_unreachable): New function, with most of the old contents of emacs_get_current_dir_name. (emacs_get_current_dir_name): Use it. Use a simpler test for unreachable directory strings, and also apply it to getcwd etc. (Bug#27871) --- diff --git a/src/sysdep.c b/src/sysdep.c index 8291a606bea..c3484920d0c 100644 --- a/src/sysdep.c +++ b/src/sysdep.c @@ -221,9 +221,12 @@ init_standard_fds (void) } /* Return the current working directory. The result should be freed - with 'free'. Return NULL on errors. */ -char * -emacs_get_current_dir_name (void) + with 'free'. Return NULL (setting errno) on errors. If the + current directory is unreachable, return either NULL or a string + beginning with '('. */ + +static char * +get_current_dir_name_or_unreachable (void) { # if HAVE_GET_CURRENT_DIR_NAME && !BROKEN_GET_CURRENT_DIR_NAME # ifdef HYBRID_MALLOC @@ -233,16 +236,9 @@ emacs_get_current_dir_name (void) # endif if (use_libc) { - /* GNU/Linux get_current_dir_name can return a string starting - with "(unreachable)" (Bug#27871). */ - char *wd = get_current_dir_name (); - if (wd && ! (IS_DIRECTORY_SEP (*wd) || (*wd && IS_DEVICE_SEP (wd[1])))) - { - free (wd); - errno = ENOENT; - return NULL; - } - return wd; + /* For an unreachable directory, this returns a string that starts + with "(unreachable)"; see Bug#27871. */ + return get_current_dir_name (); } # endif @@ -294,6 +290,23 @@ emacs_get_current_dir_name (void) return buf; } +/* Return the current working directory. The result should be freed + with 'free'. Return NULL (setting errno) on errors; an unreachable + directory (e.g., its name starts with '(') counts as an error. */ + +char * +emacs_get_current_dir_name (void) +{ + char *dir = get_current_dir_name_or_unreachable (); + if (dir && *dir == '(') + { + free (dir); + errno = ENOENT; + return NULL; + } + return dir; +} + /* Discard pending input on all input descriptors. */