xref: /titanic_44/usr/src/lib/libc/port/gen/seekdir.c (revision 7257d1b4d25bfac0c802847390e98a464fd787ac)
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 
30*7257d1b4Sraf #pragma ident	"%Z%%M%	%I%	%E% SMI"
31*7257d1b4Sraf 
327c478bd9Sstevel@tonic-gate /*
337c478bd9Sstevel@tonic-gate  * seekdir -- C library extension routine
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include	<sys/feature_tests.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #if !defined(_LP64)
39*7257d1b4Sraf #pragma weak _seekdir64 = seekdir64
407c478bd9Sstevel@tonic-gate #endif
41*7257d1b4Sraf #pragma weak _seekdir = seekdir
427c478bd9Sstevel@tonic-gate 
43*7257d1b4Sraf #include "lint.h"
44494a4c51Sraf #include "libc.h"
457c478bd9Sstevel@tonic-gate #include <mtlib.h>
46494a4c51Sraf #include <dirent.h>
477c478bd9Sstevel@tonic-gate #include <fcntl.h>
487c478bd9Sstevel@tonic-gate #include <unistd.h>
497c478bd9Sstevel@tonic-gate 
507c478bd9Sstevel@tonic-gate #ifdef _LP64
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate void
seekdir(DIR * dirp,long loc)537c478bd9Sstevel@tonic-gate seekdir(DIR *dirp, long loc)
547c478bd9Sstevel@tonic-gate {
55494a4c51Sraf 	private_DIR	*pdirp = (private_DIR *)dirp;
56494a4c51Sraf 	dirent_t	*dp;
577c478bd9Sstevel@tonic-gate 	off_t		off = 0;
587c478bd9Sstevel@tonic-gate 
59494a4c51Sraf 	lmutex_lock(&pdirp->dd_lock);
607c478bd9Sstevel@tonic-gate 	if (lseek(dirp->dd_fd, 0, SEEK_CUR) != 0) {
61494a4c51Sraf 		dp = (dirent_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
627c478bd9Sstevel@tonic-gate 		off = dp->d_off;
637c478bd9Sstevel@tonic-gate 	}
647c478bd9Sstevel@tonic-gate 	if (off != loc) {
657c478bd9Sstevel@tonic-gate 		dirp->dd_loc = 0;
667c478bd9Sstevel@tonic-gate 		(void) lseek(dirp->dd_fd, loc, SEEK_SET);
677c478bd9Sstevel@tonic-gate 		dirp->dd_size = 0;
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate 		/*
707c478bd9Sstevel@tonic-gate 		 * Save seek offset in d_off field, in case telldir
717c478bd9Sstevel@tonic-gate 		 * follows seekdir with no intervening call to readdir
727c478bd9Sstevel@tonic-gate 		 */
73494a4c51Sraf 		((dirent_t *)(uintptr_t)&dirp->dd_buf[0])->d_off = loc;
747c478bd9Sstevel@tonic-gate 	}
75494a4c51Sraf 	lmutex_unlock(&pdirp->dd_lock);
767c478bd9Sstevel@tonic-gate }
777c478bd9Sstevel@tonic-gate 
787c478bd9Sstevel@tonic-gate #else	/* _LP64 */
797c478bd9Sstevel@tonic-gate 
80dfb96a4fSab196087 /*
81dfb96a4fSab196087  * Note: Instead of making this function static, we reduce it to local
82dfb96a4fSab196087  * scope in the mapfile. That allows the linker to prevent it from
83dfb96a4fSab196087  * appearing in the .SUNW_dynsymsort section.
84dfb96a4fSab196087  */
85dfb96a4fSab196087 void
seekdir64(DIR * dirp,off64_t loc)867c478bd9Sstevel@tonic-gate seekdir64(DIR *dirp, off64_t loc)
877c478bd9Sstevel@tonic-gate {
88494a4c51Sraf 	private_DIR	*pdirp = (private_DIR *)(uintptr_t)dirp;
89494a4c51Sraf 	dirent64_t	*dp64;
907c478bd9Sstevel@tonic-gate 	off64_t		off = 0;
917c478bd9Sstevel@tonic-gate 
92494a4c51Sraf 	lmutex_lock(&pdirp->dd_lock);
937c478bd9Sstevel@tonic-gate 	if (lseek64(dirp->dd_fd, 0, SEEK_CUR) != 0) {
94494a4c51Sraf 		dp64 = (dirent64_t *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
957c478bd9Sstevel@tonic-gate 		/* was converted by readdir and needs to be reversed */
967c478bd9Sstevel@tonic-gate 		if (dp64->d_ino == (ino64_t)-1) {
97494a4c51Sraf 			dirent_t *dp32;
987c478bd9Sstevel@tonic-gate 
99494a4c51Sraf 			dp32 = (dirent_t *)((uintptr_t)dp64 + sizeof (ino64_t));
1007c478bd9Sstevel@tonic-gate 			dp64->d_ino = (ino64_t)dp32->d_ino;
1017c478bd9Sstevel@tonic-gate 			dp64->d_off = (off64_t)dp32->d_off;
1027c478bd9Sstevel@tonic-gate 			dp64->d_reclen = (unsigned short)(dp32->d_reclen +
1037c478bd9Sstevel@tonic-gate 			    ((char *)&dp64->d_off - (char *)dp64));
1047c478bd9Sstevel@tonic-gate 		}
1057c478bd9Sstevel@tonic-gate 		off = dp64->d_off;
1067c478bd9Sstevel@tonic-gate 	}
1077c478bd9Sstevel@tonic-gate 	if (off != loc) {
1087c478bd9Sstevel@tonic-gate 		dirp->dd_loc = 0;
1097c478bd9Sstevel@tonic-gate 		(void) lseek64(dirp->dd_fd, loc, SEEK_SET);
1107c478bd9Sstevel@tonic-gate 		dirp->dd_size = 0;
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 		/*
1137c478bd9Sstevel@tonic-gate 		 * Save seek offset in d_off field, in case telldir
1147c478bd9Sstevel@tonic-gate 		 * follows seekdir with no intervening call to readdir
1157c478bd9Sstevel@tonic-gate 		 */
116494a4c51Sraf 		((dirent64_t *)(uintptr_t)&dirp->dd_buf[0])->d_off = loc;
1177c478bd9Sstevel@tonic-gate 	}
118494a4c51Sraf 	lmutex_unlock(&pdirp->dd_lock);
1197c478bd9Sstevel@tonic-gate }
1207c478bd9Sstevel@tonic-gate 
1217c478bd9Sstevel@tonic-gate void
seekdir(DIR * dirp,long loc)1227c478bd9Sstevel@tonic-gate seekdir(DIR *dirp, long loc)
1237c478bd9Sstevel@tonic-gate {
124fdc06696Sfvdl 	seekdir64(dirp, (off64_t)(uint32_t)loc);
1257c478bd9Sstevel@tonic-gate }
1267c478bd9Sstevel@tonic-gate 
1277c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
128