1 /* Copyright (C) 1991, 1992, 1993, 1994, 1995 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__
184 #define __getcwd getcwd
187 /* Get the pathname of the current working directory, and put it in SIZE
188 bytes of BUF. Returns NULL if the directory couldn't be determined or
189 SIZE was too small. If successful, returns BUF. In GNU, if BUF is
190 NULL, an array is allocated with `malloc'; the array is SIZE bytes long,
191 unless SIZE <= 0, in which case it is as big as necessary. */
198 static const char dots[]
199 = "../../../../../../../../../../../../../../../../../../../../../../../\
200 ../../../../../../../../../../../../../../../../../../../../../../../../../../\
201 ../../../../../../../../../../../../../../../../../../../../../../../../../..";
202 const char *dotp, *dotlist;
204 dev_t rootdev, thisdev;
205 ino_t rootino, thisino;
207 register char *pathp;
225 path = malloc (size);
233 if (__lstat (".", &st) < 0)
238 if (__lstat ("/", &st) < 0)
243 dotsize = sizeof (dots) - 1;
244 dotp = &dots[sizeof (dots)];
246 while (!(thisdev == rootdev && thisino == rootino))
248 register DIR *dirstream;
249 register struct dirent *d;
254 /* Look at the parent directory. */
257 /* My, what a deep directory tree you have, Grandma. */
261 new = malloc (dotsize * 2 + 1);
264 memcpy (new, dots, dotsize);
268 new = realloc ((__ptr_t) dotlist, dotsize * 2 + 1);
272 memcpy (&new[dotsize], new, dotsize);
273 dotp = &new[dotsize];
281 /* Figure out if this directory is a mount point. */
282 if (__lstat (dotp, &st) < 0)
286 mount_point = dotdev != thisdev;
288 /* Search for the last directory. */
289 dirstream = opendir (dotp);
290 if (dirstream == NULL)
292 while ((d = readdir (dirstream)) != NULL)
294 if (d->d_name[0] == '.' &&
295 (d->d_namlen == 1 || (d->d_namlen == 2 && d->d_name[1] == '.')))
297 if (mount_point || d->d_ino == thisino)
299 char *name = __alloca (dotlist + dotsize - dotp +
300 1 + d->d_namlen + 1);
301 memcpy (name, dotp, dotlist + dotsize - dotp);
302 name[dotlist + dotsize - dotp] = '/';
303 memcpy (&name[dotlist + dotsize - dotp + 1],
304 d->d_name, d->d_namlen + 1);
305 if (__lstat (name, &st) < 0)
308 (void) closedir (dirstream);
312 if (st.st_dev == thisdev && st.st_ino == thisino)
319 (void) closedir (dirstream);
325 if (pathp - path < d->d_namlen + 1)
335 buf = realloc (path, size);
338 (void) closedir (dirstream);
340 errno = ENOMEM; /* closedir might have changed it. */
343 pathp = &buf[pathp - path];
347 pathp -= d->d_namlen;
348 (void) memcpy (pathp, d->d_name, d->d_namlen);
350 (void) closedir (dirstream);
357 if (pathp == &path[size - 1])
361 free ((__ptr_t) dotlist);
363 memmove (path, pathp, path + size - pathp);
368 free ((__ptr_t) dotlist);
373 weak_alias (__getcwd, getcwd)