xref: /titanic_41/usr/src/ucblib/libucb/port/gen/readdir.c (revision 9e39c5ba00a55fa05777cc94b148296af305e135)
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 1997 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1984, 1986, 1987, 1988, 1989 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 /*LINTLIBRARY*/
43 
44 /*
45  * readdir -- C library extension routine
46  */
47 
48 #include	<sys/types.h>
49 #include	<sys/dir.h>
50 #include 	<sys/dirent.h>
51 #include 	<limits.h>
52 #include 	<string.h>
53 #include 	<errno.h>
54 #include 	"libc.h"
55 
56 static struct direct	dc;
57 static struct direct64	dc64;
58 
59 static struct direct64 *
60 internal_readdir(DIR *dirp)
61 {
62 	struct dirent64	*dp;	/* -> directory data */
63 	int saveloc = 0;
64 
65 	if (dirp->dd_size != 0) {
66 		dp = (struct dirent64 *)&dirp->dd_buf[dirp->dd_loc];
67 		saveloc = dirp->dd_loc;   /* save for possible EOF */
68 		dirp->dd_loc += dp->d_reclen;
69 	}
70 	if (dirp->dd_loc >= dirp->dd_size)
71 		dirp->dd_loc = dirp->dd_size = 0;
72 
73 	if (dirp->dd_size == 0 && 	/* refill buffer */
74 	    (dirp->dd_size = getdents64(dirp->dd_fd,
75 	    (struct dirent64 *)dirp->dd_buf, DIRBUF)) <= 0) {
76 		if (dirp->dd_size == 0)	/* This means EOF */
77 			dirp->dd_loc = saveloc;  /* EOF so save for telldir */
78 		return (NULL);	/* error or EOF */
79 	}
80 
81 	dp = (struct dirent64 *)&dirp->dd_buf[dirp->dd_loc];
82 
83 	/* Copy dirent into direct */
84 	dc64.d_ino = dp->d_ino;
85 	dc64.d_reclen = dp->d_reclen;
86 	dc64.d_namlen = (ushort_t)strlen(dp->d_name);
87 	if (dc64.d_namlen > MAXNAMLEN) {
88 		errno = ENAMETOOLONG;
89 		return (NULL);
90 	}
91 	(void) strcpy(dc64.d_name, dp->d_name);
92 
93 	return (&dc64);
94 }
95 
96 struct direct *
97 readdir(DIR *dirp)
98 {
99 	if (internal_readdir(dirp) == NULL)
100 		return (NULL);
101 
102 	/* Check for overflows */
103 	if (dc64.d_ino > SIZE_MAX) {
104 		errno = EOVERFLOW;
105 		return (NULL);
106 	}
107 
108 	/* Copy dirent into direct */
109 	dc.d_ino = dc64.d_ino;
110 	dc.d_reclen = dc64.d_reclen - 4;
111 	dc.d_namlen = dc64.d_namlen;
112 	(void) strcpy(dc.d_name, dc64.d_name);
113 
114 	return (&dc);
115 }
116 
117 #if !defined(_LP64)
118 struct direct64 *
119 readdir64(DIR *dirp)
120 {
121 	return (internal_readdir(dirp));
122 }
123 #endif
124