]>
Commit | Line | Data |
---|---|---|
b168057a | 1 | /* Copyright (C) 1993-2015 Free Software Foundation, Inc. |
478b92f0 | 2 | This file is part of the GNU C Library. |
28f540f4 | 3 | |
478b92f0 | 4 | The GNU C Library is free software; you can redistribute it and/or |
41bdb6e2 AJ |
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. | |
28f540f4 | 8 | |
478b92f0 UD |
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 | |
41bdb6e2 | 12 | Lesser General Public License for more details. |
28f540f4 | 13 | |
41bdb6e2 | 14 | You should have received a copy of the GNU Lesser General Public |
59ba27a6 PE |
15 | License along with the GNU C Library; if not, see |
16 | <http://www.gnu.org/licenses/>. */ | |
28f540f4 | 17 | |
28f540f4 | 18 | #include <errno.h> |
28f540f4 | 19 | #include <stddef.h> |
28f540f4 RM |
20 | #include <dirent.h> |
21 | #include <unistd.h> | |
337738b7 RM |
22 | #include <endian.h> |
23 | #include <assert.h> | |
28f540f4 RM |
24 | |
25 | /* Read a directory entry from DIRP. */ | |
26 | struct dirent * | |
23396375 | 27 | __readdir (DIR *dirp) |
28f540f4 | 28 | { |
337738b7 RM |
29 | struct dirent64 *entry64 = __readdir64 (dirp); |
30 | ||
31 | if (sizeof (struct dirent64) == sizeof (struct dirent)) | |
32 | /* We should in fact just be an alias to readdir64 on this machine. */ | |
33 | return (struct dirent *) entry64; | |
34 | ||
35 | /* These are all compile-time constants. We know that d_ino is the first | |
36 | member and that the layout of the following members matches exactly in | |
37 | both structures. */ | |
38 | assert (offsetof (struct dirent, d_ino) == 0); | |
39 | assert (offsetof (struct dirent64, d_ino) == 0); | |
40 | # define MATCH(memb) \ | |
41 | assert (offsetof (struct dirent64, memb) - sizeof (entry64->d_ino) \ | |
42 | == offsetof (struct dirent, memb) - sizeof (ino_t)) | |
43 | MATCH (d_reclen); | |
44 | MATCH (d_type); | |
45 | MATCH (d_namlen); | |
46 | # undef MATCH | |
47 | ||
48 | if (entry64 == NULL) | |
49 | return NULL; | |
50 | ||
51 | struct dirent *const entry = ((void *) (&entry64->d_ino + 1) | |
52 | - sizeof entry->d_ino); | |
53 | const ino_t d_ino = entry64->d_ino; | |
54 | if (d_ino != entry64->d_ino) | |
28f540f4 | 55 | { |
337738b7 | 56 | __set_errno (EOVERFLOW); |
28f540f4 RM |
57 | return NULL; |
58 | } | |
337738b7 RM |
59 | # if BYTE_ORDER != BIG_ENDIAN /* We just skipped over the zero high word. */ |
60 | entry->d_ino = d_ino; /* ... or the nonzero low word, swap it. */ | |
61 | # endif | |
62 | entry->d_reclen -= sizeof entry64->d_ino - sizeof entry->d_ino; | |
63 | return entry; | |
28f540f4 | 64 | } |
337738b7 | 65 | |
23396375 | 66 | weak_alias (__readdir, readdir) |