]>
git.ipfire.org Git - thirdparty/bash.git/blob - getcwd.c
1 /* getcwd.c -- stolen from the GNU C library and modified to work with bash. */
3 /* Copyright (C) 1991 Free Software Foundation, Inc.
4 This file is part of the GNU C Library.
6 The GNU C Library is free software; you can redistribute it and/or
7 modify it under the terms of the GNU Library General Public License as
8 published by the Free Software Foundation; either version 2 of the
9 License, or (at your option) any later version.
11 The GNU C Library is distributed in the hope that it will be useful,
12 but WITHOUT ANY WARRANTY; without even the implied warranty of
13 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 Library General Public License for more details.
16 You should have received a copy of the GNU Library General Public
17 License along with the GNU C Library; see the file COPYING.LIB. If
18 not, write to the Free Software Foundation, Inc., 675 Mass Ave,
19 Cambridge, MA 02139, USA. */
23 #if !defined (HAVE_GETCWD)
25 #include "bashtypes.h"
28 #if defined (HAVE_LIMITS_H)
32 #if defined (HAVE_UNISTD_H)
37 #include "posixstat.h"
41 #if defined (HAVE_STDLIB_H)
44 # include "ansi_stdlib.h"
45 #endif /* !HAVE_STDLIB_H */
47 #if defined (HAVE_STRING_H)
51 #endif /* !HAVE_STRING_H */
57 #if defined (__STDC__)
63 #endif /* !__STDC__ */
65 #if !defined (PATH_MAX)
66 # if defined (MAXPATHLEN)
67 # define PATH_MAX MAXPATHLEN
68 # else /* !MAXPATHLEN */
69 # define PATH_MAX 1024
70 # endif /* !MAXPATHLEN */
71 #endif /* !PATH_MAX */
73 #if !defined (HAVE_LSTAT)
77 /* Get the pathname of the current working directory,
78 and put it in SIZE bytes of BUF. Returns NULL if the
79 directory couldn't be determined or SIZE was too small.
80 If successful, returns BUF. In GNU, if BUF is NULL,
81 an array is allocated with `malloc'; the array is SIZE
82 bytes long, unless SIZE <= 0, in which case it is as
84 #if defined (__STDC__)
86 getcwd (char *buf
, size_t size
)
92 #endif /* !__STDC__ */
94 static CONST
char dots
[]
95 = "../../../../../../../../../../../../../../../../../../../../../../../\
96 ../../../../../../../../../../../../../../../../../../../../../../../../../../\
97 ../../../../../../../../../../../../../../../../../../../../../../../../../..";
98 CONST
char *dotp
, *dotlist
;
100 dev_t rootdev
, thisdev
;
101 ino_t rootino
, thisino
;
102 char path
[PATH_MAX
+ 1];
103 register char *pathp
;
108 if (buf
!= NULL
&& size
== 0)
111 return ((char *)NULL
);
114 pathsize
= sizeof (path
);
115 pathp
= &path
[pathsize
];
119 if (stat (".", &st
) < 0)
120 return ((char *)NULL
);
124 if (stat ("/", &st
) < 0)
125 return ((char *)NULL
);
129 dotsize
= sizeof (dots
) - 1;
130 dotp
= &dots
[sizeof (dots
)];
132 while (!(thisdev
== rootdev
&& thisino
== rootino
))
134 register DIR *dirstream
;
135 register struct dirent
*d
;
141 /* Look at the parent directory. */
144 /* My, what a deep directory tree you have, Grandma. */
148 new = malloc (dotsize
* 2 + 1);
151 memcpy (new, dots
, dotsize
);
155 new = realloc ((PTR
) dotlist
, dotsize
* 2 + 1);
159 memcpy (&new[dotsize
], new, dotsize
);
160 dotp
= &new[dotsize
];
168 /* Figure out if this directory is a mount point. */
169 if (stat (dotp
, &st
) < 0)
173 mount_point
= dotdev
!= thisdev
;
175 /* Search for the last directory. */
176 dirstream
= opendir (dotp
);
177 if (dirstream
== NULL
)
179 while ((d
= readdir (dirstream
)) != NULL
)
181 if (d
->d_name
[0] == '.' &&
182 (d
->d_name
[1] == '\0' ||
183 (d
->d_name
[1] == '.' && d
->d_name
[2] == '\0')))
185 if (mount_point
|| d
->d_fileno
== thisino
)
189 namlen
= D_NAMLEN(d
);
191 alloca (dotlist
+ dotsize
- dotp
+ 1 + namlen
+ 1);
192 memcpy (name
, dotp
, dotlist
+ dotsize
- dotp
);
193 name
[dotlist
+ dotsize
- dotp
] = '/';
194 memcpy (&name
[dotlist
+ dotsize
- dotp
+ 1],
195 d
->d_name
, namlen
+ 1);
196 if (lstat (name
, &st
) < 0)
199 (void) closedir (dirstream
);
203 if (st
.st_dev
== thisdev
&& st
.st_ino
== thisino
)
210 (void) closedir (dirstream
);
218 while ((space
= pathp
- pathbuf
) <= namlen
)
224 new = malloc (pathsize
* 2);
230 new = realloc ((PTR
) pathbuf
, (pathsize
* 2));
235 (void) memcpy (new + pathsize
+ space
, pathp
, pathsize
- space
);
236 pathp
= new + pathsize
+ space
;
242 (void) memcpy (pathp
, d
->d_name
, namlen
);
244 (void) closedir (dirstream
);
251 if (pathp
== &path
[sizeof(path
) - 1])
255 free ((PTR
) dotlist
);
258 size_t len
= pathbuf
+ pathsize
- pathp
;
261 if (len
< (size_t) size
)
263 buf
= (char *) malloc (len
);
267 else if ((size_t) size
< len
)
272 (void) memcpy((PTR
) buf
, (PTR
) pathp
, len
);
281 if ((dotlist
!= dots
) && dotlist
)
284 free ((PTR
) dotlist
);
289 if ((pathbuf
!= path
) && pathbuf
)
292 free ((PTR
) pathbuf
);
295 return ((char *)NULL
);
306 if (getcwd(b
, sizeof(b
)))
313 perror ("cwd: getcwd");
318 #endif /* !HAVE_GETCWD */