Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 1 | This file is cd.def, from which is created cd.c. It implements the |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 2 | builtins "cd" and "pwd" in Bash. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 3 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 4 | Copyright (C) 1987-2013 Free Software Foundation, Inc. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 5 | |
| 6 | This file is part of GNU Bash, the Bourne Again SHell. |
| 7 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 8 | Bash is free software: you can redistribute it and/or modify |
| 9 | it under the terms of the GNU General Public License as published by |
| 10 | the Free Software Foundation, either version 3 of the License, or |
| 11 | (at your option) any later version. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 12 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 13 | Bash is distributed in the hope that it will be useful, |
| 14 | but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 15 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 16 | GNU General Public License for more details. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 17 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 18 | You should have received a copy of the GNU General Public License |
| 19 | along with Bash. If not, see <http://www.gnu.org/licenses/>. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 20 | |
| 21 | $PRODUCES cd.c |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 22 | #include <config.h> |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 23 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 24 | #if defined (HAVE_UNISTD_H) |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 25 | # ifdef _MINIX |
| 26 | # include <sys/types.h> |
| 27 | # endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 28 | # include <unistd.h> |
| 29 | #endif |
| 30 | |
| 31 | #include "../bashtypes.h" |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 32 | #include "posixdir.h" |
| 33 | #include "posixstat.h" |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 34 | #if defined (HAVE_SYS_PARAM_H) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 35 | #include <sys/param.h> |
Jari Aalto | cce855b | 1998-04-17 19:52:44 +0000 | [diff] [blame] | 36 | #endif |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 37 | #include <fcntl.h> |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 38 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 39 | #include <stdio.h> |
| 40 | |
| 41 | #include "../bashansi.h" |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 42 | #include "../bashintl.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 43 | |
| 44 | #include <errno.h> |
| 45 | #include <tilde/tilde.h> |
| 46 | |
| 47 | #include "../shell.h" |
| 48 | #include "../flags.h" |
Jari Aalto | bb70624 | 2000-03-17 21:46:59 +0000 | [diff] [blame] | 49 | #include "maxpath.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 50 | #include "common.h" |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 51 | #include "bashgetopt.h" |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 52 | |
| 53 | #if !defined (errno) |
Ricardo Cerqueira | a02fbff | 2013-07-25 22:35:34 +0100 | [diff] [blame] | 54 | #include <errno.h> |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 55 | #endif /* !errno */ |
| 56 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 57 | extern int posixly_correct; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 58 | extern int array_needs_making; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 59 | extern const char * const bash_getcwd_errstr; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 60 | |
Jari Aalto | f73dda0 | 2001-11-13 17:56:06 +0000 | [diff] [blame] | 61 | static int bindpwd __P((int)); |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 62 | static int setpwd __P((char *)); |
Jari Aalto | 0628567 | 2006-10-10 14:15:34 +0000 | [diff] [blame] | 63 | static char *resetpwd __P((char *)); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 64 | static int change_to_directory __P((char *, int, int)); |
| 65 | |
| 66 | static int cdxattr __P((char *, char **)); |
| 67 | static void resetxattr __P((void)); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 68 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 69 | /* Change this to 1 to get cd spelling correction by default. */ |
| 70 | int cdspelling = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 71 | |
| 72 | int cdable_vars; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 73 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 74 | static int eflag; /* file scope so bindpwd() can see it */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 75 | static int xattrflag; /* O_XATTR support for openat */ |
| 76 | static int xattrfd = -1; |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 77 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 78 | $BUILTIN cd |
| 79 | $FUNCTION cd_builtin |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 80 | $SHORT_DOC cd [-L|[-P [-e]] [-@]] [dir] |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 81 | Change the shell working directory. |
| 82 | |
| 83 | Change the current directory to DIR. The default DIR is the value of the |
| 84 | HOME shell variable. |
| 85 | |
| 86 | The variable CDPATH defines the search path for the directory containing |
| 87 | DIR. Alternative directory names in CDPATH are separated by a colon (:). |
| 88 | A null directory name is the same as the current directory. If DIR begins |
| 89 | with a slash (/), then CDPATH is not used. |
| 90 | |
| 91 | If the directory is not found, and the shell option `cdable_vars' is set, |
| 92 | the word is assumed to be a variable name. If that variable has a value, |
| 93 | its value is used for DIR. |
| 94 | |
| 95 | Options: |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 96 | -L force symbolic links to be followed: resolve symbolic links in |
| 97 | DIR after processing instances of `..' |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 98 | -P use the physical directory structure without following symbolic |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 99 | links: resolve symbolic links in DIR before processing instances |
| 100 | of `..' |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 101 | -e if the -P option is supplied, and the current working directory |
| 102 | cannot be determined successfully, exit with a non-zero status |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 103 | #if defined (O_XATTR) |
| 104 | -@ on systems that support it, present a file with extended attributes |
| 105 | as a directory containing the file attributes |
| 106 | #endif |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 107 | |
| 108 | The default is to follow symbolic links, as if `-L' were specified. |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 109 | `..' is processed by removing the immediately previous pathname component |
| 110 | back to a slash or the beginning of DIR. |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 111 | |
| 112 | Exit Status: |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 113 | Returns 0 if the directory is changed, and if $PWD is set successfully when |
| 114 | -P is used; non-zero otherwise. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 115 | $END |
| 116 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 117 | /* Just set $PWD, don't change OLDPWD. Used by `pwd -P' in posix mode. */ |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 118 | static int |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 119 | setpwd (dirname) |
| 120 | char *dirname; |
| 121 | { |
| 122 | int old_anm; |
| 123 | SHELL_VAR *tvar; |
| 124 | |
| 125 | old_anm = array_needs_making; |
| 126 | tvar = bind_variable ("PWD", dirname ? dirname : "", 0); |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 127 | if (tvar && readonly_p (tvar)) |
| 128 | return EXECUTION_FAILURE; |
| 129 | if (tvar && old_anm == 0 && array_needs_making && exported_p (tvar)) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 130 | { |
| 131 | update_export_env_inplace ("PWD=", 4, dirname ? dirname : ""); |
| 132 | array_needs_making = 0; |
| 133 | } |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 134 | return EXECUTION_SUCCESS; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 135 | } |
| 136 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 137 | static int |
| 138 | bindpwd (no_symlinks) |
| 139 | int no_symlinks; |
| 140 | { |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 141 | char *dirname, *pwdvar; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 142 | int old_anm, r; |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 143 | SHELL_VAR *tvar; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 144 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 145 | r = sh_chkwrite (EXECUTION_SUCCESS); |
| 146 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 147 | #define tcwd the_current_working_directory |
| 148 | dirname = tcwd ? (no_symlinks ? sh_physpath (tcwd, 0) : tcwd) |
| 149 | : get_working_directory ("cd"); |
| 150 | #undef tcwd |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 151 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 152 | old_anm = array_needs_making; |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 153 | pwdvar = get_string_value ("PWD"); |
| 154 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 155 | tvar = bind_variable ("OLDPWD", pwdvar, 0); |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 156 | if (tvar && readonly_p (tvar)) |
| 157 | r = EXECUTION_FAILURE; |
| 158 | |
Jari Aalto | d166f04 | 1997-06-05 14:59:13 +0000 | [diff] [blame] | 159 | if (old_anm == 0 && array_needs_making && exported_p (tvar)) |
| 160 | { |
Jari Aalto | b72432f | 1999-02-19 17:11:39 +0000 | [diff] [blame] | 161 | update_export_env_inplace ("OLDPWD=", 7, pwdvar); |
| 162 | array_needs_making = 0; |
| 163 | } |
| 164 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 165 | if (setpwd (dirname) == EXECUTION_FAILURE) |
| 166 | r = EXECUTION_FAILURE; |
| 167 | if (dirname == 0 && eflag) |
| 168 | r = EXECUTION_FAILURE; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 169 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 170 | if (dirname && dirname != the_current_working_directory) |
| 171 | free (dirname); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 172 | |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 173 | return (r); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 174 | } |
| 175 | |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 176 | /* Call get_working_directory to reset the value of |
| 177 | the_current_working_directory () */ |
| 178 | static char * |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 179 | resetpwd (caller) |
| 180 | char *caller; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 181 | { |
| 182 | char *tdir; |
| 183 | |
| 184 | FREE (the_current_working_directory); |
| 185 | the_current_working_directory = (char *)NULL; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 186 | tdir = get_working_directory (caller); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 187 | return (tdir); |
| 188 | } |
| 189 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 190 | static int |
| 191 | cdxattr (dir, ndirp) |
| 192 | char *dir; /* don't assume we can always free DIR */ |
| 193 | char **ndirp; /* return new constructed directory name */ |
| 194 | { |
| 195 | #if defined (O_XATTR) |
| 196 | int apfd, fd, r, e; |
| 197 | char buf[11+40+40]; /* construct new `fake' path for pwd */ |
| 198 | |
| 199 | apfd = openat (AT_FDCWD, dir, O_RDONLY|O_NONBLOCK); |
| 200 | if (apfd < 0) |
| 201 | return -1; |
| 202 | fd = openat (apfd, ".", O_XATTR); |
| 203 | e = errno; |
| 204 | close (apfd); /* ignore close error for now */ |
| 205 | errno = e; |
| 206 | if (fd < 0) |
| 207 | return -1; |
| 208 | r = fchdir (fd); /* assume fchdir exists everywhere with O_XATTR */ |
| 209 | if (r < 0) |
| 210 | { |
| 211 | close (fd); |
| 212 | return -1; |
| 213 | } |
| 214 | /* NFSv4 and ZFS extended attribute directories do not have names which are |
| 215 | visible in the standard Unix directory tree structure. To ensure we have |
| 216 | a valid name for $PWD, we synthesize one under /proc, but to keep that |
| 217 | path valid, we need to keep the file descriptor open as long as we are in |
| 218 | this directory. This imposes a certain structure on /proc. */ |
| 219 | if (ndirp) |
| 220 | { |
| 221 | sprintf (buf, "/proc/%d/fd/%d", getpid(), fd); |
| 222 | *ndirp = savestring (buf); |
| 223 | } |
| 224 | |
| 225 | if (xattrfd >= 0) |
| 226 | close (xattrfd); |
| 227 | xattrfd = fd; |
| 228 | |
| 229 | return r; |
| 230 | #else |
| 231 | return -1; |
| 232 | #endif |
| 233 | } |
| 234 | |
| 235 | /* Clean up the O_XATTR baggage. Currently only closes xattrfd */ |
| 236 | static void |
| 237 | resetxattr () |
| 238 | { |
| 239 | #if defined (O_XATTR) |
| 240 | if (xattrfd >= 0) |
| 241 | { |
| 242 | close (xattrfd); |
| 243 | xattrfd = -1; |
| 244 | } |
| 245 | #else |
| 246 | xattrfd = -1; /* not strictly necessary */ |
| 247 | #endif |
| 248 | } |
| 249 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 250 | #define LCD_DOVARS 0x001 |
| 251 | #define LCD_DOSPELL 0x002 |
| 252 | #define LCD_PRINTPATH 0x004 |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 253 | #define LCD_FREEDIRNAME 0x008 |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 254 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 255 | /* This builtin is ultimately the way that all user-visible commands should |
| 256 | change the current working directory. It is called by cd_to_string (), |
| 257 | so the programming interface is simple, and it handles errors and |
| 258 | restrictions properly. */ |
| 259 | int |
| 260 | cd_builtin (list) |
| 261 | WORD_LIST *list; |
| 262 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 263 | char *dirname, *cdpath, *path, *temp; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 264 | int path_index, no_symlinks, opt, lflag; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 265 | |
| 266 | #if defined (RESTRICTED_SHELL) |
| 267 | if (restricted) |
| 268 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 269 | sh_restricted ((char *)NULL); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 270 | return (EXECUTION_FAILURE); |
| 271 | } |
| 272 | #endif /* RESTRICTED_SHELL */ |
| 273 | |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 274 | eflag = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 275 | no_symlinks = no_symbolic_links; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 276 | xattrflag = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 277 | reset_internal_getopt (); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 278 | #if defined (O_XATTR) |
| 279 | while ((opt = internal_getopt (list, "eLP@")) != -1) |
| 280 | #else |
| 281 | while ((opt = internal_getopt (list, "eLP")) != -1) |
| 282 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 283 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 284 | switch (opt) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 285 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 286 | case 'P': |
| 287 | no_symlinks = 1; |
| 288 | break; |
| 289 | case 'L': |
| 290 | no_symlinks = 0; |
| 291 | break; |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 292 | case 'e': |
| 293 | eflag = 1; |
| 294 | break; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 295 | #if defined (O_XATTR) |
| 296 | case '@': |
| 297 | xattrflag = 1; |
| 298 | break; |
| 299 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 300 | default: |
| 301 | builtin_usage (); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 302 | return (EX_USAGE); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 303 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 304 | } |
| 305 | list = loptend; |
| 306 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 307 | lflag = (cdable_vars ? LCD_DOVARS : 0) | |
| 308 | ((interactive && cdspelling) ? LCD_DOSPELL : 0); |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 309 | if (eflag && no_symlinks == 0) |
| 310 | eflag = 0; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 311 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 312 | if (list == 0) |
| 313 | { |
| 314 | /* `cd' without arguments is equivalent to `cd $HOME' */ |
| 315 | dirname = get_string_value ("HOME"); |
| 316 | |
| 317 | if (dirname == 0) |
| 318 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 319 | builtin_error (_("HOME not set")); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 320 | return (EXECUTION_FAILURE); |
| 321 | } |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 322 | lflag = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 323 | } |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 324 | #if defined (CD_COMPLAINS) |
| 325 | else if (list->next) |
| 326 | { |
| 327 | builtin_error (_("too many arguments")); |
| 328 | return (EXECUTION_FAILURE); |
| 329 | } |
| 330 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 331 | else if (list->word->word[0] == '-' && list->word->word[1] == '\0') |
| 332 | { |
| 333 | /* This is `cd -', equivalent to `cd $OLDPWD' */ |
| 334 | dirname = get_string_value ("OLDPWD"); |
| 335 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 336 | if (dirname == 0) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 337 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 338 | builtin_error (_("OLDPWD not set")); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 339 | return (EXECUTION_FAILURE); |
| 340 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 341 | #if 0 |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 342 | lflag = interactive ? LCD_PRINTPATH : 0; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 343 | #else |
| 344 | lflag = LCD_PRINTPATH; /* According to SUSv3 */ |
| 345 | #endif |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 346 | } |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 347 | else if (absolute_pathname (list->word->word)) |
| 348 | dirname = list->word->word; |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 349 | else if (privileged_mode == 0 && (cdpath = get_string_value ("CDPATH"))) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 350 | { |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 351 | dirname = list->word->word; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 352 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 353 | /* Find directory in $CDPATH. */ |
| 354 | path_index = 0; |
| 355 | while (path = extract_colon_unit (cdpath, &path_index)) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 356 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 357 | /* OPT is 1 if the path element is non-empty */ |
| 358 | opt = path[0] != '\0'; |
| 359 | temp = sh_makepath (path, dirname, MP_DOTILDE); |
| 360 | free (path); |
| 361 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 362 | if (change_to_directory (temp, no_symlinks, xattrflag)) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 363 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 364 | /* POSIX.2 says that if a nonempty directory from CDPATH |
| 365 | is used to find the directory to change to, the new |
| 366 | directory name is echoed to stdout, whether or not |
| 367 | the shell is interactive. */ |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 368 | if (opt && (path = no_symlinks ? temp : the_current_working_directory)) |
| 369 | printf ("%s\n", path); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 370 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 371 | free (temp); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 372 | #if 0 |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 373 | /* Posix.2 says that after using CDPATH, the resultant |
| 374 | value of $PWD will not contain `.' or `..'. */ |
| 375 | return (bindpwd (posixly_correct || no_symlinks)); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 376 | #else |
| 377 | return (bindpwd (no_symlinks)); |
| 378 | #endif |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 379 | } |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 380 | else |
| 381 | free (temp); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 382 | } |
| 383 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 384 | #if 0 |
| 385 | /* changed for bash-4.2 Posix cd description steps 5-6 */ |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 386 | /* POSIX.2 says that if `.' does not appear in $CDPATH, we don't |
| 387 | try the current directory, so we just punt now with an error |
| 388 | message if POSIXLY_CORRECT is non-zero. The check for cdpath[0] |
| 389 | is so we don't mistakenly treat a CDPATH value of "" as not |
| 390 | specifying the current directory. */ |
| 391 | if (posixly_correct && cdpath[0]) |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 392 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 393 | builtin_error ("%s: %s", dirname, strerror (ENOENT)); |
| 394 | return (EXECUTION_FAILURE); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 395 | } |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 396 | #endif |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 397 | } |
| 398 | else |
| 399 | dirname = list->word->word; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 400 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 401 | /* When we get here, DIRNAME is the directory to change to. If we |
| 402 | chdir successfully, just return. */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 403 | if (change_to_directory (dirname, no_symlinks, xattrflag)) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 404 | { |
| 405 | if (lflag & LCD_PRINTPATH) |
| 406 | printf ("%s\n", dirname); |
| 407 | return (bindpwd (no_symlinks)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 408 | } |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 409 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 410 | /* If the user requests it, then perhaps this is the name of |
| 411 | a shell variable, whose value contains the directory to |
| 412 | change to. */ |
| 413 | if (lflag & LCD_DOVARS) |
| 414 | { |
| 415 | temp = get_string_value (dirname); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 416 | if (temp && change_to_directory (temp, no_symlinks, xattrflag)) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 417 | { |
| 418 | printf ("%s\n", temp); |
| 419 | return (bindpwd (no_symlinks)); |
| 420 | } |
| 421 | } |
| 422 | |
| 423 | /* If the user requests it, try to find a directory name similar in |
| 424 | spelling to the one requested, in case the user made a simple |
| 425 | typo. This is similar to the UNIX 8th and 9th Edition shells. */ |
| 426 | if (lflag & LCD_DOSPELL) |
| 427 | { |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 428 | temp = dirspell (dirname); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 429 | if (temp && change_to_directory (temp, no_symlinks, xattrflag)) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 430 | { |
| 431 | printf ("%s\n", temp); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 432 | free (temp); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 433 | return (bindpwd (no_symlinks)); |
| 434 | } |
| 435 | else |
| 436 | FREE (temp); |
| 437 | } |
| 438 | |
| 439 | builtin_error ("%s: %s", dirname, strerror (errno)); |
| 440 | return (EXECUTION_FAILURE); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 441 | } |
| 442 | |
| 443 | $BUILTIN pwd |
| 444 | $FUNCTION pwd_builtin |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 445 | $SHORT_DOC pwd [-LP] |
Jari Aalto | 3185942 | 2009-01-12 13:36:28 +0000 | [diff] [blame] | 446 | Print the name of the current working directory. |
| 447 | |
| 448 | Options: |
| 449 | -L print the value of $PWD if it names the current working |
| 450 | directory |
| 451 | -P print the physical directory, without any symbolic links |
| 452 | |
| 453 | By default, `pwd' behaves as if `-L' were specified. |
| 454 | |
| 455 | Exit Status: |
| 456 | Returns 0 unless an invalid option is given or the current directory |
| 457 | cannot be read. |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 458 | $END |
| 459 | |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 460 | /* Non-zero means that pwd always prints the physical directory, without |
| 461 | symbolic links. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 462 | static int verbatim_pwd; |
| 463 | |
| 464 | /* Print the name of the current working directory. */ |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 465 | int |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 466 | pwd_builtin (list) |
| 467 | WORD_LIST *list; |
| 468 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 469 | char *directory; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 470 | int opt, pflag; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 471 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 472 | verbatim_pwd = no_symbolic_links; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 473 | pflag = 0; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 474 | reset_internal_getopt (); |
| 475 | while ((opt = internal_getopt (list, "LP")) != -1) |
| 476 | { |
| 477 | switch (opt) |
| 478 | { |
| 479 | case 'P': |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 480 | verbatim_pwd = pflag = 1; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 481 | break; |
| 482 | case 'L': |
| 483 | verbatim_pwd = 0; |
| 484 | break; |
| 485 | default: |
| 486 | builtin_usage (); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 487 | return (EX_USAGE); |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 488 | } |
| 489 | } |
| 490 | list = loptend; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 491 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 492 | #define tcwd the_current_working_directory |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 493 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 494 | directory = tcwd ? (verbatim_pwd ? sh_physpath (tcwd, 0) : tcwd) |
| 495 | : get_working_directory ("pwd"); |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 496 | |
| 497 | /* Try again using getcwd() if canonicalization fails (for instance, if |
| 498 | the file system has changed state underneath bash). */ |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 499 | if ((tcwd && directory == 0) || |
| 500 | (posixly_correct && same_file (".", tcwd, (struct stat *)0, (struct stat *)0) == 0)) |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 501 | { |
| 502 | if (directory && directory != tcwd) |
| 503 | free (directory); |
| 504 | directory = resetpwd ("pwd"); |
| 505 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 506 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 507 | #undef tcwd |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 508 | |
| 509 | if (directory) |
| 510 | { |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 511 | opt = EXECUTION_SUCCESS; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 512 | printf ("%s\n", directory); |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 513 | /* This is dumb but posix-mandated. */ |
| 514 | if (posixly_correct && pflag) |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 515 | opt = setpwd (directory); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 516 | if (directory != the_current_working_directory) |
| 517 | free (directory); |
Chet Ramey | 495aee4 | 2011-11-22 19:11:26 -0500 | [diff] [blame] | 518 | return (sh_chkwrite (opt)); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 519 | } |
| 520 | else |
| 521 | return (EXECUTION_FAILURE); |
| 522 | } |
| 523 | |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 524 | /* Do the work of changing to the directory NEWDIR. Handle symbolic |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 525 | link following, etc. This function *must* return with |
| 526 | the_current_working_directory either set to NULL (in which case |
| 527 | getcwd() will eventually be called), or set to a string corresponding |
| 528 | to the working directory. Return 1 on success, 0 on failure. */ |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 529 | |
| 530 | static int |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 531 | change_to_directory (newdir, nolinks, xattr) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 532 | char *newdir; |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 533 | int nolinks, xattr; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 534 | { |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 535 | char *t, *tdir, *ndir; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 536 | int err, canon_failed, r, ndlen, dlen; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 537 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 538 | tdir = (char *)NULL; |
| 539 | |
| 540 | if (the_current_working_directory == 0) |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 541 | { |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 542 | t = get_working_directory ("chdir"); |
| 543 | FREE (t); |
| 544 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 545 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 546 | t = make_absolute (newdir, the_current_working_directory); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 547 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 548 | /* TDIR is either the canonicalized absolute pathname of NEWDIR |
| 549 | (nolinks == 0) or the absolute physical pathname of NEWDIR |
| 550 | (nolinks != 0). */ |
| 551 | tdir = nolinks ? sh_physpath (t, 0) |
| 552 | : sh_canonpath (t, PATH_CHECKDOTDOT|PATH_CHECKEXISTS); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 553 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 554 | ndlen = strlen (newdir); |
| 555 | dlen = strlen (t); |
| 556 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 557 | /* Use the canonicalized version of NEWDIR, or, if canonicalization |
| 558 | failed, use the non-canonical form. */ |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 559 | canon_failed = 0; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 560 | if (tdir && *tdir) |
| 561 | free (t); |
| 562 | else |
| 563 | { |
| 564 | FREE (tdir); |
| 565 | tdir = t; |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 566 | canon_failed = 1; |
| 567 | } |
| 568 | |
| 569 | /* In POSIX mode, if we're resolving symlinks logically and sh_canonpath |
| 570 | returns NULL (because it checks the path, it will return NULL if the |
| 571 | resolved path doesn't exist), fail immediately. */ |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 572 | if (posixly_correct && nolinks == 0 && canon_failed && (errno != ENAMETOOLONG || ndlen > PATH_MAX)) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 573 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 574 | #if defined ENAMETOOLONG |
| 575 | if (errno != ENOENT && errno != ENAMETOOLONG) |
| 576 | #else |
| 577 | if (errno != ENOENT) |
| 578 | #endif |
| 579 | errno = ENOTDIR; |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 580 | free (tdir); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 581 | return (0); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 582 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 583 | |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 584 | #if defined (O_XATTR) |
| 585 | if (xattrflag) |
| 586 | { |
| 587 | r = cdxattr (nolinks ? newdir : tdir, &ndir); |
| 588 | if (r >= 0) |
| 589 | { |
| 590 | canon_failed = 0; |
| 591 | free (tdir); |
| 592 | tdir = ndir; |
| 593 | } |
| 594 | else |
| 595 | { |
| 596 | err = errno; |
| 597 | free (tdir); |
| 598 | errno = err; |
| 599 | return (0); /* no xattr */ |
| 600 | } |
| 601 | } |
| 602 | else |
| 603 | #endif |
| 604 | { |
| 605 | r = chdir (nolinks ? newdir : tdir); |
| 606 | if (r >= 0) |
| 607 | resetxattr (); |
| 608 | } |
| 609 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 610 | /* If the chdir succeeds, update the_current_working_directory. */ |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 611 | if (r == 0) |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 612 | { |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 613 | /* If canonicalization failed, but the chdir succeeded, reset the |
| 614 | shell's idea of the_current_working_directory. */ |
| 615 | if (canon_failed) |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 616 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 617 | t = resetpwd ("cd"); |
| 618 | if (t == 0) |
| 619 | set_working_directory (tdir); |
Chet Ramey | ac50fba | 2014-02-26 09:36:43 -0500 | [diff] [blame] | 620 | else |
| 621 | free (t); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 622 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 623 | else |
| 624 | set_working_directory (tdir); |
Jari Aalto | 7117c2d | 2002-07-17 14:10:11 +0000 | [diff] [blame] | 625 | |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 626 | free (tdir); |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 627 | return (1); |
| 628 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 629 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 630 | /* We failed to change to the appropriate directory name. If we tried |
| 631 | what the user passed (nolinks != 0), punt now. */ |
| 632 | if (nolinks) |
Jari Aalto | 95732b4 | 2005-12-07 14:08:12 +0000 | [diff] [blame] | 633 | { |
| 634 | free (tdir); |
| 635 | return (0); |
| 636 | } |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 637 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 638 | err = errno; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 639 | |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 640 | /* We're not in physical mode (nolinks == 0), but we failed to change to |
| 641 | the canonicalized directory name (TDIR). Try what the user passed |
| 642 | verbatim. If we succeed, reinitialize the_current_working_directory. */ |
| 643 | if (chdir (newdir) == 0) |
| 644 | { |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 645 | t = resetpwd ("cd"); |
| 646 | if (t == 0) |
| 647 | set_working_directory (tdir); |
| 648 | else |
| 649 | free (t); |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 650 | |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 651 | r = 1; |
Jari Aalto | 726f638 | 1996-08-26 18:22:31 +0000 | [diff] [blame] | 652 | } |
| 653 | else |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 654 | { |
| 655 | errno = err; |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 656 | r = 0; |
Jari Aalto | 28ef6c3 | 2001-04-06 19:14:31 +0000 | [diff] [blame] | 657 | } |
Jari Aalto | b80f644 | 2004-07-27 13:29:18 +0000 | [diff] [blame] | 658 | |
| 659 | free (tdir); |
| 660 | return r; |
Jari Aalto | ccc6cda | 1996-12-23 17:02:34 +0000 | [diff] [blame] | 661 | } |