Deleted Added
full compact
getcwd.c (126274) getcwd.c (157016)
1/* OPENBSD ORIGINAL: lib/libc/gen/getcwd.c */
2
1/* $OpenBSD: getcwd.c,v 1.14 2005/08/08 08:05:34 espie Exp $ */
3/*
4 * Copyright (c) 1989, 1991, 1993
5 * The Regents of the University of California. All rights reserved.
6 *
7 * Redistribution and use in source and binary forms, with or without
8 * modification, are permitted provided that the following conditions
9 * are met:
10 * 1. Redistributions of source code must retain the above copyright

--- 13 unchanged lines hidden (view full) ---

24 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
25 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
26 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
27 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
28 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 * SUCH DAMAGE.
30 */
31
2/*
3 * Copyright (c) 1989, 1991, 1993
4 * The Regents of the University of California. All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions
8 * are met:
9 * 1. Redistributions of source code must retain the above copyright

--- 13 unchanged lines hidden (view full) ---

23 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
24 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
25 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
26 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
27 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
28 * SUCH DAMAGE.
29 */
30
31/* OPENBSD ORIGINAL: lib/libc/gen/getcwd.c */
32
32#include "includes.h"
33
34#if !defined(HAVE_GETCWD)
35
33#include "includes.h"
34
35#if !defined(HAVE_GETCWD)
36
36#if defined(LIBC_SCCS) && !defined(lint)
37static char rcsid[] = "$OpenBSD: getcwd.c,v 1.9 2003/06/11 21:03:10 deraadt Exp $";
38#endif /* LIBC_SCCS and not lint */
39
40#include <sys/param.h>
41#include <sys/stat.h>
42#include <errno.h>
43#include <dirent.h>
44#include <sys/dir.h>
45#include <stdio.h>
46#include <stdlib.h>
47#include <string.h>
48#include "includes.h"
49
50#define ISDOT(dp) \
51 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
52 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
53
54char *
55getcwd(char *pt, size_t size)
56{
37#include <sys/param.h>
38#include <sys/stat.h>
39#include <errno.h>
40#include <dirent.h>
41#include <sys/dir.h>
42#include <stdio.h>
43#include <stdlib.h>
44#include <string.h>
45#include "includes.h"
46
47#define ISDOT(dp) \
48 (dp->d_name[0] == '.' && (dp->d_name[1] == '\0' || \
49 (dp->d_name[1] == '.' && dp->d_name[2] == '\0')))
50
51char *
52getcwd(char *pt, size_t size)
53{
57 register struct dirent *dp;
58 register DIR *dir = NULL;
59 register dev_t dev;
60 register ino_t ino;
61 register int first;
62 register char *bpt, *bup;
54 struct dirent *dp;
55 DIR *dir = NULL;
56 dev_t dev;
57 ino_t ino;
58 int first;
59 char *bpt, *bup;
63 struct stat s;
64 dev_t root_dev;
65 ino_t root_ino;
66 size_t ptsize, upsize;
67 int save_errno;
68 char *ept, *eup, *up;
69
70 /*

--- 4 unchanged lines hidden (view full) ---

75 if (pt) {
76 ptsize = 0;
77 if (!size) {
78 errno = EINVAL;
79 return (NULL);
80 }
81 ept = pt + size;
82 } else {
60 struct stat s;
61 dev_t root_dev;
62 ino_t root_ino;
63 size_t ptsize, upsize;
64 int save_errno;
65 char *ept, *eup, *up;
66
67 /*

--- 4 unchanged lines hidden (view full) ---

72 if (pt) {
73 ptsize = 0;
74 if (!size) {
75 errno = EINVAL;
76 return (NULL);
77 }
78 ept = pt + size;
79 } else {
83 if ((pt = malloc(ptsize = 1024 - 4)) == NULL)
80 if ((pt = malloc(ptsize = MAXPATHLEN)) == NULL)
84 return (NULL);
85 ept = pt + ptsize;
86 }
87 bpt = ept - 1;
88 *bpt = '\0';
89
90 /*
81 return (NULL);
82 ept = pt + ptsize;
83 }
84 bpt = ept - 1;
85 *bpt = '\0';
86
87 /*
91 * Allocate bytes (1024 - malloc space) for the string of "../"'s.
88 * Allocate bytes for the string of "../"'s.
92 * Should always be enough (it's 340 levels). If it's not, allocate
93 * as necessary. Special * case the first stat, it's ".", not "..".
94 */
89 * Should always be enough (it's 340 levels). If it's not, allocate
90 * as necessary. Special * case the first stat, it's ".", not "..".
91 */
95 if ((up = malloc(upsize = 1024 - 4)) == NULL)
92 if ((up = malloc(upsize = MAXPATHLEN)) == NULL)
96 goto err;
93 goto err;
97 eup = up + MAXPATHLEN;
94 eup = up + upsize;
98 bup = up;
99 up[0] = '.';
100 up[1] = '\0';
101
102 /* Save root values, so know when to stop. */
103 if (stat("/", &s))
104 goto err;
105 root_dev = s.st_dev;

--- 28 unchanged lines hidden (view full) ---

134 * as necessary. Max length is 3 for "../", the largest
135 * possible component name, plus a trailing NUL.
136 */
137 if (bup + 3 + MAXNAMLEN + 1 >= eup) {
138 char *nup;
139
140 if ((nup = realloc(up, upsize *= 2)) == NULL)
141 goto err;
95 bup = up;
96 up[0] = '.';
97 up[1] = '\0';
98
99 /* Save root values, so know when to stop. */
100 if (stat("/", &s))
101 goto err;
102 root_dev = s.st_dev;

--- 28 unchanged lines hidden (view full) ---

131 * as necessary. Max length is 3 for "../", the largest
132 * possible component name, plus a trailing NUL.
133 */
134 if (bup + 3 + MAXNAMLEN + 1 >= eup) {
135 char *nup;
136
137 if ((nup = realloc(up, upsize *= 2)) == NULL)
138 goto err;
139 bup = nup + (bup - up);
142 up = nup;
140 up = nup;
143 bup = up;
144 eup = up + upsize;
145 }
146 *bup++ = '.';
147 *bup++ = '.';
148 *bup = '\0';
149
141 eup = up + upsize;
142 }
143 *bup++ = '.';
144 *bup++ = '.';
145 *bup = '\0';
146
150 /* Open and stat parent directory.
151 * RACE?? - replaced fstat(dirfd(dir), &s) w/ lstat(up,&s)
152 */
153 if (!(dir = opendir(up)) || lstat(up,&s))
147 /* Open and stat parent directory. */
148 if (!(dir = opendir(up)) || fstat(dirfd(dir), &s))
154 goto err;
155
156 /* Add trailing slash for next directory. */
157 *bup++ = '/';
158
159 /*
160 * If it's a mount point, have to stat each element because
161 * the inode number in the directory is for the entry in the

--- 8 unchanged lines hidden (view full) ---

170 break;
171 }
172 } else
173 for (;;) {
174 if (!(dp = readdir(dir)))
175 goto notfound;
176 if (ISDOT(dp))
177 continue;
149 goto err;
150
151 /* Add trailing slash for next directory. */
152 *bup++ = '/';
153
154 /*
155 * If it's a mount point, have to stat each element because
156 * the inode number in the directory is for the entry in the

--- 8 unchanged lines hidden (view full) ---

165 break;
166 }
167 } else
168 for (;;) {
169 if (!(dp = readdir(dir)))
170 goto notfound;
171 if (ISDOT(dp))
172 continue;
178 memmove(bup, dp->d_name, dp->d_namlen + 1);
173 memcpy(bup, dp->d_name, dp->d_namlen + 1);
179
180 /* Save the first error for later. */
181 if (lstat(up, &s)) {
182 if (!save_errno)
183 save_errno = errno;
184 errno = 0;
185 continue;
186 }
187 if (s.st_dev == dev && s.st_ino == ino)
188 break;
189 }
190
191 /*
192 * Check for length of the current name, preceding slash,
193 * leading slash.
194 */
195 if (bpt - pt < dp->d_namlen + (first ? 1 : 2)) {
174
175 /* Save the first error for later. */
176 if (lstat(up, &s)) {
177 if (!save_errno)
178 save_errno = errno;
179 errno = 0;
180 continue;
181 }
182 if (s.st_dev == dev && s.st_ino == ino)
183 break;
184 }
185
186 /*
187 * Check for length of the current name, preceding slash,
188 * leading slash.
189 */
190 if (bpt - pt < dp->d_namlen + (first ? 1 : 2)) {
196 size_t len, off;
191 size_t len;
197 char *npt;
198
199 if (!ptsize) {
200 errno = ERANGE;
201 goto err;
202 }
192 char *npt;
193
194 if (!ptsize) {
195 errno = ERANGE;
196 goto err;
197 }
203 off = bpt - pt;
204 len = ept - bpt;
205 if ((npt = realloc(pt, ptsize *= 2)) == NULL)
206 goto err;
198 len = ept - bpt;
199 if ((npt = realloc(pt, ptsize *= 2)) == NULL)
200 goto err;
201 bpt = npt + (bpt - pt);
207 pt = npt;
202 pt = npt;
208 bpt = pt + off;
209 ept = pt + ptsize;
210 memmove(ept - len, bpt, len);
211 bpt = ept - len;
212 }
213 if (!first)
214 *--bpt = '/';
215 bpt -= dp->d_namlen;
203 ept = pt + ptsize;
204 memmove(ept - len, bpt, len);
205 bpt = ept - len;
206 }
207 if (!first)
208 *--bpt = '/';
209 bpt -= dp->d_namlen;
216 memmove(bpt, dp->d_name, dp->d_namlen);
210 memcpy(bpt, dp->d_name, dp->d_namlen);
217 (void)closedir(dir);
218
219 /* Truncate any file name. */
220 *bup = '\0';
221 }
222
223notfound:
224 /*
225 * If readdir set errno, use it, not any saved error; otherwise,
226 * didn't find the current directory in its parent directory, set
227 * errno to ENOENT.
228 */
229 if (!errno)
230 errno = save_errno ? save_errno : ENOENT;
231 /* FALLTHROUGH */
232err:
211 (void)closedir(dir);
212
213 /* Truncate any file name. */
214 *bup = '\0';
215 }
216
217notfound:
218 /*
219 * If readdir set errno, use it, not any saved error; otherwise,
220 * didn't find the current directory in its parent directory, set
221 * errno to ENOENT.
222 */
223 if (!errno)
224 errno = save_errno ? save_errno : ENOENT;
225 /* FALLTHROUGH */
226err:
227 save_errno = errno;
228
233 if (ptsize)
234 free(pt);
229 if (ptsize)
230 free(pt);
235 if (up)
236 free(up);
231 free(up);
237 if (dir)
238 (void)closedir(dir);
232 if (dir)
233 (void)closedir(dir);
234
235 errno = save_errno;
236
239 return (NULL);
240}
241
242#endif /* !defined(HAVE_GETCWD) */
237 return (NULL);
238}
239
240#endif /* !defined(HAVE_GETCWD) */