author | Jon Tibble <meths@btinternet.com> |
Thu, 09 Dec 2010 22:32:39 +0100 | |
changeset 13255 | 4afa820d78b9 |
parent 6812 | febeba71273d |
permissions | -rw-r--r-- |
0 | 1 |
/* |
2 |
* CDDL HEADER START |
|
3 |
* |
|
4 |
* The contents of this file are subject to the terms of the |
|
3453
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
5 |
* Common Development and Distribution License (the "License"). |
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
6 |
* You may not use this file except in compliance with the License. |
0 | 7 |
* |
8 |
* You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE |
|
9 |
* or http://www.opensolaris.org/os/licensing. |
|
10 |
* See the License for the specific language governing permissions |
|
11 |
* and limitations under the License. |
|
12 |
* |
|
13 |
* When distributing Covered Code, include this CDDL HEADER in each |
|
14 |
* file and include the License file at usr/src/OPENSOLARIS.LICENSE. |
|
15 |
* If applicable, add the following below this CDDL HEADER, with the |
|
16 |
* fields enclosed by brackets "[]" replaced with your own identifying |
|
17 |
* information: Portions Copyright [yyyy] [name of copyright owner] |
|
18 |
* |
|
19 |
* CDDL HEADER END |
|
20 |
*/ |
|
3453
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
21 |
|
0 | 22 |
/* |
6812 | 23 |
* Copyright 2008 Sun Microsystems, Inc. All rights reserved. |
0 | 24 |
* Use is subject to license terms. |
25 |
*/ |
|
26 |
||
27 |
/* Copyright (c) 1988 AT&T */ |
|
28 |
/* All Rights Reserved */ |
|
29 |
||
30 |
/* |
|
31 |
* University Copyright- Copyright (c) 1982, 1986, 1988 |
|
32 |
* The Regents of the University of California |
|
33 |
* All Rights Reserved |
|
34 |
* |
|
35 |
* University Acknowledgment- Portions of this document are derived from |
|
36 |
* software developed by the University of California, Berkeley, and its |
|
37 |
* contributors. |
|
38 |
*/ |
|
39 |
||
40 |
#pragma ident "%Z%%M% %I% %E% SMI" |
|
41 |
||
42 |
/* |
|
43 |
* readdir -- C library extension routine |
|
44 |
*/ |
|
45 |
||
46 |
#include <sys/feature_tests.h> |
|
47 |
||
48 |
#if !defined(_LP64) |
|
6812 | 49 |
#pragma weak _readdir64 = readdir64 |
0 | 50 |
#endif |
6812 | 51 |
#pragma weak _readdir = readdir |
0 | 52 |
|
6812 | 53 |
#include "lint.h" |
3453
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
54 |
#include <dirent.h> |
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
55 |
#include <limits.h> |
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
56 |
#include <errno.h> |
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
57 |
#include "libc.h" |
0 | 58 |
|
59 |
#ifdef _LP64 |
|
60 |
||
3453
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
61 |
dirent_t * |
0 | 62 |
readdir(DIR *dirp) |
63 |
{ |
|
3453
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
64 |
dirent_t *dp; /* -> directory data */ |
0 | 65 |
int saveloc = 0; |
66 |
||
67 |
if (dirp->dd_size != 0) { |
|
3453
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
68 |
dp = (dirent_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc]; |
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
69 |
saveloc = dirp->dd_loc; /* save for possible EOF */ |
0 | 70 |
dirp->dd_loc += (int)dp->d_reclen; |
71 |
} |
|
72 |
if (dirp->dd_loc >= dirp->dd_size) |
|
73 |
dirp->dd_loc = dirp->dd_size = 0; |
|
74 |
||
75 |
if (dirp->dd_size == 0 && /* refill buffer */ |
|
76 |
(dirp->dd_size = getdents(dirp->dd_fd, |
|
3453
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
77 |
(dirent_t *)(uintptr_t)dirp->dd_buf, DIRBUF)) <= 0) { |
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
78 |
if (dirp->dd_size == 0) /* This means EOF */ |
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
79 |
dirp->dd_loc = saveloc; /* so save for telldir */ |
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
80 |
return (NULL); /* error or EOF */ |
0 | 81 |
} |
82 |
||
3453
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
83 |
return ((dirent_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc]); |
0 | 84 |
} |
85 |
||
86 |
#else /* _LP64 */ |
|
87 |
||
88 |
/* |
|
89 |
* Welcome to the complicated world of large files on a small system. |
|
90 |
*/ |
|
91 |
||
3453
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
92 |
dirent64_t * |
0 | 93 |
readdir64(DIR *dirp) |
94 |
{ |
|
3453
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
95 |
dirent64_t *dp64; /* -> directory data */ |
0 | 96 |
int saveloc = 0; |
97 |
||
98 |
if (dirp->dd_size != 0) { |
|
3453
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
99 |
dp64 = (dirent64_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc]; |
0 | 100 |
/* was converted by readdir and needs to be reversed */ |
101 |
if (dp64->d_ino == (ino64_t)-1) { |
|
3453
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
102 |
dirent_t *dp32; |
0 | 103 |
|
3453
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
104 |
dp32 = (dirent_t *)(&dp64->d_off); |
0 | 105 |
dp64->d_ino = (ino64_t)dp32->d_ino; |
106 |
dp64->d_off = (off64_t)dp32->d_off; |
|
107 |
dp64->d_reclen = (unsigned short)(dp32->d_reclen + |
|
3453
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
108 |
((char *)&dp64->d_off - (char *)dp64)); |
0 | 109 |
} |
3453
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
110 |
saveloc = dirp->dd_loc; /* save for possible EOF */ |
0 | 111 |
dirp->dd_loc += (int)dp64->d_reclen; |
112 |
} |
|
113 |
if (dirp->dd_loc >= dirp->dd_size) |
|
114 |
dirp->dd_loc = dirp->dd_size = 0; |
|
115 |
||
116 |
if (dirp->dd_size == 0 && /* refill buffer */ |
|
117 |
(dirp->dd_size = getdents64(dirp->dd_fd, |
|
3453
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
118 |
(dirent64_t *)(uintptr_t)dirp->dd_buf, DIRBUF)) <= 0) { |
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
119 |
if (dirp->dd_size == 0) /* This means EOF */ |
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
120 |
dirp->dd_loc = saveloc; /* so save for telldir */ |
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
121 |
return (NULL); /* error or EOF */ |
0 | 122 |
} |
123 |
||
3453
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
124 |
dp64 = (dirent64_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc]; |
0 | 125 |
return (dp64); |
126 |
} |
|
127 |
||
128 |
/* |
|
129 |
* readdir now does translation of dirent64 entries into dirent entries. |
|
130 |
* We rely on the fact that dirents are smaller than dirent64s and we |
|
131 |
* reuse the space accordingly. |
|
132 |
*/ |
|
3453
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
133 |
dirent_t * |
0 | 134 |
readdir(DIR *dirp) |
135 |
{ |
|
3453
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
136 |
dirent64_t *dp64; /* -> directory data */ |
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
137 |
dirent_t *dp32; /* -> directory data */ |
0 | 138 |
|
139 |
if ((dp64 = readdir64(dirp)) == NULL) |
|
140 |
return (NULL); |
|
141 |
||
142 |
/* |
|
143 |
* Make sure that the offset fits in 32 bits. |
|
144 |
*/ |
|
145 |
if (((off_t)dp64->d_off != dp64->d_off && |
|
3453
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
146 |
(uint64_t)dp64->d_off > (uint64_t)UINT32_MAX) || |
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
147 |
dp64->d_ino > SIZE_MAX) { |
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
148 |
errno = EOVERFLOW; |
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
149 |
return (NULL); |
0 | 150 |
} |
151 |
||
3453
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
152 |
dp32 = (dirent_t *)(&dp64->d_off); |
0 | 153 |
dp32->d_off = (off_t)dp64->d_off; |
154 |
dp32->d_ino = (ino_t)dp64->d_ino; |
|
155 |
dp32->d_reclen = (unsigned short)(dp64->d_reclen - |
|
156 |
((char *)&dp64->d_off - (char *)dp64)); |
|
3453
bc3a776612a0
6513772 readdir_r() and associates should use per-DIR locking
raf
parents:
0
diff
changeset
|
157 |
dp64->d_ino = (ino64_t)-1; /* flag as converted for readdir64 */ |
0 | 158 |
/* d_name d_reclen should not move */ |
159 |
return (dp32); |
|
160 |
} |
|
161 |
#endif /* _LP64 */ |