1 --- getcwd.c.orig 2006-06-07 17:42:52.000000000 -0700
2 +++ getcwd.c 2006-06-07 17:44:47.000000000 -0700
4 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
5 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
7 -extern int __getcwd(char *, size_t);
9 + * If __getcwd() ever becomes a syscall, we can remove this workaround.
10 + * The problem here is that fcntl() assumes a buffer of size MAXPATHLEN,
11 + * if size is less than MAXPATHLEN, we need to use a temporary buffer
12 + * and see if it fits. We also have to assume that open() or fcntl()
13 + * don't fail with errno=ERANGE.
16 +__getcwd(char *buf, size_t size)
19 + struct stat dot, pt;
22 + if ((fd = open(".", O_RDONLY)) < 0)
24 + if (fstat(fd, &dot) < 0) {
30 + /* check that the device and inode are non-zero, otherwise punt */
31 + if (dot.st_dev == 0 || dot.st_ino == 0) {
36 + if (size < MAXPATHLEN) {
37 + /* the hard case; allocate a buffer of size MAXPATHLEN to use */
38 + b = (char *)alloca(MAXPATHLEN);
41 + errno = ENOMEM; /* make sure it isn't ERANGE */
49 + err = fcntl(fd, F_GETPATH, b);
58 + * now double-check that the path returned by fcntl() has the same
59 + * device and inode number as '.'.
61 + if (stat(b, &pt) < 0)
64 + * Since dot.st_dev and dot.st_ino are non-zero, we don't need to
65 + * separately test for pt.st_dev and pt.st_ino being non-zero, because
66 + * they have to match
68 + if (dot.st_dev != pt.st_dev || dot.st_ino != pt.st_ino) {
73 + * For the case where we allocated a buffer, check that it can fit
74 + * in the real buffer, and copy it over.
76 + if (size < MAXPATHLEN) {
77 + if (strlen(b) >= size) {
86 +__private_extern__ char *
87 +__private_getcwd(pt, size, usegetpath)
98 - if ((pt = malloc(ptsize = 1024 - 4)) == NULL)
99 + if ((pt = malloc(ptsize = MAXPATHLEN)) == NULL)
103 - if (__getcwd(pt, ept - pt) == 0) {
106 - ept = pt + strlen(pt) - 1;
107 - while (bpt < ept) {
115 + if (__getcwd(pt, ept - pt) == 0) {
117 + } else if (errno == ERANGE) /* failed because buffer too small */
124 - * Allocate bytes (1024 - malloc space) for the string of "../"'s.
125 + * Allocate bytes MAXPATHLEN) for the string of "../"'s.
126 * Should always be enough (it's 340 levels). If it's not, allocate
127 * as necessary. Special case the first stat, it's ".", not "..".
129 - if ((up = malloc(upsize = 1024 - 4)) == NULL)
130 + if ((up = malloc(upsize = MAXPATHLEN)) == NULL)
132 eup = up + MAXPATHLEN;
144 + return __private_getcwd(pt, size, 1);