xref: /csrg-svn/lib/libc/gen/seekdir.c (revision 36545)
121358Sdist /*
221358Sdist  * Copyright (c) 1983 Regents of the University of California.
334790Sbostic  * All rights reserved.
434790Sbostic  *
534790Sbostic  * Redistribution and use in source and binary forms are permitted
634790Sbostic  * provided that the above copyright notice and this paragraph are
734790Sbostic  * duplicated in all such forms and that any documentation,
834790Sbostic  * advertising materials, and other materials related to such
934790Sbostic  * distribution and use acknowledge that the software was developed
1034790Sbostic  * by the University of California, Berkeley.  The name of the
1134790Sbostic  * University may not be used to endorse or promote products derived
1234790Sbostic  * from this software without specific prior written permission.
1334790Sbostic  * THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR
1434790Sbostic  * IMPLIED WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED
1534790Sbostic  * WARRANTIES OF MERCHANTIBILITY AND FITNESS FOR A PARTICULAR PURPOSE.
1621358Sdist  */
1721358Sdist 
1826583Sdonn #if defined(LIBC_SCCS) && !defined(lint)
19*36545Smckusick static char sccsid[] = "@(#)seekdir.c	5.4 (Berkeley) 01/11/89";
2034790Sbostic #endif /* LIBC_SCCS and not lint */
215752Smckusick 
226370Smckusic #include <sys/param.h>
23*36545Smckusick #include <dirent.h>
245752Smckusick 
255752Smckusick /*
265928Smckusic  * seek to an entry in a directory.
2711636Smckusick  * Only values returned by "telldir" should be passed to seekdir.
285752Smckusick  */
295752Smckusick void
305928Smckusic seekdir(dirp, loc)
315928Smckusic 	register DIR *dirp;
325928Smckusic 	long loc;
335752Smckusick {
345947Smckusic 	long curloc, base, offset;
35*36545Smckusick 	struct dirent *dp;
3611718Smckusick 	extern long lseek();
375947Smckusic 
385947Smckusic 	curloc = telldir(dirp);
395947Smckusic 	if (loc == curloc)
405947Smckusic 		return;
415947Smckusic 	base = loc & ~(DIRBLKSIZ - 1);
425947Smckusic 	offset = loc & (DIRBLKSIZ - 1);
4311718Smckusick 	(void) lseek(dirp->dd_fd, base, 0);
445947Smckusic 	dirp->dd_loc = 0;
455947Smckusic 	while (dirp->dd_loc < offset) {
465947Smckusic 		dp = readdir(dirp);
475947Smckusic 		if (dp == NULL)
485947Smckusic 			return;
495947Smckusic 	}
505752Smckusick }
51