* sysdeps/mach/hurd/xstat64.c: Conditionalize entire contents of the
[platform/upstream/glibc.git] / sysdeps / mach / hurd / readdir.c
1 /* Copyright (C) 1993,94,95,96,97,2002 Free Software Foundation, Inc.
2    This file is part of the GNU C Library.
3
4    The GNU C Library is free software; you can redistribute it and/or
5    modify it under the terms of the GNU Lesser General Public
6    License as published by the Free Software Foundation; either
7    version 2.1 of the License, or (at your option) any later version.
8
9    The GNU C Library is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
12    Lesser General Public License for more details.
13
14    You should have received a copy of the GNU Lesser General Public
15    License along with the GNU C Library; if not, write to the Free
16    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
17    02111-1307 USA.  */
18
19 #include <errno.h>
20 #include <stddef.h>
21 #include <dirent.h>
22 #include <unistd.h>
23 #include <endian.h>
24 #include <assert.h>
25
26 /* Read a directory entry from DIRP.  */
27 struct dirent *
28 __readdir (DIR *dirp)
29 {
30   struct dirent64 *entry64 = __readdir64 (dirp);
31
32   if (sizeof (struct dirent64) == sizeof (struct dirent))
33     /* We should in fact just be an alias to readdir64 on this machine.  */
34     return (struct dirent *) entry64;
35
36   /* These are all compile-time constants.  We know that d_ino is the first
37      member and that the layout of the following members matches exactly in
38      both structures.  */
39   assert (offsetof (struct dirent, d_ino) == 0);
40   assert (offsetof (struct dirent64, d_ino) == 0);
41 # define MATCH(memb)                                                          \
42   assert (offsetof (struct dirent64, memb) - sizeof (entry64->d_ino)          \
43           == offsetof (struct dirent, memb) - sizeof (ino_t))
44   MATCH (d_reclen);
45   MATCH (d_type);
46   MATCH (d_namlen);
47 # undef MATCH
48
49   if (entry64 == NULL)
50     return NULL;
51
52   struct dirent *const entry = ((void *) (&entry64->d_ino + 1)
53                                 - sizeof entry->d_ino);
54   const ino_t d_ino = entry64->d_ino;
55   if (d_ino != entry64->d_ino)
56     {
57       __set_errno (EOVERFLOW);
58       return NULL;
59     }
60 # if BYTE_ORDER != BIG_ENDIAN   /* We just skipped over the zero high word.  */
61   entry->d_ino = d_ino; /* ... or the nonzero low word, swap it.  */
62 # endif
63   entry->d_reclen -= sizeof entry64->d_ino - sizeof entry->d_ino;
64   return entry;
65 }
66
67 weak_alias (__readdir, readdir)