xref: /onnv-gate/usr/src/lib/libc/port/gen/telldir.c (revision 3453:bc3a776612a0)
10Sstevel@tonic-gate /*
20Sstevel@tonic-gate  * CDDL HEADER START
30Sstevel@tonic-gate  *
40Sstevel@tonic-gate  * The contents of this file are subject to the terms of the
5*3453Sraf  * Common Development and Distribution License (the "License").
6*3453Sraf  * You may not use this file except in compliance with the License.
70Sstevel@tonic-gate  *
80Sstevel@tonic-gate  * You can obtain a copy of the license at usr/src/OPENSOLARIS.LICENSE
90Sstevel@tonic-gate  * or http://www.opensolaris.org/os/licensing.
100Sstevel@tonic-gate  * See the License for the specific language governing permissions
110Sstevel@tonic-gate  * and limitations under the License.
120Sstevel@tonic-gate  *
130Sstevel@tonic-gate  * When distributing Covered Code, include this CDDL HEADER in each
140Sstevel@tonic-gate  * file and include the License file at usr/src/OPENSOLARIS.LICENSE.
150Sstevel@tonic-gate  * If applicable, add the following below this CDDL HEADER, with the
160Sstevel@tonic-gate  * fields enclosed by brackets "[]" replaced with your own identifying
170Sstevel@tonic-gate  * information: Portions Copyright [yyyy] [name of copyright owner]
180Sstevel@tonic-gate  *
190Sstevel@tonic-gate  * CDDL HEADER END
200Sstevel@tonic-gate  */
21*3453Sraf 
220Sstevel@tonic-gate /*
23*3453Sraf  * Copyright 2007 Sun Microsystems, Inc.  All rights reserved.
240Sstevel@tonic-gate  * Use is subject to license terms.
250Sstevel@tonic-gate  */
260Sstevel@tonic-gate 
270Sstevel@tonic-gate #pragma ident	"%Z%%M%	%I%	%E% SMI"
280Sstevel@tonic-gate 
290Sstevel@tonic-gate /*	Copyright (c) 1988 AT&T	*/
300Sstevel@tonic-gate /*	  All Rights Reserved  	*/
310Sstevel@tonic-gate 
320Sstevel@tonic-gate /*
330Sstevel@tonic-gate  * telldir -- C library extension routine
340Sstevel@tonic-gate  */
350Sstevel@tonic-gate 
360Sstevel@tonic-gate #include <sys/isa_defs.h>
370Sstevel@tonic-gate 
380Sstevel@tonic-gate #if !defined(_LP64)
390Sstevel@tonic-gate #pragma weak telldir64 = _telldir64
400Sstevel@tonic-gate #endif
410Sstevel@tonic-gate #pragma weak telldir = _telldir
420Sstevel@tonic-gate 
430Sstevel@tonic-gate #include "synonyms.h"
44*3453Sraf #include "libc.h"
450Sstevel@tonic-gate #include <mtlib.h>
460Sstevel@tonic-gate #include <dirent.h>
470Sstevel@tonic-gate #include <errno.h>
480Sstevel@tonic-gate #include <limits.h>
490Sstevel@tonic-gate #include <fcntl.h>
500Sstevel@tonic-gate #include <unistd.h>
510Sstevel@tonic-gate 
520Sstevel@tonic-gate #ifdef _LP64
530Sstevel@tonic-gate 
540Sstevel@tonic-gate long
550Sstevel@tonic-gate telldir(DIR *dirp)
560Sstevel@tonic-gate {
57*3453Sraf 	private_DIR	*pdirp = (private_DIR *)dirp;
58*3453Sraf 	dirent_t	*dp;
59*3453Sraf 	off_t		off = 0;
600Sstevel@tonic-gate 
61*3453Sraf 	lmutex_lock(&pdirp->dd_lock);
620Sstevel@tonic-gate 	/* if at beginning of dir, return 0 */
630Sstevel@tonic-gate 	if (lseek(dirp->dd_fd, 0, SEEK_CUR) != 0) {
64*3453Sraf 		dp = (dirent_t *)(uintptr_t)(&dirp->dd_buf[dirp->dd_loc]);
650Sstevel@tonic-gate 		off = dp->d_off;
660Sstevel@tonic-gate 	}
67*3453Sraf 	lmutex_unlock(&pdirp->dd_lock);
680Sstevel@tonic-gate 	return (off);
690Sstevel@tonic-gate }
700Sstevel@tonic-gate 
710Sstevel@tonic-gate #else
720Sstevel@tonic-gate 
730Sstevel@tonic-gate static off64_t
740Sstevel@tonic-gate telldir64(DIR *dirp)
750Sstevel@tonic-gate {
76*3453Sraf 	private_DIR	*pdirp = (private_DIR *)(uintptr_t)dirp;
77*3453Sraf 	dirent64_t	*dp64;
780Sstevel@tonic-gate 	off64_t		off = 0;
790Sstevel@tonic-gate 
80*3453Sraf 	lmutex_lock(&pdirp->dd_lock);
810Sstevel@tonic-gate 	/* if at beginning of dir, return 0 */
820Sstevel@tonic-gate 	if (lseek64(dirp->dd_fd, 0, SEEK_CUR) != 0) {
83*3453Sraf 		dp64 = (dirent64_t *)(uintptr_t)(&dirp->dd_buf[dirp->dd_loc]);
840Sstevel@tonic-gate 		/* was converted by readdir and needs to be reversed */
850Sstevel@tonic-gate 		if (dp64->d_ino == (ino64_t)-1) {
86*3453Sraf 			dirent_t *dp32;
870Sstevel@tonic-gate 
88*3453Sraf 			dp32 = (dirent_t *)((uintptr_t)dp64 + sizeof (ino64_t));
890Sstevel@tonic-gate 			dp64->d_ino = (ino64_t)dp32->d_ino;
900Sstevel@tonic-gate 			dp64->d_off = (off64_t)dp32->d_off;
910Sstevel@tonic-gate 			dp64->d_reclen = (unsigned short)(dp32->d_reclen +
92*3453Sraf 			    ((char *)&dp64->d_off - (char *)dp64));
930Sstevel@tonic-gate 		}
940Sstevel@tonic-gate 		off = dp64->d_off;
950Sstevel@tonic-gate 	}
96*3453Sraf 	lmutex_unlock(&pdirp->dd_lock);
970Sstevel@tonic-gate 	return (off);
980Sstevel@tonic-gate }
990Sstevel@tonic-gate 
1000Sstevel@tonic-gate long
1010Sstevel@tonic-gate telldir(DIR *dirp)
1020Sstevel@tonic-gate {
1030Sstevel@tonic-gate 	off64_t off;
1040Sstevel@tonic-gate 
1050Sstevel@tonic-gate 	off = telldir64(dirp);
1060Sstevel@tonic-gate 
1070Sstevel@tonic-gate 	/*
1080Sstevel@tonic-gate 	 * Make sure that the offset fits in 32 bits.
1090Sstevel@tonic-gate 	 */
110*3453Sraf 	if ((long)off != off && (uint64_t)off > (uint64_t)UINT32_MAX) {
1110Sstevel@tonic-gate 		errno = EOVERFLOW;
1120Sstevel@tonic-gate 		return (-1);
1130Sstevel@tonic-gate 	}
1140Sstevel@tonic-gate 	return ((long)off);
1150Sstevel@tonic-gate }
1160Sstevel@tonic-gate 
1170Sstevel@tonic-gate #endif	/* _LP64 */
118