diff --git a/builtins/cd.def b/builtins/cd.def index b87c5d9d..2482e4c9 100644 --- a/builtins/cd.def +++ b/builtins/cd.def @@ -343,75 +343,78 @@ cd_builtin (list) return (EXECUTION_FAILURE); } #endif - else if (list->word->word[0] == '-' && list->word->word[1] == '\0') + else { - /* This is `cd -', equivalent to `cd $OLDPWD' */ - dirname = get_string_value ("OLDPWD"); - - if (dirname == 0) + if (list->word->word[0] == '-' && list->word->word[1] == '\0') { - builtin_error (_("OLDPWD not set")); - return (EXECUTION_FAILURE); - } + /* This is `cd -', equivalent to `cd $OLDPWD' */ + dirname = get_string_value ("OLDPWD"); + + if (dirname == 0) + { + builtin_error (_("OLDPWD not set")); + return (EXECUTION_FAILURE); + } + #if 0 - lflag = interactive ? LCD_PRINTPATH : 0; + lflag = interactive ? LCD_PRINTPATH : 0; #else - lflag = LCD_PRINTPATH; /* According to SUSv3 */ + lflag = LCD_PRINTPATH; /* According to SUSv3 */ #endif - } - else if (absolute_pathname (list->word->word)) - dirname = list->word->word; - else if (privileged_mode == 0 && (cdpath = get_string_value ("CDPATH"))) - { - dirname = list->word->word; + } + else + dirname = list->word->word; - /* Find directory in $CDPATH. */ - path_index = 0; - while (path = extract_colon_unit (cdpath, &path_index)) + if (!absolute_pathname (dirname) && + privileged_mode == 0 && + (cdpath = get_string_value ("CDPATH"))) { - /* OPT is 1 if the path element is non-empty */ - opt = path[0] != '\0'; - temp = sh_makepath (path, dirname, MP_DOTILDE); - free (path); - - if (change_to_directory (temp, no_symlinks, xattrflag)) + /* Find directory in $CDPATH. */ + path_index = 0; + while (path = extract_colon_unit (cdpath, &path_index)) { - /* POSIX.2 says that if a nonempty directory from CDPATH - is used to find the directory to change to, the new - directory name is echoed to stdout, whether or not - the shell is interactive. */ - if (opt && (path = no_symlinks ? temp : the_current_working_directory)) - printf ("%s\n", path); - - free (temp); + /* OPT is 1 if the path element is non-empty */ + opt = path[0] != '\0'; + temp = sh_makepath (path, dirname, MP_DOTILDE); + free (path); + + if (change_to_directory (temp, no_symlinks, xattrflag)) + { + /* POSIX.2 says that if a nonempty directory from CDPATH + is used to find the directory to change to, the new + directory name is echoed to stdout, whether or not + the shell is interactive. */ + if (opt && (path = no_symlinks ? temp : the_current_working_directory)) + printf ("%s\n", path); + + free (temp); #if 0 - /* Posix.2 says that after using CDPATH, the resultant - value of $PWD will not contain `.' or `..'. */ - return (bindpwd (posixly_correct || no_symlinks)); + /* Posix.2 says that after using CDPATH, the resultant + value of $PWD will not contain `.' or `..'. */ + return (bindpwd (posixly_correct || no_symlinks)); #else - return (bindpwd (no_symlinks)); + return (bindpwd (no_symlinks)); #endif + } + else + free (temp); } - else - free (temp); - } #if 0 - /* changed for bash-4.2 Posix cd description steps 5-6 */ - /* POSIX.2 says that if `.' does not appear in $CDPATH, we don't - try the current directory, so we just punt now with an error - message if POSIXLY_CORRECT is non-zero. The check for cdpath[0] - is so we don't mistakenly treat a CDPATH value of "" as not - specifying the current directory. */ - if (posixly_correct && cdpath[0]) - { - builtin_error ("%s: %s", dirname, strerror (ENOENT)); - return (EXECUTION_FAILURE); - } + /* changed for bash-4.2 Posix cd description steps 5-6 */ + /* POSIX.2 says that if `.' does not appear in $CDPATH, we don't + try the current directory, so we just punt now with an error + message if POSIXLY_CORRECT is non-zero. The check for cdpath[0] + is so we don't mistakenly treat a CDPATH value of "" as not + specifying the current directory. */ + if (posixly_correct && cdpath[0]) + { + builtin_error ("%s: %s", dirname, strerror (ENOENT)); + return (EXECUTION_FAILURE); + } #endif + } } - else - dirname = list->word->word; /* When we get here, DIRNAME is the directory to change to. If we chdir successfully, just return. */