]> git.saurik.com Git - apple/libc.git/blobdiff - gen/FreeBSD/getcwd.c
Libc-763.12.tar.gz
[apple/libc.git] / gen / FreeBSD / getcwd.c
index 3b6a81376a756a11375f3941835ab1f6a1ddbd93..d06210b5d0dc46d4c9a1b437bdbcaf4238a0a939 100644 (file)
  * 2. Redistributions in binary form must reproduce the above copyright
  *    notice, this list of conditions and the following disclaimer in the
  *    documentation and/or other materials provided with the distribution.
- * 3. All advertising materials mentioning features or use of this software
- *    must display the following acknowledgement:
- *     This product includes software developed by the University of
- *     California, Berkeley and its contributors.
  * 4. Neither the name of the University nor the names of its contributors
  *    may be used to endorse or promote products derived from this software
  *    without specific prior written permission.
@@ -35,7 +31,7 @@
 static char sccsid[] = "@(#)getcwd.c   8.5 (Berkeley) 2/7/95";
 #endif /* LIBC_SCCS and not lint */
 #include <sys/cdefs.h>
-__FBSDID("$FreeBSD: src/lib/libc/gen/getcwd.c,v 1.25 2003/10/29 10:45:01 tjr Exp $");
+__FBSDID("$FreeBSD: src/lib/libc/gen/getcwd.c,v 1.29 2007/01/09 00:27:53 imp Exp $");
 
 #include "namespace.h"
 #include <sys/param.h>
@@ -91,7 +87,7 @@ getcwd(pt, size)
                }
                ept = pt + size;
        } else {
-               if ((pt = malloc(ptsize = 1024 - 4)) == NULL)
+               if ((pt = malloc(ptsize = PATH_MAX)) == NULL)
                        return (NULL);
                ept = pt + ptsize;
        }
@@ -111,13 +107,13 @@ getcwd(pt, size)
        *bpt = '\0';
 
        /*
-        * Allocate bytes (1024 - malloc space) for the string of "../"'s.
-        * Should always be enough (it's 340 levels).  If it's not, allocate
+        * Allocate 1024 bytes for the string of "../"'s.
+        * Should always be enough.  If it's not, allocate
         * as necessary.  Special case the first stat, it's ".", not "..".
         */
-       if ((up = malloc(upsize = 1024 - 4)) == NULL)
+       if ((up = malloc(upsize = 1024)) == NULL)
                goto err;
-       eup = up + MAXPATHLEN;
+       eup = up + upsize;
        bup = up;
        up[0] = '.';
        up[1] = '\0';
@@ -157,7 +153,7 @@ getcwd(pt, size)
                 * as necessary.  Max length is 3 for "../", the largest
                 * possible component name, plus a trailing NUL.
                 */
-               if (bup + 3  + MAXNAMLEN + 1 >= eup) {
+               while (bup + 3  + MAXNAMLEN + 1 >= eup) {
                        if ((up = reallocf(up, upsize *= 2)) == NULL)
                                goto err;
                        bup = up;
@@ -211,7 +207,7 @@ getcwd(pt, size)
                 * Check for length of the current name, preceding slash,
                 * leading slash.
                 */
-               if (bpt - pt < dp->d_namlen + (first ? 1 : 2)) {
+               while (bpt - pt < dp->d_namlen + (first ? 1 : 2)) {
                        size_t len, off;
 
                        if (!ptsize) {