xref: /titanic_50/usr/src/lib/libc/port/gen/telldir.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  * telldir -- C library extension routine
347c478bd9Sstevel@tonic-gate  */
357c478bd9Sstevel@tonic-gate 
367c478bd9Sstevel@tonic-gate #include <sys/isa_defs.h>
377c478bd9Sstevel@tonic-gate 
387c478bd9Sstevel@tonic-gate #if !defined(_LP64)
39*7257d1b4Sraf #pragma weak _telldir64 = telldir64
407c478bd9Sstevel@tonic-gate #endif
41*7257d1b4Sraf #pragma weak _telldir = telldir
427c478bd9Sstevel@tonic-gate 
43*7257d1b4Sraf #include "lint.h"
44494a4c51Sraf #include "libc.h"
457c478bd9Sstevel@tonic-gate #include <mtlib.h>
467c478bd9Sstevel@tonic-gate #include <dirent.h>
477c478bd9Sstevel@tonic-gate #include <errno.h>
487c478bd9Sstevel@tonic-gate #include <limits.h>
497c478bd9Sstevel@tonic-gate #include <fcntl.h>
507c478bd9Sstevel@tonic-gate #include <unistd.h>
517c478bd9Sstevel@tonic-gate 
527c478bd9Sstevel@tonic-gate #ifdef _LP64
537c478bd9Sstevel@tonic-gate 
547c478bd9Sstevel@tonic-gate long
telldir(DIR * dirp)557c478bd9Sstevel@tonic-gate telldir(DIR *dirp)
567c478bd9Sstevel@tonic-gate {
57494a4c51Sraf 	private_DIR	*pdirp = (private_DIR *)dirp;
58494a4c51Sraf 	dirent_t	*dp;
597c478bd9Sstevel@tonic-gate 	off_t		off = 0;
607c478bd9Sstevel@tonic-gate 
61494a4c51Sraf 	lmutex_lock(&pdirp->dd_lock);
627c478bd9Sstevel@tonic-gate 	/* if at beginning of dir, return 0 */
637c478bd9Sstevel@tonic-gate 	if (lseek(dirp->dd_fd, 0, SEEK_CUR) != 0) {
64494a4c51Sraf 		dp = (dirent_t *)(uintptr_t)(&dirp->dd_buf[dirp->dd_loc]);
657c478bd9Sstevel@tonic-gate 		off = dp->d_off;
667c478bd9Sstevel@tonic-gate 	}
67494a4c51Sraf 	lmutex_unlock(&pdirp->dd_lock);
687c478bd9Sstevel@tonic-gate 	return (off);
697c478bd9Sstevel@tonic-gate }
707c478bd9Sstevel@tonic-gate 
717c478bd9Sstevel@tonic-gate #else
727c478bd9Sstevel@tonic-gate 
73dfb96a4fSab196087 /*
74dfb96a4fSab196087  * Note: Instead of making this function static, we reduce it to local
75dfb96a4fSab196087  * scope in the mapfile. That allows the linker to prevent it from
76dfb96a4fSab196087  * appearing in the .SUNW_dynsymsort section.
77dfb96a4fSab196087  */
78dfb96a4fSab196087 off64_t
telldir64(DIR * dirp)797c478bd9Sstevel@tonic-gate telldir64(DIR *dirp)
807c478bd9Sstevel@tonic-gate {
81494a4c51Sraf 	private_DIR	*pdirp = (private_DIR *)(uintptr_t)dirp;
82494a4c51Sraf 	dirent64_t	*dp64;
837c478bd9Sstevel@tonic-gate 	off64_t		off = 0;
847c478bd9Sstevel@tonic-gate 
85494a4c51Sraf 	lmutex_lock(&pdirp->dd_lock);
867c478bd9Sstevel@tonic-gate 	/* if at beginning of dir, return 0 */
877c478bd9Sstevel@tonic-gate 	if (lseek64(dirp->dd_fd, 0, SEEK_CUR) != 0) {
88494a4c51Sraf 		dp64 = (dirent64_t *)(uintptr_t)(&dirp->dd_buf[dirp->dd_loc]);
897c478bd9Sstevel@tonic-gate 		/* was converted by readdir and needs to be reversed */
907c478bd9Sstevel@tonic-gate 		if (dp64->d_ino == (ino64_t)-1) {
91494a4c51Sraf 			dirent_t *dp32;
927c478bd9Sstevel@tonic-gate 
93494a4c51Sraf 			dp32 = (dirent_t *)((uintptr_t)dp64 + sizeof (ino64_t));
947c478bd9Sstevel@tonic-gate 			dp64->d_ino = (ino64_t)dp32->d_ino;
957c478bd9Sstevel@tonic-gate 			dp64->d_off = (off64_t)dp32->d_off;
967c478bd9Sstevel@tonic-gate 			dp64->d_reclen = (unsigned short)(dp32->d_reclen +
977c478bd9Sstevel@tonic-gate 			    ((char *)&dp64->d_off - (char *)dp64));
987c478bd9Sstevel@tonic-gate 		}
997c478bd9Sstevel@tonic-gate 		off = dp64->d_off;
1007c478bd9Sstevel@tonic-gate 	}
101494a4c51Sraf 	lmutex_unlock(&pdirp->dd_lock);
1027c478bd9Sstevel@tonic-gate 	return (off);
1037c478bd9Sstevel@tonic-gate }
1047c478bd9Sstevel@tonic-gate 
1057c478bd9Sstevel@tonic-gate long
telldir(DIR * dirp)1067c478bd9Sstevel@tonic-gate telldir(DIR *dirp)
1077c478bd9Sstevel@tonic-gate {
1087c478bd9Sstevel@tonic-gate 	off64_t off;
1097c478bd9Sstevel@tonic-gate 
1107c478bd9Sstevel@tonic-gate 	off = telldir64(dirp);
1117c478bd9Sstevel@tonic-gate 
1127c478bd9Sstevel@tonic-gate 	/*
1137c478bd9Sstevel@tonic-gate 	 * Make sure that the offset fits in 32 bits.
1147c478bd9Sstevel@tonic-gate 	 */
115494a4c51Sraf 	if ((long)off != off && (uint64_t)off > (uint64_t)UINT32_MAX) {
1167c478bd9Sstevel@tonic-gate 		errno = EOVERFLOW;
1177c478bd9Sstevel@tonic-gate 		return (-1);
1187c478bd9Sstevel@tonic-gate 	}
1197c478bd9Sstevel@tonic-gate 	return ((long)off);
1207c478bd9Sstevel@tonic-gate }
1217c478bd9Sstevel@tonic-gate 
1227c478bd9Sstevel@tonic-gate #endif	/* _LP64 */
123