xref: /minix3/lib/libc/compat/sys/compat_stat.c (revision 84d9c625bfea59e274550651111ae9edfdc40fbd)
1*84d9c625SLionel Sambuc /*	$NetBSD: compat_stat.c,v 1.4 2013/10/04 21:07:37 christos Exp $	*/
22fe8fb19SBen Gras 
32fe8fb19SBen Gras /*
42fe8fb19SBen Gras  * Copyright (c) 1997 Frank van der Linden
52fe8fb19SBen Gras  * All rights reserved.
62fe8fb19SBen Gras  *
72fe8fb19SBen Gras  * Redistribution and use in source and binary forms, with or without
82fe8fb19SBen Gras  * modification, are permitted provided that the following conditions
92fe8fb19SBen Gras  * are met:
102fe8fb19SBen Gras  * 1. Redistributions of source code must retain the above copyright
112fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer.
122fe8fb19SBen Gras  * 2. Redistributions in binary form must reproduce the above copyright
132fe8fb19SBen Gras  *    notice, this list of conditions and the following disclaimer in the
142fe8fb19SBen Gras  *    documentation and/or other materials provided with the distribution.
152fe8fb19SBen Gras  * 3. All advertising materials mentioning features or use of this software
162fe8fb19SBen Gras  *    must display the following acknowledgement:
172fe8fb19SBen Gras  *      This product includes software developed for the NetBSD Project
182fe8fb19SBen Gras  *      by Frank van der Linden
192fe8fb19SBen Gras  * 4. The name of the author may not be used to endorse or promote products
202fe8fb19SBen Gras  *    derived from this software without specific prior written permission
212fe8fb19SBen Gras  *
222fe8fb19SBen Gras  * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
232fe8fb19SBen Gras  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
242fe8fb19SBen Gras  * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
252fe8fb19SBen Gras  * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
262fe8fb19SBen Gras  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
272fe8fb19SBen Gras  * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
282fe8fb19SBen Gras  * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
292fe8fb19SBen Gras  * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
302fe8fb19SBen Gras  * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
312fe8fb19SBen Gras  * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
322fe8fb19SBen Gras  */
332fe8fb19SBen Gras 
342fe8fb19SBen Gras #include <sys/cdefs.h>
352fe8fb19SBen Gras #if defined(LIBC_SCCS) && !defined(lint)
36*84d9c625SLionel Sambuc __RCSID("$NetBSD: compat_stat.c,v 1.4 2013/10/04 21:07:37 christos Exp $");
372fe8fb19SBen Gras #endif /* LIBC_SCCS and not lint */
382fe8fb19SBen Gras 
392fe8fb19SBen Gras #define __LIBC12_SOURCE__
402fe8fb19SBen Gras 
412fe8fb19SBen Gras #include <sys/types.h>
422fe8fb19SBen Gras #include <sys/stat.h>
432fe8fb19SBen Gras #include <compat/sys/time.h>
442fe8fb19SBen Gras #include <compat/sys/stat.h>
452fe8fb19SBen Gras 
462fe8fb19SBen Gras __warn_references(stat,
472fe8fb19SBen Gras     "warning: reference to compatibility stat(); include <sys/stat.h> to generate correct reference")
482fe8fb19SBen Gras 
492fe8fb19SBen Gras __warn_references(fstat,
502fe8fb19SBen Gras     "warning: reference to compatibility fstat(); include <sys/stat.h> to generate correct reference")
512fe8fb19SBen Gras 
522fe8fb19SBen Gras __warn_references(lstat,
532fe8fb19SBen Gras     "warning: reference to compatibility lstat(); include <sys/stat.h> to generate correct reference")
542fe8fb19SBen Gras 
552fe8fb19SBen Gras /*
562fe8fb19SBen Gras  * Convert from a new to an old stat structure.
572fe8fb19SBen Gras  */
582fe8fb19SBen Gras 
592fe8fb19SBen Gras static void cvtstat(const struct stat *, struct stat12 *);
602fe8fb19SBen Gras 
__strong_alias(stat,__compat_stat)61*84d9c625SLionel Sambuc __strong_alias(stat, __compat_stat)
62*84d9c625SLionel Sambuc __strong_alias(lstat, __compat_lstat)
63*84d9c625SLionel Sambuc __strong_alias(fstat, __compat_fstat)
64*84d9c625SLionel Sambuc 
652fe8fb19SBen Gras static void
662fe8fb19SBen Gras cvtstat(const struct stat *st, struct stat12 *ost)
672fe8fb19SBen Gras {
682fe8fb19SBen Gras 
692fe8fb19SBen Gras 	ost->st_dev = (uint32_t)st->st_dev;
702fe8fb19SBen Gras 	ost->st_ino = (uint32_t)st->st_ino;
712fe8fb19SBen Gras 	ost->st_mode = st->st_mode;
722fe8fb19SBen Gras 	if (st->st_nlink >= (1 << 15))
732fe8fb19SBen Gras 		ost->st_nlink = (1 << 15) - 1;
742fe8fb19SBen Gras 	else
752fe8fb19SBen Gras 		ost->st_nlink = st->st_nlink;
762fe8fb19SBen Gras 	ost->st_uid = st->st_uid;
772fe8fb19SBen Gras 	ost->st_gid = st->st_gid;
782fe8fb19SBen Gras 	ost->st_rdev = (uint32_t)st->st_rdev;
792fe8fb19SBen Gras 	ost->st_atimespec.tv_sec = (int32_t)st->st_atimespec.tv_sec;
802fe8fb19SBen Gras 	ost->st_atimespec.tv_nsec = st->st_atimespec.tv_nsec;
812fe8fb19SBen Gras 	ost->st_mtimespec.tv_sec = (int32_t)st->st_mtimespec.tv_sec;
822fe8fb19SBen Gras 	ost->st_mtimespec.tv_nsec = st->st_mtimespec.tv_nsec;
832fe8fb19SBen Gras 	ost->st_ctimespec.tv_sec = (int32_t)st->st_ctimespec.tv_sec;
842fe8fb19SBen Gras 	ost->st_ctimespec.tv_nsec = st->st_ctimespec.tv_nsec;
852fe8fb19SBen Gras 	ost->st_size = st->st_size;
862fe8fb19SBen Gras 	ost->st_blocks = st->st_blocks;
872fe8fb19SBen Gras 	ost->st_blksize = st->st_blksize;
882fe8fb19SBen Gras 	ost->st_flags = st->st_flags;
892fe8fb19SBen Gras 	ost->st_gen = st->st_gen;
902fe8fb19SBen Gras }
912fe8fb19SBen Gras 
922fe8fb19SBen Gras int
__compat_stat(const char * file,struct stat12 * ost)93*84d9c625SLionel Sambuc __compat_stat(const char *file, struct stat12 *ost)
942fe8fb19SBen Gras {
952fe8fb19SBen Gras 	struct stat nst;
962fe8fb19SBen Gras 	int ret;
972fe8fb19SBen Gras 
982fe8fb19SBen Gras 	if ((ret = __stat50(file, &nst)) < 0)
992fe8fb19SBen Gras 		return ret;
1002fe8fb19SBen Gras 	cvtstat(&nst, ost);
1012fe8fb19SBen Gras 	return ret;
1022fe8fb19SBen Gras }
1032fe8fb19SBen Gras 
1042fe8fb19SBen Gras int
__compat_fstat(int f,struct stat12 * ost)105*84d9c625SLionel Sambuc __compat_fstat(int f, struct stat12 *ost)
1062fe8fb19SBen Gras {
1072fe8fb19SBen Gras 	struct stat nst;
1082fe8fb19SBen Gras 	int ret;
1092fe8fb19SBen Gras 
1102fe8fb19SBen Gras 	if ((ret = __fstat50(f, &nst)) < 0)
1112fe8fb19SBen Gras 		return ret;
1122fe8fb19SBen Gras 	cvtstat(&nst, ost);
1132fe8fb19SBen Gras 	return ret;
1142fe8fb19SBen Gras }
1152fe8fb19SBen Gras 
1162fe8fb19SBen Gras int
__compat_lstat(const char * file,struct stat12 * ost)117*84d9c625SLionel Sambuc __compat_lstat(const char *file, struct stat12 *ost)
1182fe8fb19SBen Gras {
1192fe8fb19SBen Gras 	struct stat nst;
1202fe8fb19SBen Gras 	int ret;
1212fe8fb19SBen Gras 
1222fe8fb19SBen Gras 	if ((ret = __lstat50(file, &nst)) < 0)
1232fe8fb19SBen Gras 		return ret;
1242fe8fb19SBen Gras 	cvtstat(&nst, ost);
1252fe8fb19SBen Gras 	return ret;
1262fe8fb19SBen Gras }
127