[Libsysio-commit] HEAD: libsysio/src readdir.c readdir64.c module.mk stdlib.c
Brought to you by:
lward
From: Ruth K. <rk...@us...> - 2004-08-30 15:58:10
|
Update of /cvsroot/libsysio/libsysio/src In directory sc8-pr-cvs1.sourceforge.net:/tmp/cvs-serv24062/src Modified Files: module.mk stdlib.c Added Files: readdir.c readdir64.c Log Message: Changes to officially import directory functions into sysio. This is necessitated by the glibc >= 2.3 hidden attribute on getdents. (ifdef'd out for Red Storm build) --- NEW FILE --- /* * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ #ifdef __linux__ #include <features.h> #if defined(__GLIBC__) && !defined(REDSTORM) #include <stdlib.h> #include <string.h> #include <errno.h> #include <fcntl.h> #include <dirent.h> #include <sysio.h> #include "sysio-symbols.h" #ifndef _READDIR #define _READDIR SYSIO_INTERFACE_NAME(readdir) #define _SCANDIR SYSIO_INTERFACE_NAME(scandir) #define _GETDIRENTRIES SYSIO_INTERFACE_NAME(getdirentries) #define _DIRENT_T struct dirent #define _OFF_T off_t #endif #include "stddir.h" _DIRENT_T * _READDIR(DIR *dir) { _DIRENT_T *dp = NULL; _OFF_T dbase; #ifndef BSD ssize_t rc; #else int rc; #endif SYSIO_INTERFACE_DISPLAY_BLOCK; SYSIO_INTERFACE_ENTER; /* need to read new data? */ rc = 0; if (dir->cur >= dir->effective) { dir->cur = 0; dbase = (_OFF_T )dir->base; if (sizeof(dbase) != sizeof(dir->base) && dbase != dir->base) { dir->effective = 0; SYSIO_INTERFACE_RETURN(NULL, -EOVERFLOW); } rc = _GETDIRENTRIES(dir->fd, dir->buf, #ifndef BSD (size_t )BUFSIZE, (_OFF_T *) &dbase); #else (int )BUFSIZE, (long *) __restrict dbase); #endif dir->base = (_SYSIO_OFF_T )dbase; /* error or end-of-file */ if (rc == -ENOENT) rc = 0; if (rc <= 0) { dir->effective = 0; SYSIO_INTERFACE_RETURN(NULL, rc); } dir->effective = rc; } dp = (_DIRENT_T *)(dir->buf + dir->cur); #ifdef _DIRENT_HAVE_D_RECLEN dir->cur += dp->d_reclen; #else dir->cur += sizeof(_DIRENT_T); #endif #ifdef _DIRENT_HAVE_D_OFF dir->filepos = dp->d_off; #else dir->filepos = dir->cur; #endif SYSIO_INTERFACE_RETURN(dp, 0); } sysio_sym_weak_alias(_READDIR, PREPEND(__,_READDIR)) int _SCANDIR(const char *dirname, _DIRENT_T ***namelist, int (*filter) (const _DIRENT_T *), int (*cmp) (const void *, const void *)) { DIR *dir = NULL; _DIRENT_T *de = NULL, *nextde = NULL, **s = NULL; int n = 32, i = 0; size_t desize; SYSIO_INTERFACE_DISPLAY_BLOCK; SYSIO_INTERFACE_ENTER; if ((dir = opendir(dirname)) == NULL) SYSIO_INTERFACE_RETURN(-1, -errno); while ((de = _READDIR(dir)) != NULL) { if ((filter == NULL) || filter(de)) { if (i == 0 || i >= n) { n = MAX(n, 2*i); s = (_DIRENT_T **)realloc(s, (size_t )(n * sizeof(_DIRENT_T *))); if (!s) SYSIO_INTERFACE_RETURN(-1, -ENOMEM); } desize = &de->d_name[_D_ALLOC_NAMLEN(de)] - (char * )de; nextde = (_DIRENT_T *)malloc(desize); if (!nextde) SYSIO_INTERFACE_RETURN(-1, -ENOMEM); s[i++] = (_DIRENT_T *)memcpy(nextde, de, desize); } } if (cmp) qsort (s, i, sizeof (*s), cmp); *namelist = s; closedir(dir); SYSIO_INTERFACE_RETURN(i, 0); } sysio_sym_weak_alias(_SCANDIR, PREPEND(__,_SCANDIR)) #endif #endif --- NEW FILE --- #ifdef _LARGEFILE64_SOURCE #define _SCANDIR SYSIO_INTERFACE_NAME(scandir64) #define _READDIR SYSIO_INTERFACE_NAME(readdir64) #define _GETDIRENTRIES SYSIO_INTERFACE_NAME(getdirentries64) #define _DIRENT_T struct dirent64 #define _OFF_T _SYSIO_OFF_T #include "readdir.c" #endif Index: module.mk =================================================================== RCS file: /cvsroot/libsysio/libsysio/src/module.mk,v retrieving revision 1.8 retrieving revision 1.9 diff -u -w -b -B -p -r1.8 -r1.9 --- module.mk 29 Jun 2004 18:49:40 -0000 1.8 +++ module.mk 30 Aug 2004 15:58:00 -0000 1.9 @@ -24,6 +24,7 @@ SRCDIR_SRCS = src/access.c src/chdir.c s src/mknod.c src/mount.c src/namei.c \ src/open.c src/rw.c src/rename.c \ src/rmdir.c src/stat64.c src/stat.c \ + src/stddir.c src/readdir.c src/readdir64.c \ src/symlink.c src/readlink.c \ src/truncate.c src/unlink.c src/utime.c \ $(FILE_SUPPORT) $(LUSTRE_SRCDIR_SRCS) Index: stdlib.c =================================================================== RCS file: /cvsroot/libsysio/libsysio/src/stdlib.c,v retrieving revision 1.5 retrieving revision 1.6 diff -u -w -b -B -p -r1.5 -r1.6 --- stdlib.c 27 Jul 2004 15:00:48 -0000 1.5 +++ stdlib.c 30 Aug 2004 15:58:00 -0000 1.6 @@ -39,200 +39,6 @@ #include "sysio-symbols.h" -#if !defined(__USE_LARGEFILE64) -#error "__LARGEFILE64_SOURCE must be defined" -#endif - - -/*********************************************************** - * dir series functions * - ***********************************************************/ - -#undef BUFSIZE -#define BUFSIZE 4096 - -struct __dirstream { - int fd; - loff_t base; - loff_t filepos; /* current pos in dir file stream */ - struct dirent *curent; /* current dirent pointer */ - struct dirent64 *curent64; /* current dirent64 pointer */ - struct dirent *retent; /* ent returned to caller */ - struct dirent64 *retent64; /* ent64 returned to caller */ - unsigned int effective; /* effective data size in buffer */ - char buf[BUFSIZE]; -}; - -DIR* opendir(const char *name) -{ - DIR *dir; - - dir = (DIR *) malloc(sizeof(*dir)); - if (!dir) { - errno = ENOMEM; - return NULL; - } - -#if __USE_LARGEFILE64 - dir->fd = open64(name, O_RDONLY); -#else - dir->fd = open(name, O_RDONLY); -#endif - if (dir->fd < 0) - goto err_out; - - dir->base = 0; - dir->filepos = 0; - dir->curent = (struct dirent *) dir->buf; - dir->curent64 = (struct dirent64 *) dir->buf; - dir->retent = NULL; - dir->retent64 = NULL; - dir->effective = 0; - - return dir; -err_out: - free(dir); - return NULL; -} - -sysio_sym_weak_alias(opendir, __opendir); - -struct dirent64 *readdir64(DIR *dir) -{ - int rc, reclen; - - /* need to read new data? */ - if ((char*)dir->curent64 - dir->buf >= dir->effective) { - rc = getdirentries64(dir->fd, dir->buf, BUFSIZE, &dir->base); - /* error or end-of-file */ - if (rc <= 0) - return NULL; - - dir->curent64 = (struct dirent64 *) dir->buf; - dir->effective = rc; - } - - dir->retent64 = dir->curent64; - dir->curent64 = (struct dirent64*) ((char *)(dir->curent64) + - dir->curent64->d_reclen); -#ifdef _DIRENT_HAVE_D_OFF - dir->filepos = dir->curent64->d_off; -#else - dir->filepos += dir->curent64->d_reclen; -#endif - return dir->retent64; -} - -sysio_sym_weak_alias(readdir64, __readdir64); - -/* XXX probably the following assumption is not true */ -#if __WORDSIZE == 64 -#define NATURAL_READDIR64 -#else -#undef NATURAL_READDIR64 -#endif - -#ifndef NATURAL_READDIR64 - -struct dirent *readdir(DIR *dir) -{ - int rc, reclen; - - /* need to read new data? */ - if ((char*)dir->curent - dir->buf >= dir->effective) { - rc = getdirentries(dir->fd, dir->buf, BUFSIZE, (off_t*) &dir->base); - /* error or end-of-file */ - if (rc <= 0) - return NULL; - - dir->curent = (struct dirent *) dir->buf; - dir->effective = rc; - } - - dir->retent = dir->curent; - dir->curent = (struct dirent*) ((char *)(dir->curent) + - dir->curent->d_reclen); -#ifdef _DIRENT_HAVE_D_OFF - dir->filepos = dir->curent->d_off; -#else - dir->filepos += dir->curent->d_reclen; -#endif - return dir->retent; -} -sysio_sym_weak_alias(readdir, __readdir); - -#else /* NATURAL_READDIR64 */ - -struct dirent *readdir(DIR *dir) { - return (struct dirent *) readdir64(dir); -} -sysio_sym_weak_alias(readdir, __readdir); - -#endif /* NATURAL_READDIR64 */ - -int closedir(DIR *dir) -{ - int rc; - - rc = close(dir->fd); - - free(dir); - return rc; -} - -sysio_sym_weak_alias(closedir, __closedir); - -int dirfd(DIR *dir) -{ - return dir->fd; -} - -off_t telldir(DIR *dir) -{ - return (dir->filepos); -} - -void seekdir(DIR *dir, off_t offset) -{ - dir->filepos = offset; - - dir->base = offset; - dir->curent64 = (struct dirent64 *) dir->buf; - dir->retent64 = NULL; - dir->effective = 0; - dir->curent = (struct dirent *) dir->buf; - dir->retent = NULL; -} - -void rewinddir(DIR *dir) -{ - dir->base = 0; - dir->filepos = 0; - dir->curent64 = (struct dirent64 *) dir->buf; - dir->retent64 = NULL; - dir->curent = (struct dirent *) dir->buf; - dir->retent = NULL; - dir->effective = 0; -} - -#if 0 -int scandir(const char *dir, struct dirent ***namelist, - int(*select)(const struct dirent *), - int(*compar)(const void *, const void *)) -{ - errno = ENOSYS; - return -1; -} - -int scandir64(const char *dir, struct dirent64 ***namelist, - int(*select)(const struct dirent64 *), - int(*compar)(const void *, const void *)) -{ - errno = ENOSYS; - return -1; -} -#endif - /*********************************************************** * FIXME workaround for linux only * ***********************************************************/ |