1 /* Copyright (C) 1991, 92, 93, 94, 95, 96 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__ */
83 #if defined (HAVE_UNISTD_H) || defined (__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
170 #if !defined (STDC_HEADERS) && !defined (__GNU_LIBRARY__)
172 #define size_t unsigned int
175 #if !__STDC__ && !defined (const)
179 #ifndef __GNU_LIBRARY__
183 /* Get the canonical absolute name of the named 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. */
190 __canonicalize_directory_name_internal (thisdir, buf, size)
195 static const char dots[]
196 = "../../../../../../../../../../../../../../../../../../../../../../../\
197 ../../../../../../../../../../../../../../../../../../../../../../../../../../\
198 ../../../../../../../../../../../../../../../../../../../../../../../../../..";
199 const char *dotp, *dotlist;
201 dev_t rootdev, thisdev;
202 ino_t rootino, thisino;
204 register char *pathp;
222 path = malloc (size);
230 if (__lstat (thisdir, &st) < 0)
235 if (__lstat ("/", &st) < 0)
240 dotsize = sizeof (dots) - 1;
241 dotp = &dots[sizeof (dots)];
243 while (!(thisdev == rootdev && thisino == rootino))
245 register DIR *dirstream;
246 register struct dirent *d;
251 /* Look at the parent directory. */
254 /* My, what a deep directory tree you have, Grandma. */
258 new = malloc (dotsize * 2 + 1);
261 memcpy (new, dots, dotsize);
265 new = realloc ((__ptr_t) dotlist, dotsize * 2 + 1);
269 memcpy (&new[dotsize], new, dotsize);
270 dotp = &new[dotsize];
278 /* Figure out if this directory is a mount point. */
279 if (__lstat (dotp, &st) < 0)
283 mount_point = dotdev != thisdev;
285 /* Search for the last directory. */
286 dirstream = opendir (dotp);
287 if (dirstream == NULL)
289 while ((d = readdir (dirstream)) != NULL)
291 if (d->d_name[0] == '.' &&
292 (d->d_name[1] == '\0' ||
293 (d->d_name[1] == '.' && d->d_name[2] == '\0')))
295 if (mount_point || d->d_ino == thisino)
297 char name[dotlist + dotsize - dotp + 1 + _D_ALLOC_NAMLEN (d)];
298 memcpy (name, dotp, dotlist + dotsize - dotp);
299 name[dotlist + dotsize - dotp] = '/';
300 strcpy (&name[dotlist + dotsize - dotp + 1], d->d_name);
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 size_t namlen = _D_EXACT_NAMLEN (d);
323 if (pathp - path < namlen)
333 buf = realloc (path, size);
336 (void) closedir (dirstream);
338 errno = ENOMEM; /* closedir might have changed it. */
341 pathp = &buf[pathp - path];
346 (void) memcpy (pathp, d->d_name, namlen);
348 (void) closedir (dirstream);
355 if (pathp == &path[size - 1])
359 free ((__ptr_t) dotlist);
361 memmove (path, pathp, path + size - pathp);
366 free ((__ptr_t) dotlist);
370 /* Get the pathname of the current working directory, and put it in SIZE
371 bytes of BUF. Returns NULL if the directory couldn't be determined or
372 SIZE was too small. If successful, returns BUF. In GNU, if BUF is
373 NULL, an array is allocated with `malloc'; the array is SIZE bytes long,
374 unless SIZE <= 0, in which case it is as big as necessary. */
377 #define __getcwd getcwd
385 return __canonicalize_directory_name_internal (".", buf, size);
389 weak_alias (__getcwd, getcwd)