xref: /titanic_44/usr/src/lib/libc/port/gen/telldir.c (revision 3db86aab554edbb4244c8d1a1c90f152eee768af)
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  * telldir -- C library extension routine
35  */
36 
37 #include <sys/isa_defs.h>
38 
39 #if !defined(_LP64)
40 #pragma weak telldir64 = _telldir64
41 #endif
42 #pragma weak telldir = _telldir
43 
44 #include "synonyms.h"
45 #include <mtlib.h>
46 #include <sys/types.h>
47 #include <dirent.h>
48 #include <thread.h>
49 #include <errno.h>
50 #include <limits.h>
51 #include <synch.h>
52 #include <fcntl.h>
53 #include <stdio.h>
54 #include <unistd.h>
55 #include <inttypes.h>
56 
57 extern mutex_t	_dirent_lock;
58 
59 #ifdef _LP64
60 
61 long
62 telldir(DIR *dirp)
63 {
64 	struct dirent	*dp;
65 	off_t off = 0;
66 
67 	lmutex_lock(&_dirent_lock);
68 	/* if at beginning of dir, return 0 */
69 	if (lseek(dirp->dd_fd, 0, SEEK_CUR) != 0) {
70 		dp = (struct dirent *)(uintptr_t)(&dirp->dd_buf[dirp->dd_loc]);
71 		off = dp->d_off;
72 	}
73 	lmutex_unlock(&_dirent_lock);
74 	return (off);
75 }
76 
77 #else
78 
79 static off64_t
80 telldir64(DIR *dirp)
81 {
82 	struct dirent64	*dp64;
83 	off64_t		off = 0;
84 
85 	lmutex_lock(&_dirent_lock);
86 	/* if at beginning of dir, return 0 */
87 	if (lseek64(dirp->dd_fd, 0, SEEK_CUR) != 0) {
88 		dp64 = (struct dirent64 *)
89 			(uintptr_t)(&dirp->dd_buf[dirp->dd_loc]);
90 		/* was converted by readdir and needs to be reversed */
91 		if (dp64->d_ino == (ino64_t)-1) {
92 			struct dirent	*dp32;
93 
94 			dp32 = (struct dirent *)
95 			    ((uintptr_t)dp64 + sizeof (ino64_t));
96 			dp64->d_ino = (ino64_t)dp32->d_ino;
97 			dp64->d_off = (off64_t)dp32->d_off;
98 			dp64->d_reclen = (unsigned short)(dp32->d_reclen +
99 				((char *)&dp64->d_off - (char *)dp64));
100 		}
101 		off = dp64->d_off;
102 	}
103 	lmutex_unlock(&_dirent_lock);
104 	return (off);
105 }
106 
107 long
108 telldir(DIR *dirp)
109 {
110 	off64_t off;
111 
112 	off = telldir64(dirp);
113 
114 	/*
115 	 * Make sure that the offset fits in 32 bits.
116 	 */
117 	if ((long)off != off &&
118 		(uint64_t)off > (uint64_t)UINT32_MAX) {
119 		errno = EOVERFLOW;
120 		return (-1);
121 	}
122 	return ((long)off);
123 }
124 
125 #endif	/* _LP64 */
126