xref: /titanic_44/usr/src/lib/libc/port/gen/readdir.c (revision 9dd0f810214fdc8e1af881a9a5c4b6927629ff9e)
1 /*
2  * CDDL HEADER START
3  *
4  * The contents of this file are subject to the terms of the
5  * Common Development and Distribution License, Version 1.0 only
6  * (the "License").  You may not use this file except in compliance
7  * with the License.
8  *
9  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10  * or http://www.opensolaris.org/os/licensing.
11  * See the License for the specific language governing permissions
12  * and limitations under the License.
13  *
14  * When distributing Covered Code, include this CDDL HEADER in each
15  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16  * If applicable, add the following below this CDDL HEADER, with the
17  * fields enclosed by brackets "[]" replaced with your own identifying
18  * information: Portions Copyright [yyyy] [name of copyright owner]
19  *
20  * CDDL HEADER END
21  */
22 /*
23  * Copyright 2004 Sun Microsystems, Inc.  All rights reserved.
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)
49 #pragma weak readdir64 = _readdir64
50 #endif
51 #pragma weak readdir = _readdir
52 
53 #include	"synonyms.h"
54 #include	<sys/types.h>
55 #include	<sys/dirent.h>
56 #include	<dirent.h>
57 #include	<limits.h>
58 #include	<errno.h>
59 #include	"libc.h"
60 
61 #ifdef _LP64
62 
63 struct dirent *
64 readdir(DIR *dirp)
65 {
66 	struct dirent	*dp;	/* -> directory data */
67 	int saveloc = 0;
68 
69 	if (dirp->dd_size != 0) {
70 		dp = (struct dirent *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
71 		saveloc = dirp->dd_loc;   /* save for possible EOF */
72 		dirp->dd_loc += (int)dp->d_reclen;
73 	}
74 	if (dirp->dd_loc >= dirp->dd_size)
75 		dirp->dd_loc = dirp->dd_size = 0;
76 
77 	if (dirp->dd_size == 0 && 	/* refill buffer */
78 	    (dirp->dd_size = getdents(dirp->dd_fd,
79 	    (struct dirent *)(uintptr_t)dirp->dd_buf, DIRBUF)) <= 0) {
80 		if (dirp->dd_size == 0)	/* This means EOF */
81 				dirp->dd_loc = saveloc;  /* EOF so save for */
82 							/* telldir */
83 		return (NULL);	/* error or EOF */
84 	}
85 
86 	return ((struct dirent *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc]);
87 }
88 
89 #else	/* _LP64 */
90 
91 /*
92  * Welcome to the complicated world of large files on a small system.
93  */
94 
95 struct dirent64 *
96 readdir64(DIR *dirp)
97 {
98 	struct dirent64	*dp64;	/* -> directory data */
99 	int saveloc = 0;
100 
101 	if (dirp->dd_size != 0) {
102 		dp64 = (struct dirent64 *)
103 			(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
104 		/* was converted by readdir and needs to be reversed */
105 		if (dp64->d_ino == (ino64_t)-1) {
106 			struct dirent		*dp32;
107 
108 			dp32 = (struct dirent *)(&dp64->d_off);
109 			dp64->d_ino = (ino64_t)dp32->d_ino;
110 			dp64->d_off = (off64_t)dp32->d_off;
111 			dp64->d_reclen = (unsigned short)(dp32->d_reclen +
112 				((char *)&dp64->d_off - (char *)dp64));
113 		}
114 		saveloc = dirp->dd_loc;   /* save for possible EOF */
115 		dirp->dd_loc += (int)dp64->d_reclen;
116 	}
117 	if (dirp->dd_loc >= dirp->dd_size)
118 		dirp->dd_loc = dirp->dd_size = 0;
119 
120 	if (dirp->dd_size == 0 && 	/* refill buffer */
121 	    (dirp->dd_size = getdents64(dirp->dd_fd,
122 	    (struct dirent64 *)(uintptr_t)dirp->dd_buf, DIRBUF)) <= 0) {
123 		if (dirp->dd_size == 0)	/* This means EOF */
124 				dirp->dd_loc = saveloc;  /* EOF so save for */
125 							/* telldir */
126 		return (NULL);	/* error or EOF */
127 	}
128 
129 	dp64 = (struct dirent64 *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
130 	return (dp64);
131 }
132 
133 /*
134  * readdir now does translation of dirent64 entries into dirent entries.
135  * We rely on the fact that dirents are smaller than dirent64s and we
136  * reuse the space accordingly.
137  */
138 struct dirent *
139 readdir(DIR *dirp)
140 {
141 	struct dirent64		*dp64;	/* -> directory data */
142 	struct dirent		*dp32;	/* -> directory data */
143 
144 	if ((dp64 = readdir64(dirp)) == NULL)
145 		return (NULL);
146 
147 	/*
148 	 * Make sure that the offset fits in 32 bits.
149 	 */
150 	if (((off_t)dp64->d_off != dp64->d_off &&
151 		(uint64_t)dp64->d_off > (uint64_t)UINT32_MAX) ||
152 		(dp64->d_ino > SIZE_MAX)) {
153 			errno = EOVERFLOW;
154 			return (NULL);
155 	}
156 
157 	dp32 = (struct dirent *)(&dp64->d_off);
158 	dp32->d_off = (off_t)dp64->d_off;
159 	dp32->d_ino = (ino_t)dp64->d_ino;
160 	dp32->d_reclen = (unsigned short)(dp64->d_reclen -
161 	    ((char *)&dp64->d_off - (char *)dp64));
162 	dp64->d_ino = (ino64_t)-1; /* flag as converted for readdir64 */
163 	/* d_name d_reclen should not move */
164 	return (dp32);
165 }
166 #endif	/* _LP64 */
167