1 /* getcwd.c -- stolen from the GNU C library and modified to work with bash. */
3 /* Copyright (C) 1991 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If
18 not, write to the Free Software Foundation, Inc.,
19 59 Temple Place, Suite 330, Boston, MA 02111 USA. */
23 #if !defined (HAVE_GETCWD)
25 #if !defined (__GNUC__) && !defined (HAVE_ALLOCA_H) && defined (_AIX)
27 #endif /* _AIX && RISC6000 && !__GNUC__ */
29 #include <bashtypes.h>
32 #if defined (HAVE_LIMITS_H)
36 #if defined (HAVE_UNISTD_H)
41 #include <posixstat.h>
53 #if !defined (HAVE_LSTAT)
61 /* Get the pathname of the current working directory,
62 and put it in SIZE bytes of BUF. Returns NULL if the
63 directory couldn't be determined or SIZE was too small.
64 If successful, returns BUF. In GNU, if BUF is NULL,
65 an array is allocated with `malloc'; the array is SIZE
66 bytes long, unless SIZE <= 0, in which case it is as
68 #if defined (__STDC__)
70 getcwd (char *buf, size_t size)
76 #endif /* !__STDC__ */
78 static const char dots[]
79 = "../../../../../../../../../../../../../../../../../../../../../../../\
80 ../../../../../../../../../../../../../../../../../../../../../../../../../../\
81 ../../../../../../../../../../../../../../../../../../../../../../../../../..";
82 const char *dotp, *dotlist;
84 dev_t rootdev, thisdev;
85 ino_t rootino, thisino;
86 char path[PATH_MAX + 1];
93 if (buf != NULL && size == 0)
96 return ((char *)NULL);
99 pathsize = sizeof (path);
100 pathp = &path[pathsize];
104 if (stat (".", &st) < 0)
105 return ((char *)NULL);
109 if (stat ("/", &st) < 0)
110 return ((char *)NULL);
116 dotsize = sizeof (dots) - 1;
117 dotp = &dots[sizeof (dots)];
119 while (!(thisdev == rootdev && thisino == rootino))
121 register DIR *dirstream;
122 register struct dirent *d;
128 /* Look at the parent directory. */
131 /* My, what a deep directory tree you have, Grandma. */
135 new = (char *)malloc (dotsize * 2 + 1);
138 memcpy (new, dots, dotsize);
142 new = (char *)realloc ((PTR_T) dotlist, dotsize * 2 + 1);
146 memcpy (&new[dotsize], new, dotsize);
147 dotp = &new[dotsize];
155 /* Figure out if this directory is a mount point. */
156 if (stat (dotp, &st) < 0)
160 mount_point = dotdev != thisdev;
162 /* Search for the last directory. */
163 dirstream = opendir (dotp);
164 if (dirstream == NULL)
166 while ((d = readdir (dirstream)) != NULL)
168 if (d->d_name[0] == '.' &&
169 (d->d_name[1] == '\0' ||
170 (d->d_name[1] == '.' && d->d_name[2] == '\0')))
172 if (mount_point || d->d_fileno == thisino)
176 namlen = D_NAMLEN(d);
178 alloca (dotlist + dotsize - dotp + 1 + namlen + 1);
179 memcpy (name, dotp, dotlist + dotsize - dotp);
180 name[dotlist + dotsize - dotp] = '/';
181 memcpy (&name[dotlist + dotsize - dotp + 1],
182 d->d_name, namlen + 1);
183 if (lstat (name, &st) < 0)
187 (void) closedir (dirstream);
194 if (st.st_dev == thisdev && st.st_ino == thisino)
203 int save = errno ? errno : saved_errno;
205 (void) closedir (dirstream);
213 while ((space = pathp - pathbuf) <= namlen)
219 new = (char *)malloc (pathsize * 2);
225 new = (char *)realloc ((PTR_T) pathbuf, (pathsize * 2));
230 (void) memcpy (new + pathsize + space, pathp, pathsize - space);
231 pathp = new + pathsize + space;
237 (void) memcpy (pathp, d->d_name, namlen);
239 (void) closedir (dirstream);
246 if (pathp == &path[sizeof(path) - 1])
250 free ((PTR_T) dotlist);
253 size_t len = pathbuf + pathsize - pathp;
256 if (len < (size_t) size)
258 buf = (char *) malloc (len);
262 else if ((size_t) size < len)
267 (void) memcpy((PTR_T) buf, (PTR_T) pathp, len);
276 if ((dotlist != dots) && dotlist)
279 free ((PTR_T) dotlist);
284 if ((pathbuf != path) && pathbuf)
287 free ((PTR_T) pathbuf);
290 return ((char *)NULL);
301 if (getcwd(b, sizeof(b)))
308 perror ("cwd: getcwd");
313 #endif /* !HAVE_GETCWD */