X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=builtins%2Fcd.def;h=1c58c7c1602ca286fc9ed7799feacfb1935450f5;hb=refs%2Ftags%2Fdevel-base-dist;hp=77e8f82155a84f3c956df74db49edc5bc9ea6149;hpb=f73dda092b33638d2d5e9c35375f687a607b5403;p=platform%2Fupstream%2Fbash.git diff --git a/builtins/cd.def b/builtins/cd.def index 77e8f82..1c58c7c 100644 --- a/builtins/cd.def +++ b/builtins/cd.def @@ -69,7 +69,7 @@ int cdable_vars; $BUILTIN cd $FUNCTION cd_builtin -$SHORT_DOC cd [-PL] [dir] +$SHORT_DOC cd [-L|-P] [dir] Change the current directory to DIR. The variable $HOME is the default DIR. The variable CDPATH defines the search path for the directory containing DIR. Alternative directory names in CDPATH @@ -118,6 +118,19 @@ bindpwd (no_symlinks) return (EXECUTION_SUCCESS); } +/* Call get_working_directory to reset the value of + the_current_working_directory () */ +static char * +resetpwd () +{ + char *tdir; + + FREE (the_current_working_directory); + the_current_working_directory = (char *)NULL; + tdir = get_working_directory ("cd"); + return (tdir); +} + #define LCD_DOVARS 0x001 #define LCD_DOSPELL 0x002 #define LCD_PRINTPATH 0x004 @@ -137,7 +150,7 @@ cd_builtin (list) #if defined (RESTRICTED_SHELL) if (restricted) { - builtin_error ("restricted"); + sh_restricted ((char *)NULL); return (EXECUTION_FAILURE); } #endif /* RESTRICTED_SHELL */ @@ -350,7 +363,7 @@ change_to_directory (newdir, nolinks) int nolinks; { char *t, *tdir; - int err; + int err, canon_failed; tdir = (char *)NULL; @@ -370,20 +383,38 @@ change_to_directory (newdir, nolinks) /* Use the canonicalized version of NEWDIR, or, if canonicalization failed, use the non-canonical form. */ + canon_failed = 0; if (tdir && *tdir) free (t); else { FREE (tdir); tdir = t; + canon_failed = 1; + } + + /* In POSIX mode, if we're resolving symlinks logically and sh_canonpath + returns NULL (because it checks the path, it will return NULL if the + resolved path doesn't exist), fail immediately. */ + if (posixly_correct && nolinks == 0 && canon_failed) + { + errno = ENOENT; + return (0); } /* If the chdir succeeds, update the_current_working_directory. */ if (chdir (nolinks ? newdir : tdir) == 0) { - FREE (the_current_working_directory); - the_current_working_directory = tdir; - + /* If canonicalization failed, but the chdir succeeded, reset the + shell's idea of the_current_working_directory. */ + if (canon_failed) + resetpwd (); + else + { + FREE (the_current_working_directory); + the_current_working_directory = tdir; + } + return (1); } @@ -400,9 +431,7 @@ change_to_directory (newdir, nolinks) verbatim. If we succeed, reinitialize the_current_working_directory. */ if (chdir (newdir) == 0) { - FREE (the_current_working_directory); - the_current_working_directory = (char *)NULL; - tdir = get_working_directory ("cd"); + tdir = resetpwd (); FREE (tdir); return (1);