1 /* Copyright (C) 1991, 92, 93, 94, 95, 96, 97 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)
69 #define D_NAMLEN(d) ((d)->d_namlen)
71 #else /* not POSIX or DIRENT */
73 #define D_NAMLEN(d) ((d)->d_namlen)
75 #if defined (USG) && !defined (sgi)
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__) \
95 #else /* No standard headers. */
111 #else /* Not NeXT. */
119 extern void bzero ();
122 extern void bcopy ();
129 extern char *malloc (), *realloc ();
132 #endif /* Standard headers. */
135 #define memcpy(d, s, n) bcopy((s), (d), (n))
136 #define memmove memcpy
137 #endif /* Not ANSI_STRING. */
139 #if !defined(__alloca) && !defined(__GNU_LIBRARY__)
143 #define alloca(n) __builtin_alloca (n)
145 #if defined (sparc) || defined (HAVE_ALLOCA_H)
147 #else /* Not sparc or HAVE_ALLOCA_H. */
149 extern char *alloca ();
150 #endif /* Not _AIX. */
151 #endif /* sparc or HAVE_ALLOCA_H. */
154 #define __alloca alloca
158 #if (defined (HAVE_LIMITS_H) || defined (STDC_HEADERS) || \
159 defined (__GNU_LIBRARY__))
162 #include <sys/param.h>
167 #define PATH_MAX MAXPATHLEN
169 #define PATH_MAX 1024
173 #if !defined (STDC_HEADERS) && !defined (__GNU_LIBRARY__)
175 #define size_t unsigned int
178 #if !__STDC__ && !defined (const)
182 #ifndef __GNU_LIBRARY__
187 #define __getcwd getcwd
190 /* Get the pathname of the current working directory, and put it in SIZE
191 bytes of BUF. Returns NULL if the directory couldn't be determined or
192 SIZE was too small. If successful, returns BUF. In GNU, if BUF is
193 NULL, an array is allocated with `malloc'; the array is SIZE bytes long,
194 unless SIZE <= 0, in which case it is as big as necessary. */
201 static const char dots
[]
202 = "../../../../../../../../../../../../../../../../../../../../../../../\
203 ../../../../../../../../../../../../../../../../../../../../../../../../../../\
204 ../../../../../../../../../../../../../../../../../../../../../../../../../..";
205 const char *dotp
, *dotlist
;
207 dev_t rootdev
, thisdev
;
208 ino_t rootino
, thisino
;
210 register char *pathp
;
217 __set_errno (EINVAL
);
228 path
= malloc (size
);
236 if (__lstat (".", &st
) < 0)
241 if (__lstat ("/", &st
) < 0)
246 dotsize
= sizeof (dots
) - 1;
247 dotp
= &dots
[sizeof (dots
)];
249 while (!(thisdev
== rootdev
&& thisino
== rootino
))
251 register DIR *dirstream
;
257 /* Look at the parent directory. */
260 /* My, what a deep directory tree you have, Grandma. */
264 new = malloc (dotsize
* 2 + 1);
267 memcpy (new, dots
, dotsize
);
271 new = realloc ((__ptr_t
) dotlist
, dotsize
* 2 + 1);
275 memcpy (&new[dotsize
], new, dotsize
);
276 dotp
= &new[dotsize
];
284 /* Figure out if this directory is a mount point. */
285 if (__lstat (dotp
, &st
) < 0)
289 mount_point
= dotdev
!= thisdev
;
291 /* Search for the last directory. */
292 dirstream
= __opendir (dotp
);
293 if (dirstream
== NULL
)
295 while ((d
= __readdir (dirstream
)) != NULL
)
297 if (d
->d_name
[0] == '.' &&
298 (d
->d_name
[1] == '\0' ||
299 (d
->d_name
[1] == '.' && d
->d_name
[2] == '\0')))
301 if (mount_point
|| (ino_t
) d
->d_ino
== thisino
)
303 char name
[dotlist
+ dotsize
- dotp
+ 1 + _D_ALLOC_NAMLEN (d
)];
304 memcpy (name
, dotp
, dotlist
+ dotsize
- dotp
);
305 name
[dotlist
+ dotsize
- dotp
] = '/';
306 strcpy (&name
[dotlist
+ dotsize
- dotp
+ 1], d
->d_name
);
307 if (__lstat (name
, &st
) < 0)
310 (void) __closedir (dirstream
);
314 if (st
.st_dev
== thisdev
&& st
.st_ino
== thisino
)
321 (void) __closedir (dirstream
);
327 size_t namlen
= _D_EXACT_NAMLEN (d
);
329 if ((size_t) (pathp
- path
) < namlen
)
333 (void) __closedir (dirstream
);
334 __set_errno (ERANGE
);
340 buf
= realloc (path
, size
);
343 (void) __closedir (dirstream
);
345 __set_errno (ENOMEM
);/* closedir might have changed it.*/
348 pathp
= &buf
[pathp
- path
+ size
/ 2];
350 /* Move current contents up to the end of the buffer.
351 This is guaranteed to be non-overlapping. */
352 memcpy (pathp
, pathp
- size
/ 2, path
+ size
- pathp
);
356 (void) memcpy (pathp
, d
->d_name
, namlen
);
358 (void) __closedir (dirstream
);
365 if (pathp
== &path
[size
- 1])
369 free ((__ptr_t
) dotlist
);
371 memmove (path
, pathp
, path
+ size
- pathp
);
376 free ((__ptr_t
) dotlist
);
381 weak_alias (__getcwd
, getcwd
)