1 /* Copyright (C) 1991, 1992 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. */
25 #include <sys/types.h>
31 /* Get the pathname of the current working directory,
32 and put it in SIZE bytes of BUF. Returns NULL if the
33 directory couldn't be determined or SIZE was too small.
34 If successful, returns BUF. In GNU, if BUF is NULL,
35 an array is allocated with `malloc'; the array is SIZE
36 bytes long, unless SIZE <= 0, in which case it is as
39 DEFUN(getcwd, (buf, size), char *buf AND size_t size)
41 static CONST char dots[]
42 = "../../../../../../../../../../../../../../../../../../../../../../../\
43 ../../../../../../../../../../../../../../../../../../../../../../../../../../\
44 ../../../../../../../../../../../../../../../../../../../../../../../../../..";
45 CONST char *dotp, *dotlist;
47 dev_t rootdev, thisdev;
48 ino_t rootino, thisino;
79 if (stat (".", &st) < 0)
84 if (stat ("/", &st) < 0)
89 dotsize = sizeof (dots) - 1;
90 dotp = &dots[sizeof (dots)];
92 while (!(thisdev == rootdev && thisino == rootino))
94 register DIR *dirstream;
95 register struct dirent *d;
100 /* Look at the parent directory. */
103 /* My, what a deep directory tree you have, Grandma. */
107 new = malloc (dotsize * 2 + 1);
110 memcpy (new, dots, dotsize);
114 new = realloc ((PTR) dotlist, dotsize * 2 + 1);
118 memcpy (&new[dotsize], new, dotsize);
119 dotp = &new[dotsize];
127 /* Figure out if this directory is a mount point. */
128 if (stat (dotp, &st) < 0)
132 mount_point = dotdev != thisdev;
134 /* Search for the last directory. */
135 dirstream = opendir (dotp);
136 if (dirstream == NULL)
138 while ((d = readdir (dirstream)) != NULL)
140 if (d->d_name[0] == '.' &&
141 (d->d_namlen == 1 || (d->d_namlen == 2 && d->d_name[1] == '.')))
143 if (mount_point || d->d_fileno == thisino)
145 char *name = __alloca (dotlist + dotsize - dotp +
146 1 + d->d_namlen + 1);
147 memcpy (name, dotp, dotlist + dotsize - dotp);
148 name[dotlist + dotsize - dotp] = '/';
149 memcpy (&name[dotlist + dotsize - dotp + 1],
150 d->d_name, d->d_namlen + 1);
151 if (stat (name, &st) < 0)
154 (void) closedir (dirstream);
158 if (st.st_dev == thisdev && st.st_ino == thisino)
165 (void) closedir (dirstream);
171 if (pathp - path < d->d_namlen + 1)
181 buf = realloc (path, size);
184 (void) closedir (dirstream);
186 errno = ENOMEM; /* closedir might have changed it. */
189 pathp = &buf[pathp - path];
193 pathp -= d->d_namlen;
194 (void) closedir (dirstream);
195 (void) memcpy (pathp, d->d_name, d->d_namlen);
197 (void) closedir (dirstream);
204 if (pathp == &path[size - 1])
208 free ((PTR) dotlist);
210 return memmove (path, pathp, path + size - pathp);
214 free ((PTR) dotlist);