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