/[path-sandbox]/trunk/getcwd.c
Gentoo

Diff of /trunk/getcwd.c

Parent Directory Parent Directory | Revision Log Revision Log | View Patch Patch

Revision 29 Revision 30
29 * $Header$ 29 * $Header$
30 */ 30 */
31 31
32/* Modified: 26 July 2003; Martin Schlemmer <azarah@gentoo.org> 32/* Modified: 26 July 2003; Martin Schlemmer <azarah@gentoo.org>
33 * 33 *
34 * Cleaned up unneeded stuff. Add a wrapper to try and detect when 34 * Cleaned up unneeded stuff. Add a wrapper to try and detect when
35 * we have a kernel whose getcwd system call do not handle directory 35 * we have a kernel whose getcwd system call do not handle directory
36 * names longer than PATH_MAX, and if so, use our generic version. 36 * names longer than PATH_MAX, and if so, use our generic version.
37 * To work truly with > PATH_MAX lengh CWDs, I had to increase the 37 * To work truly with > PATH_MAX lengh CWDs, I had to increase the
38 * size of the dots[] array. Also prepended a 'e' to functions that 38 * size of the dots[] array. Also prepended a 'e' to functions that
39 * I did not rip out. 39 * I did not rip out.
64#endif 64#endif
65#ifndef __set_errno 65#ifndef __set_errno
66# define __set_errno(val) errno = (val) 66# define __set_errno(val) errno = (val)
67#endif 67#endif
68 68
69#ifndef NULL 69#ifndef NULL
70# define NULL 0 70# define NULL 0
71#endif 71#endif
72 72
73#if defined USGr3 && !defined DIRENT 73#if defined USGr3 && !defined DIRENT
74# define DIRENT 74# define DIRENT
75#endif /* USGr3 */ 75#endif /* USGr3 */
76#if defined Xenix && !defined SYSNDIR 76#if defined Xenix && !defined SYSNDIR
77# define SYSNDIR 77# define SYSNDIR
78#endif /* Xenix */ 78#endif /* Xenix */
79 79
80#if defined POSIX || defined DIRENT || defined __GNU_LIBRARY__ 80#if defined POSIX || defined DIRENT || defined __GNU_LIBRARY__
81# include <dirent.h> 81# include <dirent.h>
82# ifndef __GNU_LIBRARY__ 82# ifndef __GNU_LIBRARY__
83# define D_NAMLEN(d) strlen((d)->d_name) 83# define D_NAMLEN(d) strlen((d)->d_name)
84# else 84# else
85# define HAVE_D_NAMLEN 85# define HAVE_D_NAMLEN
86# define D_NAMLEN(d) ((d)->d_namlen) 86# define D_NAMLEN(d) ((d)->d_namlen)
87# endif 87# endif
88#else /* not POSIX or DIRENT */ 88#else /* not POSIX or DIRENT */
89# define dirent direct 89# define dirent direct
90# define D_NAMLEN(d) ((d)->d_namlen) 90# define D_NAMLEN(d) ((d)->d_namlen)
91# define HAVE_D_NAMLEN 91# define HAVE_D_NAMLEN
92# if defined USG && !defined sgi 92# if defined USG && !defined sgi
93# if defined SYSNDIR 93# if defined SYSNDIR
94# include <sys/ndir.h> 94# include <sys/ndir.h>
95# else /* Not SYSNDIR */ 95# else /* Not SYSNDIR */
96# include "ndir.h" 96# include "ndir.h"
97# endif /* SYSNDIR */ 97# endif /* SYSNDIR */
98# else /* not USG */ 98# else /* not USG */
99# include <sys/dir.h> 99# include <sys/dir.h>
100# endif /* USG */ 100# endif /* USG */
101#endif /* POSIX or DIRENT or __GNU_LIBRARY__ */ 101#endif /* POSIX or DIRENT or __GNU_LIBRARY__ */
102 102
103#if defined HAVE_UNISTD_H || defined __GNU_LIBRARY__ 103#if defined HAVE_UNISTD_H || defined __GNU_LIBRARY__
104# include <unistd.h> 104# include <unistd.h>
105#endif 105#endif
106 106
107#if defined STDC_HEADERS || defined __GNU_LIBRARY__ || defined POSIX 107#if defined STDC_HEADERS || defined __GNU_LIBRARY__ || defined POSIX
108# include <stdlib.h> 108# include <stdlib.h>
109# include <string.h> 109# include <string.h>
110# define ANSI_STRING 110# define ANSI_STRING
111#else /* No standard headers. */ 111#else /* No standard headers. */
112 112
113# ifdef USG 113# ifdef USG
114 114
115# include <string.h> 115# include <string.h>
116# ifdef NEED_MEMORY_H 116# ifdef NEED_MEMORY_H
117# include <memory.h> 117# include <memory.h>
118# endif 118# endif
119# define ANSI_STRING 119# define ANSI_STRING
120 120
121# else /* Not USG. */ 121# else /* Not USG. */
122 122
123# ifdef NeXT 123# ifdef NeXT
124 124
125# include <string.h> 125# include <string.h>
126 126
127# else /* Not NeXT. */ 127# else /* Not NeXT. */
128 128
129# include <strings.h> 129# include <strings.h>
130 130
131# ifndef bcmp 131# ifndef bcmp
132extern int bcmp(); 132extern int bcmp();
136# endif 136# endif
137# ifndef bcopy 137# ifndef bcopy
138extern void bcopy(); 138extern void bcopy();
139# endif 139# endif
140 140
141# endif /* NeXT. */ 141# endif /* NeXT. */
142 142
143# endif /* USG. */ 143# endif /* USG. */
144 144
145extern char *malloc(), *realloc(); 145extern char *malloc(), *realloc();
146extern void free(); 146extern void free();
147 147
148#endif /* Standard headers. */ 148#endif /* Standard headers. */
149 149
150#ifndef ANSI_STRING 150#ifndef ANSI_STRING
151# define memcpy(d, s, n) bcopy((s), (d), (n)) 151# define memcpy(d, s, n) bcopy((s), (d), (n))
152# define memmove memcpy 152# define memmove memcpy
153#endif /* Not ANSI_STRING. */ 153#endif /* Not ANSI_STRING. */
154 154
155#ifndef MAX 155#ifndef MAX
156# define MAX(a, b) ((a) < (b) ? (b) : (a)) 156# define MAX(a, b) ((a) < (b) ? (b) : (a))
157#endif 157#endif
158 158
163# define HAVE_MEMPCPY 1 163# define HAVE_MEMPCPY 1
164#endif 164#endif
165 165
166#if !defined __alloca && !defined __GNU_LIBRARY__ 166#if !defined __alloca && !defined __GNU_LIBRARY__
167 167
168# ifdef __GNUC__ 168# ifdef __GNUC__
169# undef alloca 169# undef alloca
170# define alloca(n) __builtin_alloca (n) 170# define alloca(n) __builtin_alloca (n)
171# else /* Not GCC. */ 171# else /* Not GCC. */
172# if defined sparc || defined HAVE_ALLOCA_H 172# if defined sparc || defined HAVE_ALLOCA_H
173# include <alloca.h> 173# include <alloca.h>
174# else /* Not sparc or HAVE_ALLOCA_H. */ 174# else /* Not sparc or HAVE_ALLOCA_H. */
175# ifndef _AIX 175# ifndef _AIX
176extern char *alloca(); 176extern char *alloca();
177# endif /* Not _AIX. */ 177# endif /* Not _AIX. */
178# endif /* sparc or HAVE_ALLOCA_H. */ 178# endif /* sparc or HAVE_ALLOCA_H. */
179# endif /* GCC. */ 179# endif /* GCC. */
180 180
181# define __alloca alloca 181# define __alloca alloca
182 182
183#endif 183#endif
184 184
187#else 187#else
188# include <sys/param.h> 188# include <sys/param.h>
189#endif 189#endif
190 190
191#ifndef PATH_MAX 191#ifndef PATH_MAX
192# ifdef MAXPATHLEN 192# ifdef MAXPATHLEN
193# define PATH_MAX MAXPATHLEN 193# define PATH_MAX MAXPATHLEN
194# else 194# else
195# define PATH_MAX 1024 195# define PATH_MAX 1024
196# endif 196# endif
197#endif 197#endif
198 198
199#if !defined STDC_HEADERS && !defined __GNU_LIBRARY__ 199#if !defined STDC_HEADERS && !defined __GNU_LIBRARY__
200# undef size_t 200# undef size_t
201# define size_t unsigned int 201# define size_t unsigned int
202#endif 202#endif
203 203
204#if !__STDC__ && !defined const 204#if !__STDC__ && !defined const
205# define const 205# define const
206#endif 206#endif
223# define __closedir closedir 223# define __closedir closedir
224#endif 224#endif
225 225
226/* Get the pathname of the current working directory, and put it in SIZE 226/* Get the pathname of the current working directory, and put it in SIZE
227 bytes of BUF. Returns NULL if the directory couldn't be determined or 227 bytes of BUF. Returns NULL if the directory couldn't be determined or
228 SIZE was too small. If successful, returns BUF. In GNU, if BUF is 228 SIZE was too small. If successful, returns BUF. In GNU, if BUF is
229 NULL, an array is allocated with `malloc'; the array is SIZE bytes long, 229 NULL, an array is allocated with `malloc'; the array is SIZE bytes long,
230 unless SIZE == 0, in which case it is as big as necessary. */ 230 unless SIZE == 0, in which case it is as big as necessary. */
231 231
232GETCWD_RETURN_TYPE 232GETCWD_RETURN_TYPE
233__egetcwd(buf, size) 233__egetcwd(buf, size)
383 continue; 383 continue;
384 if (mount_point || (ino_t) d->d_ino == thisino) { 384 if (mount_point || (ino_t) d->d_ino == thisino) {
385 char name[dotlist + dotsize - dotp + 1 + _D_ALLOC_NAMLEN(d)]; 385 char name[dotlist + dotsize - dotp + 1 + _D_ALLOC_NAMLEN(d)];
386#ifdef HAVE_MEMPCPY 386#ifdef HAVE_MEMPCPY
387 char *tmp = mempcpy(name, dotp, 387 char *tmp = mempcpy(name, dotp,
388 dotlist + dotsize - dotp); 388 dotlist + dotsize - dotp);
389 *tmp++ = '/'; 389 *tmp++ = '/';
390 strcpy(tmp, d->d_name); 390 strcpy(tmp, d->d_name);
391#else 391#else
392 memcpy(name, dotp, dotlist + dotsize - dotp); 392 memcpy(name, dotp, dotlist + dotsize - dotp);
393 name[dotlist + dotsize - dotp] = '/'; 393 name[dotlist + dotsize - dotp] = '/';
432 goto lose; 432 goto lose;
433 } 433 }
434 434
435 /* Move current contents up to the end of the buffer. 435 /* Move current contents up to the end of the buffer.
436 This is guaranteed to be non-overlapping. */ 436 This is guaranteed to be non-overlapping. */
437 pathp =
438 memcpy(tmp + allocated - 437 pathp = memcpy(tmp + allocated -
439 (path + oldsize - pathp), 438 (path + oldsize - pathp),
440 tmp + (pathp - path), path + oldsize - pathp); 439 tmp + (pathp - path), path + oldsize - pathp);
441 path = tmp; 440 path = tmp;
442 } 441 }
443 } 442 }
444 pathp -= namlen; 443 pathp -= namlen;
445 (void) memcpy(pathp, d->d_name, namlen); 444 (void) memcpy(pathp, d->d_name, namlen);
457 if (dotlist != dots) 456 if (dotlist != dots)
458 free((__ptr_t) dotlist); 457 free((__ptr_t) dotlist);
459 458
460 memmove(path, pathp, path + allocated - pathp); 459 memmove(path, pathp, path + allocated - pathp);
461 460
462 /* Restore errno on successful return. */ 461 /* Restore errno on successful return. */
463 __set_errno(prev_errno); 462 __set_errno(prev_errno);
464 463
465 return path; 464 return path;
466 465
467lose: 466lose:
503 } 502 }
504 503
505 return tmpbuf; 504 return tmpbuf;
506} 505}
507 506
508// vim:expandtab noai:cindent ai 507// vim:noexpandtab noai:cindent ai

Legend:
Removed from v.29  
changed lines
  Added in v.30

  ViewVC Help
Powered by ViewVC 1.1.20