1 /* Copyright (C) 1991, 1992, 1993, 1994 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
16 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
17 Cambridge, MA 02139, 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)
53 #if defined (USGr3) && !defined (DIRENT)
56 #if defined (Xenix) && !defined (SYSNDIR)
60 #if defined (POSIX) || defined (DIRENT) || defined (__GNU_LIBRARY__)
62 #ifndef __GNU_LIBRARY__
63 #define D_NAMLEN(d) strlen((d)->d_name)
66 #define D_NAMLEN(d) ((d)->d_namlen)
68 #else /* not POSIX or DIRENT */
70 #define D_NAMLEN(d) ((d)->d_namlen)
72 #if defined (USG) && !defined (sgi)
75 #else /* Not SYSNDIR */
81 #endif /* POSIX or DIRENT or __GNU_LIBRARY__ */
87 #if (defined (STDC_HEADERS) || defined (__GNU_LIBRARY__) \
92 #else /* No standard headers. */
108 #else /* Not NeXT. */
116 extern void bzero ();
119 extern void bcopy ();
126 extern char *malloc (), *realloc ();
129 #endif /* Standard headers. */
132 #define memcpy(d, s, n) bcopy((s), (d), (n))
133 #define memmove memcpy
134 #endif /* Not ANSI_STRING. */
136 #if !defined(__alloca) && !defined(__GNU_LIBRARY__)
140 #define alloca(n) __builtin_alloca (n)
142 #if defined (sparc) || defined (HAVE_ALLOCA_H)
144 #else /* Not sparc or HAVE_ALLOCA_H. */
146 extern char *alloca ();
147 #endif /* Not _AIX. */
148 #endif /* sparc or HAVE_ALLOCA_H. */
151 #define __alloca alloca
155 #if (defined (HAVE_LIMITS_H) || defined (STDC_HEADERS) || \
156 defined (__GNU_LIBRARY__))
159 #include <sys/param.h>
164 #define PATH_MAX MAXPATHLEN
166 #define PATH_MAX 1024
172 #define size_t unsigned int
175 #if !__STDC__ && !defined (const)
179 #ifndef __GNU_LIBRARY__
183 /* Get the pathname of the current working directory, and put it in SIZE
184 bytes of BUF. Returns NULL if the directory couldn't be determined or
185 SIZE was too small. If successful, returns BUF. In GNU, if BUF is
186 NULL, an array is allocated with `malloc'; the array is SIZE bytes long,
187 unless SIZE <= 0, in which case it is as big as necessary. */
194 static const char dots
[]
195 = "../../../../../../../../../../../../../../../../../../../../../../../\
196 ../../../../../../../../../../../../../../../../../../../../../../../../../../\
197 ../../../../../../../../../../../../../../../../../../../../../../../../../..";
198 const char *dotp
, *dotlist
;
200 dev_t rootdev
, thisdev
;
201 ino_t rootino
, thisino
;
203 register char *pathp
;
221 path
= malloc (size
);
229 if (__lstat (".", &st
) < 0)
234 if (__lstat ("/", &st
) < 0)
239 dotsize
= sizeof (dots
) - 1;
240 dotp
= &dots
[sizeof (dots
)];
242 while (!(thisdev
== rootdev
&& thisino
== rootino
))
244 register DIR *dirstream
;
245 register struct dirent
*d
;
250 /* Look at the parent directory. */
253 /* My, what a deep directory tree you have, Grandma. */
257 new = malloc (dotsize
* 2 + 1);
260 memcpy (new, dots
, dotsize
);
264 new = realloc ((__ptr_t
) dotlist
, dotsize
* 2 + 1);
268 memcpy (&new[dotsize
], new, dotsize
);
269 dotp
= &new[dotsize
];
277 /* Figure out if this directory is a mount point. */
278 if (__lstat (dotp
, &st
) < 0)
282 mount_point
= dotdev
!= thisdev
;
284 /* Search for the last directory. */
285 dirstream
= opendir (dotp
);
286 if (dirstream
== NULL
)
288 while ((d
= readdir (dirstream
)) != NULL
)
290 if (d
->d_name
[0] == '.' &&
291 (d
->d_namlen
== 1 || (d
->d_namlen
== 2 && d
->d_name
[1] == '.')))
293 if (mount_point
|| d
->d_ino
== thisino
)
295 char *name
= __alloca (dotlist
+ dotsize
- dotp
+
296 1 + d
->d_namlen
+ 1);
297 memcpy (name
, dotp
, dotlist
+ dotsize
- dotp
);
298 name
[dotlist
+ dotsize
- dotp
] = '/';
299 memcpy (&name
[dotlist
+ dotsize
- dotp
+ 1],
300 d
->d_name
, d
->d_namlen
+ 1);
301 if (__lstat (name
, &st
) < 0)
304 (void) closedir (dirstream
);
308 if (st
.st_dev
== thisdev
&& st
.st_ino
== thisino
)
315 (void) closedir (dirstream
);
321 if (pathp
- path
< d
->d_namlen
+ 1)
331 buf
= realloc (path
, size
);
334 (void) closedir (dirstream
);
336 errno
= ENOMEM
; /* closedir might have changed it. */
339 pathp
= &buf
[pathp
- path
];
343 pathp
-= d
->d_namlen
;
344 (void) memcpy (pathp
, d
->d_name
, d
->d_namlen
);
346 (void) closedir (dirstream
);
353 if (pathp
== &path
[size
- 1])
357 free ((__ptr_t
) dotlist
);
359 memmove (path
, pathp
, path
+ size
- pathp
);
364 free ((__ptr_t
) dotlist
);