Back to index

glibc  2.9
readdir.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991-1997,1999,2000,2002,2007 Free Software Foundation, Inc.
00002    This file is part of the GNU C Library.
00003 
00004    The GNU C Library is free software; you can redistribute it and/or
00005    modify it under the terms of the GNU Lesser General Public
00006    License as published by the Free Software Foundation; either
00007    version 2.1 of the License, or (at your option) any later version.
00008 
00009    The GNU C Library is distributed in the hope that it will be useful,
00010    but WITHOUT ANY WARRANTY; without even the implied warranty of
00011    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00012    Lesser General Public License for more details.
00013 
00014    You should have received a copy of the GNU Lesser General Public
00015    License along with the GNU C Library; if not, write to the Free
00016    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00017    02111-1307 USA.  */
00018 
00019 #include <errno.h>
00020 #include <limits.h>
00021 #include <stddef.h>
00022 #include <string.h>
00023 #include <dirent.h>
00024 #include <unistd.h>
00025 #include <sys/types.h>
00026 #include <assert.h>
00027 
00028 #include <dirstream.h>
00029 
00030 #ifndef __READDIR
00031 # define __READDIR __readdir
00032 # define __GETDENTS __getdents
00033 # define DIRENT_TYPE struct dirent
00034 # define __READDIR_ALIAS
00035 #endif
00036 
00037 /* Read a directory entry from DIRP.  */
00038 DIRENT_TYPE *
00039 __READDIR (DIR *dirp)
00040 {
00041   DIRENT_TYPE *dp;
00042   int saved_errno = errno;
00043 
00044 #ifndef NOT_IN_libc
00045   __libc_lock_lock (dirp->lock);
00046 #endif
00047 
00048   do
00049     {
00050       size_t reclen;
00051 
00052       if (dirp->offset >= dirp->size)
00053        {
00054          /* We've emptied out our buffer.  Refill it.  */
00055 
00056          size_t maxread;
00057          ssize_t bytes;
00058 
00059 #ifndef _DIRENT_HAVE_D_RECLEN
00060          /* Fixed-size struct; must read one at a time (see below).  */
00061          maxread = sizeof *dp;
00062 #else
00063          maxread = dirp->allocation;
00064 #endif
00065 
00066          bytes = __GETDENTS (dirp->fd, dirp->data, maxread);
00067          if (bytes <= 0)
00068            {
00069              /* On some systems getdents fails with ENOENT when the
00070                open directory has been rmdir'd already.  POSIX.1
00071                requires that we treat this condition like normal EOF.  */
00072              if (bytes < 0 && errno == ENOENT)
00073               bytes = 0;
00074 
00075              /* Don't modifiy errno when reaching EOF.  */
00076              if (bytes == 0)
00077               __set_errno (saved_errno);
00078              dp = NULL;
00079              break;
00080            }
00081          dirp->size = (size_t) bytes;
00082 
00083          /* Reset the offset into the buffer.  */
00084          dirp->offset = 0;
00085        }
00086 
00087       dp = (DIRENT_TYPE *) &dirp->data[dirp->offset];
00088 
00089 #ifdef _DIRENT_HAVE_D_RECLEN
00090       reclen = dp->d_reclen;
00091 #else
00092       /* The only version of `struct dirent*' that lacks `d_reclen'
00093         is fixed-size.  */
00094       assert (sizeof dp->d_name > 1);
00095       reclen = sizeof *dp;
00096       /* The name is not terminated if it is the largest possible size.
00097         Clobber the following byte to ensure proper null termination.  We
00098         read jst one entry at a time above so we know that byte will not
00099         be used later.  */
00100       dp->d_name[sizeof dp->d_name] = '\0';
00101 #endif
00102 
00103       dirp->offset += reclen;
00104 
00105 #ifdef _DIRENT_HAVE_D_OFF
00106       dirp->filepos = dp->d_off;
00107 #else
00108       dirp->filepos += reclen;
00109 #endif
00110 
00111       /* Skip deleted files.  */
00112     } while (dp->d_ino == 0);
00113 
00114 #ifndef NOT_IN_libc
00115   __libc_lock_unlock (dirp->lock);
00116 #endif
00117 
00118   return dp;
00119 }
00120 
00121 #ifdef __READDIR_ALIAS
00122 weak_alias (__readdir, readdir)
00123 #endif