xref: /titanic_41/usr/src/lib/libbc/libc/sys/common/getdents.c (revision 7c478bd95313f5f23a4c958a745db2134aa03244)
1*7c478bd9Sstevel@tonic-gate /*
2*7c478bd9Sstevel@tonic-gate  * CDDL HEADER START
3*7c478bd9Sstevel@tonic-gate  *
4*7c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*7c478bd9Sstevel@tonic-gate  * Common Development and Distribution License, Version 1.0 only
6*7c478bd9Sstevel@tonic-gate  * (the "License").  You may not use this file except in compliance
7*7c478bd9Sstevel@tonic-gate  * with the License.
8*7c478bd9Sstevel@tonic-gate  *
9*7c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
10*7c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
11*7c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
12*7c478bd9Sstevel@tonic-gate  * and limitations under the License.
13*7c478bd9Sstevel@tonic-gate  *
14*7c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
15*7c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
16*7c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
17*7c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
18*7c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
19*7c478bd9Sstevel@tonic-gate  *
20*7c478bd9Sstevel@tonic-gate  * CDDL HEADER END
21*7c478bd9Sstevel@tonic-gate  */
22*7c478bd9Sstevel@tonic-gate /*
23*7c478bd9Sstevel@tonic-gate  * Copyright 1994 Sun Microsystems, Inc.  All rights reserved.
24*7c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
25*7c478bd9Sstevel@tonic-gate  */
26*7c478bd9Sstevel@tonic-gate 
27*7c478bd9Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
28*7c478bd9Sstevel@tonic-gate 
29*7c478bd9Sstevel@tonic-gate #include <unistd.h>
30*7c478bd9Sstevel@tonic-gate #include <sys/types.h>
31*7c478bd9Sstevel@tonic-gate #include <sys/dirent.h>
32*7c478bd9Sstevel@tonic-gate #include <sys/syscall.h>
33*7c478bd9Sstevel@tonic-gate #include <sys/param.h>
34*7c478bd9Sstevel@tonic-gate #include <sys/errno.h>
35*7c478bd9Sstevel@tonic-gate 
36*7c478bd9Sstevel@tonic-gate #define	ALIGN	4
37*7c478bd9Sstevel@tonic-gate 
38*7c478bd9Sstevel@tonic-gate extern int errno;
39*7c478bd9Sstevel@tonic-gate 
40*7c478bd9Sstevel@tonic-gate struct n_dirent{
41*7c478bd9Sstevel@tonic-gate 	unsigned long	d_ino;
42*7c478bd9Sstevel@tonic-gate 	long		d_off;
43*7c478bd9Sstevel@tonic-gate 	unsigned short	d_reclen;
44*7c478bd9Sstevel@tonic-gate 	char		d_name[1];
45*7c478bd9Sstevel@tonic-gate };
46*7c478bd9Sstevel@tonic-gate 
47*7c478bd9Sstevel@tonic-gate 
getdents(fd,buf,bytes)48*7c478bd9Sstevel@tonic-gate int getdents(fd, buf, bytes)
49*7c478bd9Sstevel@tonic-gate int fd;
50*7c478bd9Sstevel@tonic-gate char *buf;
51*7c478bd9Sstevel@tonic-gate int bytes;
52*7c478bd9Sstevel@tonic-gate {
53*7c478bd9Sstevel@tonic-gate 	return(bc_getdents(fd, buf, bytes));
54*7c478bd9Sstevel@tonic-gate }
55*7c478bd9Sstevel@tonic-gate 
56*7c478bd9Sstevel@tonic-gate 
bc_getdents(fd,buf,bytes)57*7c478bd9Sstevel@tonic-gate int bc_getdents(fd, buf, bytes)
58*7c478bd9Sstevel@tonic-gate int fd;
59*7c478bd9Sstevel@tonic-gate char *buf;
60*7c478bd9Sstevel@tonic-gate int bytes;
61*7c478bd9Sstevel@tonic-gate {
62*7c478bd9Sstevel@tonic-gate 	int ret, nbytes;
63*7c478bd9Sstevel@tonic-gate 	char *nbuf;
64*7c478bd9Sstevel@tonic-gate 	struct dirent *dir;
65*7c478bd9Sstevel@tonic-gate 	struct n_dirent *ndir;
66*7c478bd9Sstevel@tonic-gate 	int count=0;
67*7c478bd9Sstevel@tonic-gate 	off_t last_off;
68*7c478bd9Sstevel@tonic-gate 
69*7c478bd9Sstevel@tonic-gate 	if (buf == (char *)0 || buf == (char *)-1) {
70*7c478bd9Sstevel@tonic-gate 		errno = EFAULT;
71*7c478bd9Sstevel@tonic-gate 		return(-1);
72*7c478bd9Sstevel@tonic-gate 	}
73*7c478bd9Sstevel@tonic-gate 
74*7c478bd9Sstevel@tonic-gate 	nbytes = bytes; /* buffer can only be as large as user expects */
75*7c478bd9Sstevel@tonic-gate 
76*7c478bd9Sstevel@tonic-gate 	if ((nbuf = (char *)malloc(nbytes)) == NULL) {
77*7c478bd9Sstevel@tonic-gate 		return(-1);
78*7c478bd9Sstevel@tonic-gate 	}
79*7c478bd9Sstevel@tonic-gate 
80*7c478bd9Sstevel@tonic-gate 	if ((ret = _syscall(SYS_getdents, fd, nbuf, nbytes)) == -1) {
81*7c478bd9Sstevel@tonic-gate 		free(nbuf);
82*7c478bd9Sstevel@tonic-gate 		return(ret);
83*7c478bd9Sstevel@tonic-gate 	}
84*7c478bd9Sstevel@tonic-gate 
85*7c478bd9Sstevel@tonic-gate 
86*7c478bd9Sstevel@tonic-gate 	dir = (struct dirent *)buf;
87*7c478bd9Sstevel@tonic-gate 	ndir = (struct n_dirent *)nbuf;	/* source directory format */
88*7c478bd9Sstevel@tonic-gate 
89*7c478bd9Sstevel@tonic-gate 	while ((((int)(((char *)dir) + sizeof(struct n_dirent) +
90*7c478bd9Sstevel@tonic-gate 	    strlen(ndir->d_name) + ALIGN) & ~(ALIGN - 1)) <
91*7c478bd9Sstevel@tonic-gate 	    (int)(buf + bytes)) &&
92*7c478bd9Sstevel@tonic-gate 	    ((char *)ndir + sizeof(struct n_dirent) <= (nbuf + ret))) {
93*7c478bd9Sstevel@tonic-gate 		dir->d_off = ndir->d_off;
94*7c478bd9Sstevel@tonic-gate 		dir->d_fileno = ndir->d_ino;	/* NOT VALID */
95*7c478bd9Sstevel@tonic-gate 		dir->d_namlen = strlen(ndir->d_name);
96*7c478bd9Sstevel@tonic-gate 		dir->d_reclen = (short)((sizeof(struct dirent) - MAXNAMLEN +
97*7c478bd9Sstevel@tonic-gate 		    dir->d_namlen + ALIGN) & -ALIGN);
98*7c478bd9Sstevel@tonic-gate 		strncpy(dir->d_name, ndir->d_name, dir->d_namlen);
99*7c478bd9Sstevel@tonic-gate 		dir->d_name[dir->d_namlen] = '\0';
100*7c478bd9Sstevel@tonic-gate 		count += dir->d_reclen;
101*7c478bd9Sstevel@tonic-gate 		last_off = ndir->d_off;
102*7c478bd9Sstevel@tonic-gate 		dir  = (struct dirent *)((char *)dir +
103*7c478bd9Sstevel@tonic-gate 				    ((int)( dir->d_reclen)));
104*7c478bd9Sstevel@tonic-gate 		ndir = (struct n_dirent *)((char *)ndir +
105*7c478bd9Sstevel@tonic-gate 				    ((int)(ndir->d_reclen)));
106*7c478bd9Sstevel@tonic-gate 	}
107*7c478bd9Sstevel@tonic-gate 
108*7c478bd9Sstevel@tonic-gate 	/*
109*7c478bd9Sstevel@tonic-gate 	 * Seek to the next entry in the directory. If all entries
110*7c478bd9Sstevel@tonic-gate 	 * in ndir were not copied to dir, the next getdents syscall
111*7c478bd9Sstevel@tonic-gate 	 * will start reading from there.
112*7c478bd9Sstevel@tonic-gate 	 */
113*7c478bd9Sstevel@tonic-gate 	(void)lseek(fd, last_off, SEEK_SET);
114*7c478bd9Sstevel@tonic-gate 	free(nbuf);
115*7c478bd9Sstevel@tonic-gate 	return(count);
116*7c478bd9Sstevel@tonic-gate }
117