xref: /illumos-gate/usr/src/lib/libc/port/gen/telldir.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  * telldir -- C library extension routine
327c478bd9Sstevel@tonic-gate  */
337c478bd9Sstevel@tonic-gate 
347c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #if !defined(_LP64)
37*7257d1b4Sraf #pragma weak _telldir64 = telldir64
387c478bd9Sstevel@tonic-gate #endif
39*7257d1b4Sraf #pragma weak _telldir = telldir
407c478bd9Sstevel@tonic-gate 
41*7257d1b4Sraf #include "lint.h"
42494a4c51Sraf #include "libc.h"
437c478bd9Sstevel@tonic-gate #include <mtlib.h>
447c478bd9Sstevel@tonic-gate #include <dirent.h>
457c478bd9Sstevel@tonic-gate #include <errno.h>
467c478bd9Sstevel@tonic-gate #include <limits.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 long
telldir(DIR * dirp)537c478bd9Sstevel@tonic-gate telldir(DIR *dirp)
547c478bd9Sstevel@tonic-gate {
55494a4c51Sraf 	private_DIR	*pdirp = (private_DIR *)dirp;
56494a4c51Sraf 	dirent_t	*dp;
57494a4c51Sraf 	off_t		off = 0;
587c478bd9Sstevel@tonic-gate 
59494a4c51Sraf 	lmutex_lock(&pdirp->dd_lock);
607c478bd9Sstevel@tonic-gate 	/* if at beginning of dir, return 0 */
617c478bd9Sstevel@tonic-gate 	if (lseek(dirp->dd_fd, 0, SEEK_CUR) != 0) {
62494a4c51Sraf 		dp = (dirent_t *)(uintptr_t)(&dirp->dd_buf[dirp->dd_loc]);
637c478bd9Sstevel@tonic-gate 		off = dp->d_off;
647c478bd9Sstevel@tonic-gate 	}
65494a4c51Sraf 	lmutex_unlock(&pdirp->dd_lock);
667c478bd9Sstevel@tonic-gate 	return (off);
677c478bd9Sstevel@tonic-gate }
687c478bd9Sstevel@tonic-gate 
697c478bd9Sstevel@tonic-gate #else
707c478bd9Sstevel@tonic-gate 
71dfb96a4fSab /*
72dfb96a4fSab  * Note: Instead of making this function static, we reduce it to local
73dfb96a4fSab  * scope in the mapfile. That allows the linker to prevent it from
74dfb96a4fSab  * appearing in the .SUNW_dynsymsort section.
75dfb96a4fSab  */
76dfb96a4fSab off64_t
telldir64(DIR * dirp)777c478bd9Sstevel@tonic-gate telldir64(DIR *dirp)
787c478bd9Sstevel@tonic-gate {
79494a4c51Sraf 	private_DIR	*pdirp = (private_DIR *)(uintptr_t)dirp;
80494a4c51Sraf 	dirent64_t	*dp64;
817c478bd9Sstevel@tonic-gate 	off64_t		off = 0;
827c478bd9Sstevel@tonic-gate 
83494a4c51Sraf 	lmutex_lock(&pdirp->dd_lock);
847c478bd9Sstevel@tonic-gate 	/* if at beginning of dir, return 0 */
857c478bd9Sstevel@tonic-gate 	if (lseek64(dirp->dd_fd, 0, SEEK_CUR) != 0) {
86494a4c51Sraf 		dp64 = (dirent64_t *)(uintptr_t)(&dirp->dd_buf[dirp->dd_loc]);
877c478bd9Sstevel@tonic-gate 		/* was converted by readdir and needs to be reversed */
887c478bd9Sstevel@tonic-gate 		if (dp64->d_ino == (ino64_t)-1) {
89494a4c51Sraf 			dirent_t *dp32;
907c478bd9Sstevel@tonic-gate 
91494a4c51Sraf 			dp32 = (dirent_t *)((uintptr_t)dp64 + sizeof (ino64_t));
927c478bd9Sstevel@tonic-gate 			dp64->d_ino = (ino64_t)dp32->d_ino;
937c478bd9Sstevel@tonic-gate 			dp64->d_off = (off64_t)dp32->d_off;
947c478bd9Sstevel@tonic-gate 			dp64->d_reclen = (unsigned short)(dp32->d_reclen +
95494a4c51Sraf 			    ((char *)&dp64->d_off - (char *)dp64));
967c478bd9Sstevel@tonic-gate 		}
977c478bd9Sstevel@tonic-gate 		off = dp64->d_off;
987c478bd9Sstevel@tonic-gate 	}
99494a4c51Sraf 	lmutex_unlock(&pdirp->dd_lock);
1007c478bd9Sstevel@tonic-gate 	return (off);
1017c478bd9Sstevel@tonic-gate }
1027c478bd9Sstevel@tonic-gate 
1037c478bd9Sstevel@tonic-gate long
telldir(DIR * dirp)1047c478bd9Sstevel@tonic-gate telldir(DIR *dirp)
1057c478bd9Sstevel@tonic-gate {
1067c478bd9Sstevel@tonic-gate 	off64_t off;
1077c478bd9Sstevel@tonic-gate 
1087c478bd9Sstevel@tonic-gate 	off = telldir64(dirp);
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	/*
1117c478bd9Sstevel@tonic-gate 	 * Make sure that the offset fits in 32 bits.
1127c478bd9Sstevel@tonic-gate 	 */
113494a4c51Sraf 	if ((long)off != off && (uint64_t)off > (uint64_t)UINT32_MAX) {
1147c478bd9Sstevel@tonic-gate 		errno = EOVERFLOW;
1157c478bd9Sstevel@tonic-gate 		return (-1);
1167c478bd9Sstevel@tonic-gate 	}
1177c478bd9Sstevel@tonic-gate 	return ((long)off);
1187c478bd9Sstevel@tonic-gate }
1197c478bd9Sstevel@tonic-gate 
1207c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
121