Remove *xattr syscalls.
[glibc.git] / sysdeps / unix / readdir.c
blobdc015d2802b9a9f2bc0834619a9ed464a4aefb0b
1 /* Copyright (C) 1991,92,93,94,95,96,97,99,2000 Free Software Foundation, Inc.
2 This file is part of the GNU C Library.
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.
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.
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. */
19 #include <errno.h>
20 #include <limits.h>
21 #include <stddef.h>
22 #include <string.h>
23 #include <dirent.h>
24 #include <unistd.h>
25 #include <sys/types.h>
26 #include <assert.h>
28 #include <dirstream.h>
30 #ifndef __READDIR
31 # define __READDIR __readdir
32 # define __GETDENTS __getdents
33 # define DIRENT_TYPE struct dirent
34 # define __READDIR_ALIAS
35 #endif
37 /* Read a directory entry from DIRP. */
38 DIRENT_TYPE *
39 __READDIR (DIR *dirp)
41 DIRENT_TYPE *dp;
42 int saved_errno = errno;
44 __libc_lock_lock (dirp->lock);
48 size_t reclen;
50 if (dirp->offset >= dirp->size)
52 /* We've emptied out our buffer. Refill it. */
54 size_t maxread;
55 ssize_t bytes;
57 #ifndef _DIRENT_HAVE_D_RECLEN
58 /* Fixed-size struct; must read one at a time (see below). */
59 maxread = sizeof *dp;
60 #else
61 maxread = dirp->allocation;
62 #endif
64 bytes = __GETDENTS (dirp->fd, dirp->data, maxread);
65 if (bytes <= 0)
67 /* Don't modifiy errno when reaching EOF. */
68 if (bytes == 0)
69 __set_errno (saved_errno);
70 dp = NULL;
71 break;
73 dirp->size = (size_t) bytes;
75 /* Reset the offset into the buffer. */
76 dirp->offset = 0;
79 dp = (DIRENT_TYPE *) &dirp->data[dirp->offset];
81 #ifdef _DIRENT_HAVE_D_RECLEN
82 reclen = dp->d_reclen;
83 #else
84 /* The only version of `struct dirent*' that lacks `d_reclen'
85 is fixed-size. */
86 assert (sizeof dp->d_name > 1);
87 reclen = sizeof *dp;
88 /* The name is not terminated if it is the largest possible size.
89 Clobber the following byte to ensure proper null termination. We
90 read jst one entry at a time above so we know that byte will not
91 be used later. */
92 dp->d_name[sizeof dp->d_name] = '\0';
93 #endif
95 dirp->offset += reclen;
97 #ifdef _DIRENT_HAVE_D_OFF
98 dirp->filepos = dp->d_off;
99 #else
100 dirp->filepos += reclen;
101 #endif
103 /* Skip deleted files. */
104 } while (dp->d_ino == 0);
106 __libc_lock_unlock (dirp->lock);
108 return dp;
111 #ifdef __READDIR_ALIAS
112 weak_alias (__readdir, readdir)
113 #endif