1*3e39914dSjoerg /* $NetBSD: compat_getdents.c,v 1.6 2012/03/13 22:24:48 joerg Exp $ */
25b84b398Schristos
35b84b398Schristos /*-
45b84b398Schristos * Copyright (c) 2005 The NetBSD Foundation, Inc.
55b84b398Schristos * All rights reserved.
65b84b398Schristos *
75b84b398Schristos * This code is derived from software contributed to The NetBSD Foundation
85b84b398Schristos * by Christos Zoulas.
95b84b398Schristos *
105b84b398Schristos * Redistribution and use in source and binary forms, with or without
115b84b398Schristos * modification, are permitted provided that the following conditions
125b84b398Schristos * are met:
135b84b398Schristos * 1. Redistributions of source code must retain the above copyright
145b84b398Schristos * notice, this list of conditions and the following disclaimer.
155b84b398Schristos * 2. Redistributions in binary form must reproduce the above copyright
165b84b398Schristos * notice, this list of conditions and the following disclaimer in the
175b84b398Schristos * documentation and/or other materials provided with the distribution.
185b84b398Schristos *
195b84b398Schristos * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
205b84b398Schristos * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
215b84b398Schristos * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
225b84b398Schristos * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
235b84b398Schristos * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
245b84b398Schristos * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
255b84b398Schristos * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
265b84b398Schristos * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
275b84b398Schristos * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
285b84b398Schristos * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
295b84b398Schristos * POSSIBILITY OF SUCH DAMAGE.
305b84b398Schristos */
315b84b398Schristos
325b84b398Schristos #include <sys/cdefs.h>
335b84b398Schristos #if defined(LIBC_SCCS) && !defined(lint)
34*3e39914dSjoerg __RCSID("$NetBSD: compat_getdents.c,v 1.6 2012/03/13 22:24:48 joerg Exp $");
355b84b398Schristos #endif /* LIBC_SCCS and not lint */
365b84b398Schristos
375b84b398Schristos #define __LIBC12_SOURCE__
385b84b398Schristos
395b84b398Schristos #include "namespace.h"
405b84b398Schristos #include <sys/types.h>
41*3e39914dSjoerg #include <assert.h>
425b84b398Schristos #include <dirent.h>
43*3e39914dSjoerg #include <stddef.h>
445b84b398Schristos #include <compat/include/dirent.h>
455b84b398Schristos #include <string.h>
465b84b398Schristos
475b84b398Schristos /*
485b84b398Schristos * libc12 compatible getdents routine.
495b84b398Schristos */
505b84b398Schristos int
getdents(int fd,char * buf,size_t nbytes)515b84b398Schristos getdents(int fd, char *buf, size_t nbytes)
525b84b398Schristos {
535b84b398Schristos struct dirent *ndp, *nndp, *endp;
545b84b398Schristos struct dirent12 *odp;
55493d3410Smrg ino_t ino;
565b84b398Schristos int rv;
57c5e820caSchristos size_t len;
585b84b398Schristos
595b84b398Schristos if ((rv = __getdents30(fd, buf, nbytes)) == -1)
605b84b398Schristos return rv;
615b84b398Schristos
625b84b398Schristos odp = (struct dirent12 *)(void *)buf;
635b84b398Schristos ndp = (struct dirent *)(void *)buf;
645b84b398Schristos endp = (struct dirent *)(void *)&buf[rv];
655b84b398Schristos
665b84b398Schristos /*
675b84b398Schristos * In-place conversion. This works because odp
685b84b398Schristos * is smaller than ndp, but it has to be done
695b84b398Schristos * in the right sequence.
705b84b398Schristos */
715b84b398Schristos for (; ndp < endp; ndp = nndp) {
725b84b398Schristos nndp = _DIRENT_NEXT(ndp);
73b0cfe41eSnakayama /* XXX: avoid unaligned 64-bit access on sparc64 */
74493d3410Smrg /* XXX: does this work? */
75493d3410Smrg memcpy(&ino, &ndp->d_ino, sizeof(ino_t));
76493d3410Smrg odp->d_ino = (uint32_t)ino;
775b84b398Schristos if (ndp->d_namlen >= sizeof(odp->d_name))
785b84b398Schristos odp->d_namlen = sizeof(odp->d_name) - 1;
795b84b398Schristos else
805b84b398Schristos odp->d_namlen = (u_int8_t)ndp->d_namlen;
815b84b398Schristos odp->d_type = ndp->d_type;
825b84b398Schristos (void)memcpy(odp->d_name, ndp->d_name, (size_t)odp->d_namlen);
835b84b398Schristos odp->d_name[odp->d_namlen] = '\0';
84c5e820caSchristos len = _DIRENT_SIZE(odp);
85c5e820caSchristos _DIAGASSERT(__type_fit(uint16_t, len));
86c5e820caSchristos odp->d_reclen = (uint16_t)len;
875b84b398Schristos odp = _DIRENT_NEXT(odp);
885b84b398Schristos }
89c5e820caSchristos ptrdiff_t td = (((char *)(void *)odp) - buf);
90c5e820caSchristos _DIAGASSERT(__type_fit(int, td));
91c5e820caSchristos return (int)td;
925b84b398Schristos }
93