]> git.saurik.com Git - apple/libc.git/blob - gen/FreeBSD/getcwd.c.patch
Libc-391.5.22.tar.gz
[apple/libc.git] / gen / FreeBSD / getcwd.c.patch
1 --- getcwd.c.orig 2006-01-19 18:35:45.000000000 -0800
2 +++ getcwd.c 2006-02-08 17:53:41.000000000 -0800
3 @@ -54,7 +54,81 @@
4 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
5 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
6
7 -extern int __getcwd(char *, size_t);
8 +/*
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.
14 + */
15 +static inline int
16 +__getcwd(char *buf, size_t size)
17 +{
18 + int fd, err, save;
19 + struct stat dot, pt;
20 + char *b;
21 +
22 + if ((fd = open(".", O_RDONLY)) < 0)
23 + return -1;
24 + if (fstat(fd, &dot) < 0) {
25 + save = errno;
26 + close(fd);
27 + errno = save;
28 + return -1;
29 + }
30 + /* check that the device and inode are non-zero, otherwise punt */
31 + if (dot.st_dev == 0 || dot.st_ino == 0) {
32 + close(fd);
33 + errno = EINVAL;
34 + return -1;
35 + }
36 + if (size < MAXPATHLEN) {
37 + /* the hard case; allocate a buffer of size MAXPATHLEN to use */
38 + b = (char *)alloca(MAXPATHLEN);
39 + if (b == NULL) {
40 + close(fd);
41 + errno = ENOMEM; /* make sure it isn't ERANGE */
42 + return -1;
43 + }
44 + } else
45 + b = buf;
46 +
47 + err = fcntl(fd, F_GETPATH, b);
48 + if (err) {
49 + save = errno;
50 + close(fd);
51 + errno = save;
52 + return err;
53 + }
54 + close(fd);
55 + /*
56 + * now double-check that the path returned by fcntl() has the same
57 + * device and inode number as '.'.
58 + */
59 + if (stat(b, &pt) < 0)
60 + return -1;
61 + /*
62 + * Since dot.st_dev and dot.st_ino are non-zero, we don't need to
63 + * separately test for pt.st_dev and pt.st_ino being non-zero, because
64 + * they have to match
65 + */
66 + if (dot.st_dev != pt.st_dev || dot.st_ino != pt.st_ino) {
67 + errno = EINVAL;
68 + return -1;
69 + }
70 + /*
71 + * For the case where we allocated a buffer, check that it can fit
72 + * in the real buffer, and copy it over.
73 + */
74 + if (size < MAXPATHLEN) {
75 + if (strlen(b) >= size) {
76 + errno = ERANGE;
77 + return -1;
78 + }
79 + strcpy(buf, b);
80 + }
81 + return 0;
82 +}
83
84 char *
85 getcwd(pt, size)
86 @@ -91,31 +165,23 @@
87 }
88 ept = pt + size;
89 } else {
90 - if ((pt = malloc(ptsize = 1024 - 4)) == NULL)
91 + if ((pt = malloc(ptsize = MAXPATHLEN)) == NULL)
92 return (NULL);
93 ept = pt + ptsize;
94 }
95 if (__getcwd(pt, ept - pt) == 0) {
96 - if (*pt != '/') {
97 - bpt = pt;
98 - ept = pt + strlen(pt) - 1;
99 - while (bpt < ept) {
100 - c = *bpt;
101 - *bpt++ = *ept;
102 - *ept-- = c;
103 - }
104 - }
105 return (pt);
106 - }
107 + } else if (errno == ERANGE) /* failed because buffer too small */
108 + return NULL;
109 bpt = ept - 1;
110 *bpt = '\0';
111
112 /*
113 - * Allocate bytes (1024 - malloc space) for the string of "../"'s.
114 + * Allocate bytes MAXPATHLEN) for the string of "../"'s.
115 * Should always be enough (it's 340 levels). If it's not, allocate
116 * as necessary. Special case the first stat, it's ".", not "..".
117 */
118 - if ((up = malloc(upsize = 1024 - 4)) == NULL)
119 + if ((up = malloc(upsize = MAXPATHLEN)) == NULL)
120 goto err;
121 eup = up + MAXPATHLEN;
122 bup = up;