xref: /illumos-gate/usr/src/lib/libc/port/gen/telldir.c (revision 1da57d551424de5a9d469760be7c4b4d4f10a755)
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 (the "License").
6  * You may not use this file except in compliance with the License.
7  *
8  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
9  * or http://www.opensolaris.org/os/licensing.
10  * See the License for the specific language governing permissions
11  * and limitations under the License.
12  *
13  * When distributing Covered Code, include this CDDL HEADER in each
14  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
15  * If applicable, add the following below this CDDL HEADER, with the
16  * fields enclosed by brackets "[]" replaced with your own identifying
17  * information: Portions Copyright [yyyy] [name of copyright owner]
18  *
19  * CDDL HEADER END
20  */
21 
22 /*
23  * Copyright 2008 Sun Microsystems, Inc.  All rights reserved.
24  * Use is subject to license terms.
25  */
26 
27 /*	Copyright (c) 1988 AT&T	*/
28 /*	  All Rights Reserved  	*/
29 
30 /*
31  * telldir -- C library extension routine
32  */
33 
34 #include <sys/isa_defs.h>
35 
36 #if !defined(_LP64)
37 #pragma weak _telldir64 = telldir64
38 #endif
39 #pragma weak _telldir = telldir
40 
41 #include "lint.h"
42 #include "libc.h"
43 #include <mtlib.h>
44 #include <dirent.h>
45 #include <errno.h>
46 #include <limits.h>
47 #include <fcntl.h>
48 #include <unistd.h>
49 
50 #ifdef _LP64
51 
52 long
telldir(DIR * dirp)53 telldir(DIR *dirp)
54 {
55 	private_DIR	*pdirp = (private_DIR *)dirp;
56 	dirent_t	*dp;
57 	off_t		off = 0;
58 
59 	lmutex_lock(&pdirp->dd_lock);
60 	/* if at beginning of dir, return 0 */
61 	if (lseek(dirp->dd_fd, 0, SEEK_CUR) != 0) {
62 		dp = (dirent_t *)(uintptr_t)(&dirp->dd_buf[dirp->dd_loc]);
63 		off = dp->d_off;
64 	}
65 	lmutex_unlock(&pdirp->dd_lock);
66 	return (off);
67 }
68 
69 #else
70 
71 /*
72  * Note: Instead of making this function static, we reduce it to local
73  * scope in the mapfile. That allows the linker to prevent it from
74  * appearing in the .SUNW_dynsymsort section.
75  */
76 off64_t
telldir64(DIR * dirp)77 telldir64(DIR *dirp)
78 {
79 	private_DIR	*pdirp = (private_DIR *)(uintptr_t)dirp;
80 	dirent64_t	*dp64;
81 	off64_t		off = 0;
82 
83 	lmutex_lock(&pdirp->dd_lock);
84 	/* if at beginning of dir, return 0 */
85 	if (lseek64(dirp->dd_fd, 0, SEEK_CUR) != 0) {
86 		dp64 = (dirent64_t *)(uintptr_t)(&dirp->dd_buf[dirp->dd_loc]);
87 		/* was converted by readdir and needs to be reversed */
88 		if (dp64->d_ino == (ino64_t)-1) {
89 			dirent_t *dp32;
90 
91 			dp32 = (dirent_t *)((uintptr_t)dp64 + sizeof (ino64_t));
92 			dp64->d_ino = (ino64_t)dp32->d_ino;
93 			dp64->d_off = (off64_t)dp32->d_off;
94 			dp64->d_reclen = (unsigned short)(dp32->d_reclen +
95 			    ((char *)&dp64->d_off - (char *)dp64));
96 		}
97 		off = dp64->d_off;
98 	}
99 	lmutex_unlock(&pdirp->dd_lock);
100 	return (off);
101 }
102 
103 long
telldir(DIR * dirp)104 telldir(DIR *dirp)
105 {
106 	off64_t off;
107 
108 	off = telldir64(dirp);
109 
110 	/*
111 	 * Make sure that the offset fits in 32 bits.
112 	 */
113 	if ((long)off != off && (uint64_t)off > (uint64_t)UINT32_MAX) {
114 		errno = EOVERFLOW;
115 		return (-1);
116 	}
117 	return ((long)off);
118 }
119 
120 #endif	/* _LP64 */
121