Back to index

glibc  2.9
readdir_r.c
Go to the documentation of this file.
00001 /* Copyright (C) 1991,92,93,94,95,96,97,98,99,2000,02
00002        Free Software Foundation, Inc.
00003    This file is part of the GNU C Library.
00004 
00005    The GNU C Library is free software; you can redistribute it and/or
00006    modify it under the terms of the GNU Lesser General Public
00007    License as published by the Free Software Foundation; either
00008    version 2.1 of the License, or (at your option) any later version.
00009 
00010    The GNU C Library is distributed in the hope that it will be useful,
00011    but WITHOUT ANY WARRANTY; without even the implied warranty of
00012    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
00013    Lesser General Public License for more details.
00014 
00015    You should have received a copy of the GNU Lesser General Public
00016    License along with the GNU C Library; if not, write to the Free
00017    Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
00018    02111-1307 USA.  */
00019 
00020 #include <errno.h>
00021 #include <limits.h>
00022 #include <stddef.h>
00023 #include <string.h>
00024 #include <dirent.h>
00025 #include <unistd.h>
00026 #include <sys/types.h>
00027 #include <assert.h>
00028 
00029 #include <dirstream.h>
00030 
00031 #ifndef __READDIR_R
00032 # define __READDIR_R __readdir_r
00033 # define __GETDENTS __getdents
00034 # define DIRENT_TYPE struct dirent
00035 # define __READDIR_R_ALIAS
00036 #endif
00037 
00038 /* Read a directory entry from DIRP.  */
00039 int
00040 __READDIR_R (DIR *dirp, DIRENT_TYPE *entry, DIRENT_TYPE **result)
00041 {
00042   DIRENT_TYPE *dp;
00043   size_t reclen;
00044   const int saved_errno = errno;
00045 
00046   __libc_lock_lock (dirp->lock);
00047 
00048   do
00049     {
00050       if (dirp->offset >= dirp->size)
00051        {
00052          /* We've emptied out our buffer.  Refill it.  */
00053 
00054          size_t maxread;
00055          ssize_t bytes;
00056 
00057 #ifndef _DIRENT_HAVE_D_RECLEN
00058          /* Fixed-size struct; must read one at a time (see below).  */
00059          maxread = sizeof *dp;
00060 #else
00061          maxread = dirp->allocation;
00062 #endif
00063 
00064          bytes = __GETDENTS (dirp->fd, dirp->data, maxread);
00065          if (bytes <= 0)
00066            {
00067              /* On some systems getdents fails with ENOENT when the
00068                open directory has been rmdir'd already.  POSIX.1
00069                requires that we treat this condition like normal EOF.  */
00070              if (bytes < 0 && errno == ENOENT)
00071               {
00072                 bytes = 0;
00073                 __set_errno (saved_errno);
00074               }
00075 
00076              dp = NULL;
00077              /* Reclen != 0 signals that an error occurred.  */
00078              reclen = bytes != 0;
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 just 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     }
00113   while (dp->d_ino == 0);
00114 
00115   if (dp != NULL)
00116     *result = memcpy (entry, dp, reclen);
00117   else
00118     *result = NULL;
00119 
00120   __libc_lock_unlock (dirp->lock);
00121 
00122   return dp != NULL ? 0 : reclen ? errno : 0;
00123 }
00124 
00125 #ifdef __READDIR_R_ALIAS
00126 weak_alias (__readdir_r, readdir_r)
00127 #endif