commit bash-20051123 snapshot
[platform/upstream/bash.git] / bash-getcwd-patch
1 *** ../bash-2.05b-patched/builtins/common.c     Fri Jun 28 12:24:31 2002
2 --- builtins/common.c   Thu Sep 30 22:25:20 2004
3 ***************
4 *** 455,464 ****
5   {
6     char *directory;
7   
8     if (no_symbolic_links)
9       {
10 !       if (the_current_working_directory)
11 !       free (the_current_working_directory);
12
13         the_current_working_directory = (char *)NULL;
14       }
15 --- 469,477 ----
16   {
17     char *directory;
18 +   size_t dsize;
19   
20     if (no_symbolic_links)
21       {
22 !       FREE (the_current_working_directory);
23         the_current_working_directory = (char *)NULL;
24       }
25 ***************
26 *** 466,480 ****
27     if (the_current_working_directory == 0)
28       {
29 !       the_current_working_directory = (char *)xmalloc (PATH_MAX);
30 !       the_current_working_directory[0] = '\0';
31 !       directory = getcwd (the_current_working_directory, PATH_MAX);
32 !       if (directory == 0)
33         {
34 !         fprintf (stderr, "%s: could not get current directory: %s: %s\n",
35                    (for_whom && *for_whom) ? for_whom : get_name_for_error (),
36 !                  bash_getcwd_errstr, strerror (errno));
37
38 !         free (the_current_working_directory);
39 !         the_current_working_directory = (char *)NULL;
40           return (char *)NULL;
41         }
42 --- 479,488 ----
43     if (the_current_working_directory == 0)
44       {
45 !       the_current_working_directory = getcwd (0, 0);
46 !       if (the_current_working_directory == 0)
47         {
48 !         fprintf (stderr, _("%s: error retrieving current directory: %s: %s\n"),
49                    (for_whom && *for_whom) ? for_whom : get_name_for_error (),
50 !                  _(bash_getcwd_errstr), strerror (errno));
51           return (char *)NULL;
52         }
53 *** ../bash-2.05b-patched/builtins/cd.def       Mon Jul 15 14:51:39 2002
54 --- builtins/cd.def     Sun Nov  7 15:13:42 2004
55 ***************
56 *** 122,126 ****
57      the_current_working_directory () */
58   static char *
59 ! resetpwd ()
60   {
61     char *tdir;
62 --- 124,129 ----
63      the_current_working_directory () */
64   static char *
65 ! resetpwd (caller)
66 !      char *caller;
67   {
68     char *tdir;
69 ***************
70 *** 128,132 ****
71     FREE (the_current_working_directory);
72     the_current_working_directory = (char *)NULL;
73 !   tdir = get_working_directory ("cd");
74     return (tdir);
75   }
76 --- 131,135 ----
77     FREE (the_current_working_directory);
78     the_current_working_directory = (char *)NULL;
79 !   tdir = get_working_directory (caller);
80     return (tdir);
81   }
82 ***************
83 *** 333,336 ****
84 --- 340,349 ----
85     directory = tcwd ? (verbatim_pwd ? sh_physpath (tcwd, 0) : tcwd)
86                    : get_working_directory ("pwd");
87
88 +   /* Try again using getcwd() if canonicalization fails (for instance, if
89 +      the file system has changed state underneath bash). */
90 +   if (tcwd && directory == 0)
91 +     directory = resetpwd ("pwd");
92
93   #undef tcwd
94   
95 ***************
96 *** 364,368 ****
97   {
98     char *t, *tdir;
99 !   int err, canon_failed;
100   
101     tdir = (char *)NULL;
102 --- 379,383 ----
103   {
104     char *t, *tdir;
105 !   int err, canon_failed, r;
106   
107     tdir = (char *)NULL;
108 ***************
109 *** 399,403 ****
110     if (posixly_correct && nolinks == 0 && canon_failed)
111       {
112 !       errno = ENOENT;
113         return (0);
114       }
115 --- 414,423 ----
116     if (posixly_correct && nolinks == 0 && canon_failed)
117       {
118 ! #if defined ENAMETOOLONG
119 !       if (errno != ENOENT && errno != ENAMETOOLONG)
120 ! #else
121 !       if (errno != ENOENT)
122 ! #endif
123 !       errno = ENOTDIR;
124         return (0);
125       }
126 ***************
127 *** 409,418 ****
128          shell's idea of the_current_working_directory. */
129         if (canon_failed)
130 -       resetpwd ();
131 -       else
132         {
133 !         FREE (the_current_working_directory);
134 !         the_current_working_directory = tdir;
135         }
136   
137         return (1);
138 --- 429,439 ----
139          shell's idea of the_current_working_directory. */
140         if (canon_failed)
141         {
142 !         t = resetpwd ("cd");
143 !         if (t == 0)
144 !           set_working_directory (tdir);
145         }
146 +       else
147 +       set_working_directory (tdir);
148   
149         return (1);
150 ***************
151 *** 425,429 ****
152   
153     err = errno;
154 -   free (tdir);
155   
156     /* We're not in physical mode (nolinks == 0), but we failed to change to
157 --- 446,449 ----
158 ***************
159 *** 432,445 ****
160     if (chdir (newdir) == 0)
161       {
162 !       tdir = resetpwd ();
163 !       FREE (tdir);
164   
165 !       return (1);
166       }
167     else
168       {
169         errno = err;
170 !       return (0);
171       }
172   }
173   
174 --- 452,471 ----
175     if (chdir (newdir) == 0)
176       {
177 !       t = resetpwd ("cd");
178 !       if (t == 0)
179 !       set_working_directory (tdir);
180 !       else
181 !       free (t);
182   
183 !       r = 1;
184       }
185     else
186       {
187         errno = err;
188 !       r = 0;
189       }
190
191 +   free (tdir);
192 +   return r;
193   }
194
195
196
197 *** ../bash-2.05b-patched/aclocal.m4    Tue Jun 25 09:45:43 2002
198 --- aclocal.m4  Sat Oct  9 15:03:28 2004
199 ***************
200 *** 686,691 ****
201   
202   AC_DEFUN(BASH_FUNC_GETCWD,
203 ! [AC_MSG_CHECKING([if getcwd() calls popen()])
204 ! AC_CACHE_VAL(bash_cv_getcwd_calls_popen,
205   [AC_TRY_RUN([
206   #include <stdio.h>
207 --- 686,691 ----
208   
209   AC_DEFUN(BASH_FUNC_GETCWD,
210 ! [AC_MSG_CHECKING([if getcwd() will dynamically allocate memory])
211 ! AC_CACHE_VAL(bash_cv_getcwd_malloc,
212   [AC_TRY_RUN([
213   #include <stdio.h>
214 ***************
215 *** 694,748 ****
216   #endif
217   
218 - #ifndef __STDC__
219 - #ifndef const
220 - #define const
221 - #endif
222 - #endif
223
224 - int popen_called;
225
226 - FILE *
227 - popen(command, type)
228 -      const char *command;
229 -      const char *type;
230 - {
231 -       popen_called = 1;
232 -       return (FILE *)NULL;
233 - }
234
235 - FILE *_popen(command, type)
236 -      const char *command;
237 -      const char *type;
238 - {
239 -   return (popen (command, type));
240 - }
241
242 - int
243 - pclose(stream)
244 - FILE *stream;
245 - {
246 -       return 0;
247 - }
248
249 - int
250 - _pclose(stream)
251 - FILE *stream;
252 - {
253 -       return 0;
254 - }
255
256   main()
257   {
258 !       char    lbuf[32];
259 !       popen_called = 0;
260 !       getcwd(lbuf, 32);
261 !       exit (popen_called);
262   }
263 ! ], bash_cv_getcwd_calls_popen=no, bash_cv_getcwd_calls_popen=yes,
264 !    [AC_MSG_WARN(cannot check whether getcwd calls popen if cross compiling -- defaulting to no)
265 !     bash_cv_getcwd_calls_popen=no]
266   )])
267 ! AC_MSG_RESULT($bash_cv_getcwd_calls_popen)
268 ! if test $bash_cv_getcwd_calls_popen = yes; then
269   AC_DEFINE(GETCWD_BROKEN)
270   AC_LIBOBJ(getcwd)
271 --- 694,709 ----
272   #endif
273   
274   main()
275   {
276 !       char    *xpwd;
277 !       xpwd = getcwd(0, 0);
278 !       exit (xpwd == 0);
279   }
280 ! ], bash_cv_getcwd_malloc=yes, bash_cv_getcwd_malloc=no,
281 !    [AC_MSG_WARN(cannot check whether getcwd allocates memory when cross-compiling -- defaulting to no)
282 !     bash_cv_getcwd_malloc=no]
283   )])
284 ! AC_MSG_RESULT($bash_cv_getcwd_malloc)
285 ! if test $bash_cv_getcwd_malloc = no; then
286   AC_DEFINE(GETCWD_BROKEN)
287   AC_LIBOBJ(getcwd)