1 /* Copyright (C) 1991,92,93,94,95,96,97,98 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
4 The GNU C Library is free software; you can redistribute it and/or
5 modify it under the terms of the GNU Library General Public License as
6 published by the Free Software Foundation; either version 2 of the
7 License, or (at your option) any later version.
9 The GNU C Library is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 Library General Public License for more details.
14 You should have received a copy of the GNU Library General Public
15 License along with the GNU C Library; see the file COPYING.LIB. If not,
16 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 Boston, MA 02111-1307, USA. */
28 /* AIX requires this to be the first thing in the file. */
29 #if defined _AIX && !defined __GNUC__
38 #include <sys/types.h>
45 #if !defined __GNU_LIBRARY__ && !defined STDC_HEADERS
49 # define __set_errno(val) errno = (val)
56 #if defined USGr3 && !defined DIRENT
59 #if defined Xenix && !defined SYSNDIR
63 #if defined POSIX || defined DIRENT || defined __GNU_LIBRARY__
65 # ifndef __GNU_LIBRARY__
66 # define D_NAMLEN(d) strlen((d)->d_name)
68 # define HAVE_D_NAMLEN
69 # define D_NAMLEN(d) ((d)->d_namlen)
71 #else /* not POSIX or DIRENT */
72 # define dirent direct
73 # define D_NAMLEN(d) ((d)->d_namlen)
74 # define HAVE_D_NAMLEN
75 # if defined USG && !defined sgi
77 # include <sys/ndir.h>
78 # else /* Not SYSNDIR */
84 #endif /* POSIX or DIRENT or __GNU_LIBRARY__ */
86 #if defined HAVE_UNISTD_H || defined __GNU_LIBRARY__
90 #if defined STDC_HEADERS || defined __GNU_LIBRARY__ || defined POSIX
94 #else /* No standard headers. */
104 # else /* Not USG. */
110 # else /* Not NeXT. */
112 # include <strings.h>
118 extern void bzero ();
121 extern void bcopy ();
128 extern char *malloc (), *realloc ();
131 #endif /* Standard headers. */
134 # define memcpy(d, s, n) bcopy((s), (d), (n))
135 # define memmove memcpy
136 #endif /* Not ANSI_STRING. */
140 # define mempcpy __mempcpy
142 # define HAVE_MEMPCPY 1
145 #if !defined __alloca && !defined __GNU_LIBRARY__
149 # define alloca(n) __builtin_alloca (n)
150 # else /* Not GCC. */
151 # if defined sparc || defined HAVE_ALLOCA_H
153 # else /* Not sparc or HAVE_ALLOCA_H. */
155 extern char *alloca ();
156 # endif /* Not _AIX. */
157 # endif /* sparc or HAVE_ALLOCA_H. */
160 # define __alloca alloca
164 #if defined HAVE_LIMITS_H || defined STDC_HEADERS || defined __GNU_LIBRARY__
167 # include <sys/param.h>
172 # define PATH_MAX MAXPATHLEN
174 # define PATH_MAX 1024
178 #if !defined STDC_HEADERS && !defined __GNU_LIBRARY__
180 # define size_t unsigned int
183 #if !__STDC__ && !defined const
187 #ifndef __GNU_LIBRARY__
188 # define __lstat stat
192 # define __getcwd getcwd
195 #ifndef GETCWD_RETURN_TYPE
196 # define GETCWD_RETURN_TYPE char *
199 /* Get the pathname of the current working directory, and put it in SIZE
200 bytes of BUF. Returns NULL if the directory couldn't be determined or
201 SIZE was too small. If successful, returns BUF. In GNU, if BUF is
202 NULL, an array is allocated with `malloc'; the array is SIZE bytes long,
203 unless SIZE <= 0, in which case it is as big as necessary. */
210 static const char dots
[]
211 = "../../../../../../../../../../../../../../../../../../../../../../../\
212 ../../../../../../../../../../../../../../../../../../../../../../../../../../\
213 ../../../../../../../../../../../../../../../../../../../../../../../../../..";
214 const char *dotp
, *dotlist
;
216 dev_t rootdev
, thisdev
;
217 ino_t rootino
, thisino
;
219 register char *pathp
;
221 int prev_errno
= errno
;
227 __set_errno (EINVAL
);
238 path
= malloc (size
);
246 if (__lstat (".", &st
) < 0)
251 if (__lstat ("/", &st
) < 0)
256 dotsize
= sizeof (dots
) - 1;
257 dotp
= &dots
[sizeof (dots
)];
259 while (!(thisdev
== rootdev
&& thisino
== rootino
))
261 register DIR *dirstream
;
267 /* Look at the parent directory. */
270 /* My, what a deep directory tree you have, Grandma. */
274 new = malloc (dotsize
* 2 + 1);
278 dotp
= mempcpy (new, dots
, dotsize
);
280 memcpy (new, dots
, dotsize
);
281 dotp
= &new[dotsize
];
286 new = realloc ((__ptr_t
) dotlist
, dotsize
* 2 + 1);
289 dotp
= &new[dotsize
];
292 *((char *) mempcpy ((char *) dotp
, new, dotsize
)) = '\0';
295 memcpy ((char *) dotp
, new, dotsize
);
304 /* Figure out if this directory is a mount point. */
305 if (__lstat (dotp
, &st
) < 0)
309 mount_point
= dotdev
!= thisdev
;
311 /* Search for the last directory. */
312 dirstream
= __opendir (dotp
);
313 if (dirstream
== NULL
)
315 /* Clear errno to distinguish EOF from error if readdir returns
318 while ((d
= __readdir (dirstream
)) != NULL
)
320 if (d
->d_name
[0] == '.' &&
321 (d
->d_name
[1] == '\0' ||
322 (d
->d_name
[1] == '.' && d
->d_name
[2] == '\0')))
324 if (mount_point
|| (ino_t
) d
->d_ino
== thisino
)
326 char name
[dotlist
+ dotsize
- dotp
+ 1 + _D_ALLOC_NAMLEN (d
)];
328 char *tmp
= mempcpy (name
, dotp
, dotlist
+ dotsize
- dotp
);
330 strcpy (tmp
, d
->d_name
);
332 memcpy (name
, dotp
, dotlist
+ dotsize
- dotp
);
333 name
[dotlist
+ dotsize
- dotp
] = '/';
334 strcpy (&name
[dotlist
+ dotsize
- dotp
+ 1], d
->d_name
);
336 /* We don't fail here if we cannot stat() a directory entry.
337 This can happen when (network) filesystems fail. If this
338 entry is in fact the one we are looking for we will find
339 out soon as we reach the end of the directory without
340 having found anything. */
341 if (__lstat (name
, &st
) >= 0
342 && st
.st_dev
== thisdev
&& st
.st_ino
== thisino
)
349 (void) __closedir (dirstream
);
351 /* EOF on dirstream, which means that the current directory
359 size_t namlen
= _D_EXACT_NAMLEN (d
);
361 if ((size_t) (pathp
- path
) <= namlen
)
365 (void) __closedir (dirstream
);
366 __set_errno (ERANGE
);
374 tmp
= realloc (path
, size
);
377 (void) __closedir (dirstream
);
379 __set_errno (ENOMEM
);/* closedir might have changed it.*/
382 pathp
= &tmp
[pathp
- path
+ size
/ 2];
384 /* Move current contents up to the end of the buffer.
385 This is guaranteed to be non-overlapping. */
386 memcpy (pathp
, pathp
- size
/ 2, path
+ size
- pathp
);
390 (void) memcpy (pathp
, d
->d_name
, namlen
);
392 (void) __closedir (dirstream
);
399 if (pathp
== &path
[size
- 1])
403 free ((__ptr_t
) dotlist
);
405 memmove (path
, pathp
, path
+ size
- pathp
);
407 /* Restore errno on successful return. */
408 __set_errno (prev_errno
);
414 free ((__ptr_t
) dotlist
);
418 #if defined _LIBC && !defined __getcwd
419 weak_alias (__getcwd
, getcwd
)