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;
80 if (stat (".", &st) < 0)
85 if (stat ("/", &st) < 0)
90 dotsize = sizeof (dots) - 1;
91 dotp = &dots[sizeof (dots)];
93 while (!(thisdev == rootdev && thisino == rootino))
95 register DIR *dirstream;
96 register struct dirent *d;
101 /* Look at the parent directory. */
104 /* My, what a deep directory tree you have, Grandma. */
108 new = malloc (dotsize * 2 + 1);
111 memcpy (new, dots, dotsize);
115 new = realloc ((PTR) dotlist, dotsize * 2 + 1);
119 memcpy (&new[dotsize], new, dotsize);
120 dotp = &new[dotsize];
128 /* Figure out if this directory is a mount point. */
129 if (stat (dotp, &st) < 0)
133 mount_point = dotdev != thisdev;
135 /* Search for the last directory. */
136 dirstream = opendir (dotp);
137 if (dirstream == NULL)
139 while ((d = readdir (dirstream)) != NULL)
141 if (d->d_name[0] == '.' &&
142 (d->d_namlen == 1 || (d->d_namlen == 2 && d->d_name[1] == '.')))
144 if (mount_point || d->d_fileno == thisino)
146 char *name = __alloca (dotlist + dotsize - dotp +
147 1 + d->d_namlen + 1);
148 memcpy (name, dotp, dotlist + dotsize - dotp);
149 name[dotlist + dotsize - dotp] = '/';
150 memcpy (&name[dotlist + dotsize - dotp + 1],
151 d->d_name, d->d_namlen + 1);
152 if (stat (name, &st) < 0)
155 (void) closedir (dirstream);
159 if (st.st_dev == thisdev && st.st_ino == thisino)
166 (void) closedir (dirstream);
172 if (pathp - path < d->d_namlen + 1)
182 buf = realloc (path, size);
185 (void) closedir (dirstream);
187 errno = ENOMEM; /* closedir might have changed it. */
190 pathp = &buf[pathp - path];
194 pathp -= d->d_namlen;
195 (void) closedir (dirstream);
196 (void) memcpy (pathp, d->d_name, d->d_namlen);
198 (void) closedir (dirstream);
205 if (pathp == &path[size - 1])
209 free ((PTR) dotlist);
211 return memmove (path, pathp, path + size - pathp);
215 free ((PTR) dotlist);