00001 /* readdir() Author: Kees J. Bot 00002 * 24 Apr 1989 00003 */ 00004 #define nil 0 00005 #include <lib.h> 00006 #define read _read 00007 #define readdir _readdir 00008 #include <sys/types.h> 00009 #include <sys/stat.h> 00010 #include <dirent.h> 00011 #include <unistd.h> 00012 #include <stdlib.h> 00013 #include <fcntl.h> 00014 #include <limits.h> 00015 #include <errno.h> 00016 #include <string.h> 00017 00018 #define v7ent(p) ((struct _v7_direct *) (p)) 00019 #define V7_EXTENT (sizeof(struct _v7_direct) / sizeof(struct _fl_direct) - 1) 00020 00021 struct dirent *readdir(DIR *dp) 00022 /* Return the next entry in a directory. Handle V7 and FLEX format dirs. */ 00023 { 00024 struct dirent *e; 00025 00026 if (dp == nil) { errno= EBADF; return nil; } 00027 00028 do { 00029 if (dp->_count <= 0) { 00030 /* Read the next directory block. */ 00031 dp->_count= read(dp->_fd, dp->_buf, sizeof(dp->_buf)); 00032 if (dp->_count <= 0) return nil; 00033 00034 dp->_count/= sizeof(dp->_buf[0]); 00035 dp->_ptr= dp->_buf; 00036 00037 /* Extent is zero of the first flex entry. */ 00038 if (dp->_v7 == (char)-1) dp->_v7= dp->_buf[0].d_extent; 00039 } 00040 00041 if (!dp->_v7) { 00042 /* FLEX. */ 00043 e= (struct dirent *) dp->_ptr; 00044 } else { 00045 /* V7: transform to FLEX. */ 00046 e= (struct dirent *) dp->_v7f; 00047 e->d_ino= v7ent(dp->_ptr)->d_ino; 00048 e->d_extent= V7_EXTENT; 00049 memcpy(e->d_name, v7ent(dp->_ptr)->d_name, DIRSIZ); 00050 e->d_name[DIRSIZ]= 0; 00051 } 00052 00053 dp->_ptr+= 1 + e->d_extent; 00054 dp->_count-= 1 + e->d_extent; 00055 dp->_pos+= (1 + e->d_extent) * sizeof(*dp->_ptr); 00056 00057 } while (e->d_ino == 0); 00058 return e; 00059 }
1.4.6