xref: /illumos-gate/usr/src/lib/libc/port/gen/seekdir.c (revision 1da57d55)
17c478bd9Sstevel@tonic-gate /*
27c478bd9Sstevel@tonic-gate  * CDDL HEADER START
37c478bd9Sstevel@tonic-gate  *
47c478bd9Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5494a4c51Sraf  * Common Development and Distribution License (the "License").
6494a4c51Sraf  * You may not use this file except in compliance with the License.
77c478bd9Sstevel@tonic-gate  *
87c478bd9Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
97c478bd9Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
107c478bd9Sstevel@tonic-gate  * See the License for the specific language governing permissions
117c478bd9Sstevel@tonic-gate  * and limitations under the License.
127c478bd9Sstevel@tonic-gate  *
137c478bd9Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
147c478bd9Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
157c478bd9Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
167c478bd9Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
177c478bd9Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
187c478bd9Sstevel@tonic-gate  *
197c478bd9Sstevel@tonic-gate  * CDDL HEADER END
207c478bd9Sstevel@tonic-gate  */
21494a4c51Sraf 
227c478bd9Sstevel@tonic-gate /*
23*7257d1b4Sraf  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
247c478bd9Sstevel@tonic-gate  * Use is subject to license terms.
257c478bd9Sstevel@tonic-gate  */
267c478bd9Sstevel@tonic-gate 
277c478bd9Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
287c478bd9Sstevel@tonic-gate /*	  All Rights Reserved  	*/
297c478bd9Sstevel@tonic-gate 
307c478bd9Sstevel@tonic-gate /*
317c478bd9Sstevel@tonic-gate  * seekdir -- C library extension routine
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include	<sys/feature_tests.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #if !defined(_LP64)
37*7257d1b4Sraf #pragma weak _seekdir64 = seekdir64
387c478bd9Sstevel@tonic-gate #endif
39*7257d1b4Sraf #pragma weak _seekdir = seekdir
407c478bd9Sstevel@tonic-gate 
41*7257d1b4Sraf #include "lint.h"
42494a4c51Sraf #include "libc.h"
43494a4c51Sraf #include <mtlib.h>
44494a4c51Sraf #include <dirent.h>
45494a4c51Sraf #include <fcntl.h>
46494a4c51Sraf #include <unistd.h>
477c478bd9Sstevel@tonic-gate 
487c478bd9Sstevel@tonic-gate #ifdef _LP64
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate void
seekdir(DIR * dirp,long loc)517c478bd9Sstevel@tonic-gate seekdir(DIR *dirp, long loc)
527c478bd9Sstevel@tonic-gate {
53494a4c51Sraf 	private_DIR	*pdirp = (private_DIR *)dirp;
54494a4c51Sraf 	dirent_t	*dp;
557c478bd9Sstevel@tonic-gate 	off_t		off = 0;
567c478bd9Sstevel@tonic-gate 
57494a4c51Sraf 	lmutex_lock(&pdirp->dd_lock);
587c478bd9Sstevel@tonic-gate 	if (lseek(dirp->dd_fd, 0, SEEK_CUR) != 0) {
59494a4c51Sraf 		dp = (dirent_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
607c478bd9Sstevel@tonic-gate 		off = dp->d_off;
617c478bd9Sstevel@tonic-gate 	}
627c478bd9Sstevel@tonic-gate 	if (off != loc) {
637c478bd9Sstevel@tonic-gate 		dirp->dd_loc = 0;
647c478bd9Sstevel@tonic-gate 		(void) lseek(dirp->dd_fd, loc, SEEK_SET);
657c478bd9Sstevel@tonic-gate 		dirp->dd_size = 0;
667c478bd9Sstevel@tonic-gate 
677c478bd9Sstevel@tonic-gate 		/*
687c478bd9Sstevel@tonic-gate 		 * Save seek offset in d_off field, in case telldir
697c478bd9Sstevel@tonic-gate 		 * follows seekdir with no intervening call to readdir
707c478bd9Sstevel@tonic-gate 		 */
71494a4c51Sraf 		((dirent_t *)(uintptr_t)&dirp->dd_buf[0])->d_off = loc;
727c478bd9Sstevel@tonic-gate 	}
73494a4c51Sraf 	lmutex_unlock(&pdirp->dd_lock);
747c478bd9Sstevel@tonic-gate }
757c478bd9Sstevel@tonic-gate 
767c478bd9Sstevel@tonic-gate #else	/* _LP64 */
777c478bd9Sstevel@tonic-gate 
78dfb96a4fSab /*
79dfb96a4fSab  * Note: Instead of making this function static, we reduce it to local
80dfb96a4fSab  * scope in the mapfile. That allows the linker to prevent it from
81dfb96a4fSab  * appearing in the .SUNW_dynsymsort section.
82dfb96a4fSab  */
83dfb96a4fSab void
seekdir64(DIR * dirp,off64_t loc)847c478bd9Sstevel@tonic-gate seekdir64(DIR *dirp, off64_t loc)
857c478bd9Sstevel@tonic-gate {
86494a4c51Sraf 	private_DIR	*pdirp = (private_DIR *)(uintptr_t)dirp;
87494a4c51Sraf 	dirent64_t	*dp64;
887c478bd9Sstevel@tonic-gate 	off64_t		off = 0;
897c478bd9Sstevel@tonic-gate 
90494a4c51Sraf 	lmutex_lock(&pdirp->dd_lock);
917c478bd9Sstevel@tonic-gate 	if (lseek64(dirp->dd_fd, 0, SEEK_CUR) != 0) {
92494a4c51Sraf 		dp64 = (dirent64_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
937c478bd9Sstevel@tonic-gate 		/* was converted by readdir and needs to be reversed */
947c478bd9Sstevel@tonic-gate 		if (dp64->d_ino == (ino64_t)-1) {
95494a4c51Sraf 			dirent_t *dp32;
967c478bd9Sstevel@tonic-gate 
97494a4c51Sraf 			dp32 = (dirent_t *)((uintptr_t)dp64 + sizeof (ino64_t));
987c478bd9Sstevel@tonic-gate 			dp64->d_ino = (ino64_t)dp32->d_ino;
997c478bd9Sstevel@tonic-gate 			dp64->d_off = (off64_t)dp32->d_off;
1007c478bd9Sstevel@tonic-gate 			dp64->d_reclen = (unsigned short)(dp32->d_reclen +
101494a4c51Sraf 			    ((char *)&dp64->d_off - (char *)dp64));
1027c478bd9Sstevel@tonic-gate 		}
1037c478bd9Sstevel@tonic-gate 		off = dp64->d_off;
1047c478bd9Sstevel@tonic-gate 	}
1057c478bd9Sstevel@tonic-gate 	if (off != loc) {
1067c478bd9Sstevel@tonic-gate 		dirp->dd_loc = 0;
1077c478bd9Sstevel@tonic-gate 		(void) lseek64(dirp->dd_fd, loc, SEEK_SET);
1087c478bd9Sstevel@tonic-gate 		dirp->dd_size = 0;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 		/*
1117c478bd9Sstevel@tonic-gate 		 * Save seek offset in d_off field, in case telldir
1127c478bd9Sstevel@tonic-gate 		 * follows seekdir with no intervening call to readdir
1137c478bd9Sstevel@tonic-gate 		 */
114494a4c51Sraf 		((dirent64_t *)(uintptr_t)&dirp->dd_buf[0])->d_off = loc;
1157c478bd9Sstevel@tonic-gate 	}
116494a4c51Sraf 	lmutex_unlock(&pdirp->dd_lock);
1177c478bd9Sstevel@tonic-gate }
1187c478bd9Sstevel@tonic-gate 
1197c478bd9Sstevel@tonic-gate void
seekdir(DIR * dirp,long loc)1207c478bd9Sstevel@tonic-gate seekdir(DIR *dirp, long loc)
1217c478bd9Sstevel@tonic-gate {
122fdc06696Sfvdl 	seekdir64(dirp, (off64_t)(uint32_t)loc);
1237c478bd9Sstevel@tonic-gate }
1247c478bd9Sstevel@tonic-gate 
1257c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
126