Blame sysdeps/mach/hurd/readdir.c

Packit 6c4009
/* Copyright (C) 1993-2018 Free Software Foundation, Inc.
Packit 6c4009
   This file is part of the GNU C Library.
Packit 6c4009
Packit 6c4009
   The GNU C Library is free software; you can redistribute it and/or
Packit 6c4009
   modify it under the terms of the GNU Lesser General Public
Packit 6c4009
   License as published by the Free Software Foundation; either
Packit 6c4009
   version 2.1 of the License, or (at your option) any later version.
Packit 6c4009
Packit 6c4009
   The GNU C Library is distributed in the hope that it will be useful,
Packit 6c4009
   but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit 6c4009
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit 6c4009
   Lesser General Public License for more details.
Packit 6c4009
Packit 6c4009
   You should have received a copy of the GNU Lesser General Public
Packit 6c4009
   License along with the GNU C Library; if not, see
Packit 6c4009
   <http://www.gnu.org/licenses/>.  */
Packit 6c4009
Packit 6c4009
#include <errno.h>
Packit 6c4009
#include <stddef.h>
Packit 6c4009
#include <dirent.h>
Packit 6c4009
#include <unistd.h>
Packit 6c4009
#include <endian.h>
Packit 6c4009
#include <assert.h>
Packit 6c4009
Packit 6c4009
/* Read a directory entry from DIRP.  */
Packit 6c4009
struct dirent *
Packit 6c4009
__readdir (DIR *dirp)
Packit 6c4009
{
Packit 6c4009
  struct dirent64 *entry64 = __readdir64 (dirp);
Packit 6c4009
Packit 6c4009
  if (sizeof (struct dirent64) == sizeof (struct dirent))
Packit 6c4009
    /* We should in fact just be an alias to readdir64 on this machine.  */
Packit 6c4009
    return (struct dirent *) entry64;
Packit 6c4009
Packit 6c4009
  /* These are all compile-time constants.  We know that d_ino is the first
Packit 6c4009
     member and that the layout of the following members matches exactly in
Packit 6c4009
     both structures.  */
Packit 6c4009
  assert (offsetof (struct dirent, d_ino) == 0);
Packit 6c4009
  assert (offsetof (struct dirent64, d_ino) == 0);
Packit 6c4009
# define MATCH(memb)							      \
Packit 6c4009
  assert (offsetof (struct dirent64, memb) - sizeof (entry64->d_ino)	      \
Packit 6c4009
	  == offsetof (struct dirent, memb) - sizeof (ino_t))
Packit 6c4009
  MATCH (d_reclen);
Packit 6c4009
  MATCH (d_type);
Packit 6c4009
  MATCH (d_namlen);
Packit 6c4009
# undef MATCH
Packit 6c4009
Packit 6c4009
  if (entry64 == NULL)
Packit 6c4009
    return NULL;
Packit 6c4009
Packit 6c4009
  struct dirent *const entry = ((void *) (&entry64->d_ino + 1)
Packit 6c4009
				- sizeof entry->d_ino);
Packit 6c4009
  const ino_t d_ino = entry64->d_ino;
Packit 6c4009
  if (d_ino != entry64->d_ino)
Packit 6c4009
    {
Packit 6c4009
      __set_errno (EOVERFLOW);
Packit 6c4009
      return NULL;
Packit 6c4009
    }
Packit 6c4009
# if BYTE_ORDER != BIG_ENDIAN	/* We just skipped over the zero high word.  */
Packit 6c4009
  entry->d_ino = d_ino;	/* ... or the nonzero low word, swap it.  */
Packit 6c4009
# endif
Packit 6c4009
  entry->d_reclen -= sizeof entry64->d_ino - sizeof entry->d_ino;
Packit 6c4009
  return entry;
Packit 6c4009
}
Packit 6c4009
Packit 6c4009
weak_alias (__readdir, readdir)