xref: /illumos-gate/usr/src/lib/libc/port/gen/seekdir.c (revision 3db86aab)
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 2004 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 #pragma ident	"%Z%%M%	%I%	%E% SMI"
28 
29 /*	Copyright (c) 1988 AT&T	*/
30 /*	  All Rights Reserved  	*/
31 
32 
33 /*
34  * seekdir -- C library extension routine
35  */
36 
37 #include	<sys/feature_tests.h>
38 
39 #if !defined(_LP64)
40 #pragma weak seekdir64 = _seekdir64
41 #endif
42 #pragma weak seekdir = _seekdir
43 
44 #include	"synonyms.h"
45 #include	<mtlib.h>
46 #include	<sys/types.h>
47 #include	<fcntl.h>
48 #include	<unistd.h>
49 #include	<stdio.h>
50 #include	<dirent.h>
51 #include	<thread.h>
52 #include	<synch.h>
53 
54 
55 extern mutex_t	_dirent_lock;
56 
57 #ifdef _LP64
58 
59 void
60 seekdir(DIR *dirp, long loc)
61 {
62 	struct dirent	*dp;
63 	off_t		off = 0;
64 
65 	lmutex_lock(&_dirent_lock);
66 	if (lseek(dirp->dd_fd, 0, SEEK_CUR) != 0) {
67 		dp = (struct dirent *)(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
68 		off = dp->d_off;
69 	}
70 	if (off != loc) {
71 		dirp->dd_loc = 0;
72 		(void) lseek(dirp->dd_fd, loc, SEEK_SET);
73 		dirp->dd_size = 0;
74 
75 		/*
76 		 * Save seek offset in d_off field, in case telldir
77 		 * follows seekdir with no intervening call to readdir
78 		 */
79 		((struct dirent *)(uintptr_t)&dirp->dd_buf[0])->d_off = loc;
80 	}
81 	lmutex_unlock(&_dirent_lock);
82 }
83 
84 #else	/* _LP64 */
85 
86 static void
87 seekdir64(DIR *dirp, off64_t loc)
88 {
89 	struct dirent64	*dp64;
90 	off64_t		off = 0;
91 
92 	lmutex_lock(&_dirent_lock);
93 	if (lseek64(dirp->dd_fd, 0, SEEK_CUR) != 0) {
94 		dp64 = (struct dirent64 *)
95 			(uintptr_t)&dirp->dd_buf[dirp->dd_loc];
96 		/* was converted by readdir and needs to be reversed */
97 		if (dp64->d_ino == (ino64_t)-1) {
98 			struct dirent	*dp32;
99 
100 			dp32 = (struct dirent *)
101 			    ((uintptr_t)dp64 + sizeof (ino64_t));
102 			dp64->d_ino = (ino64_t)dp32->d_ino;
103 			dp64->d_off = (off64_t)dp32->d_off;
104 			dp64->d_reclen = (unsigned short)(dp32->d_reclen +
105 				((char *)&dp64->d_off - (char *)dp64));
106 		}
107 		off = dp64->d_off;
108 	}
109 	if (off != loc) {
110 		dirp->dd_loc = 0;
111 		(void) lseek64(dirp->dd_fd, loc, SEEK_SET);
112 		dirp->dd_size = 0;
113 
114 		/*
115 		 * Save seek offset in d_off field, in case telldir
116 		 * follows seekdir with no intervening call to readdir
117 		 */
118 		((struct dirent64 *)(uintptr_t)&dirp->dd_buf[0])->d_off = loc;
119 	}
120 	lmutex_unlock(&_dirent_lock);
121 }
122 
123 void
124 seekdir(DIR *dirp, long loc)
125 {
126 	seekdir64(dirp, (off64_t)loc);
127 }
128 
129 #endif	/* _LP64 */
130