[go: up one dir, main page]

Menu

[r1136]: / tags / purelibc / 0.4 / dir.c  Maximize  Restore  History

Download this file

137 lines (121 with data), 3.2 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
/* This is part of pure_libc (a project related to ViewOS and Virtual Square)
*
* dir.c: Directory management
*
* Copyright 2006 Renzo Davoli University of Bologna - Italy
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program 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 General Public License for more details.
*
* You should have received a copy of the GNU General Public License a
* with this program; if not, write to the Free Software Foundation, Inc.,
* 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
*/
#include <config.h>
#include <fcntl.h>
#include <stdlib.h>
#include <sys/types.h>
#include <dirent.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#define PURE_DIRSTREAM_SIZE 2048
#define PURE_DIRBUF_SIZE (PURE_DIRSTREAM_SIZE - 3*sizeof(int))
struct __dirstream {
int fd;
int bufsize;
int bufpos;
char buf[PURE_DIRBUF_SIZE];
struct dirent de32;
};
DIR *opendir(const char *name)
{
int fd;
DIR *newdir=NULL;
if ((fd=open(name, O_RDONLY | O_DIRECTORY)) >= 0) {
if (fcntl (fd, F_SETFD, FD_CLOEXEC) < 0)
close(fd);
else {
newdir=(DIR *) malloc (sizeof (struct __dirstream));
if (!newdir)
return NULL;
else {
newdir->fd=fd;
newdir->bufsize=newdir->bufpos=0;
}
}
}
return newdir;
}
int closedir(DIR *dir){
int fd=dir->fd;
free(dir);
return close(fd);
}
#define _MAX_OFF_T ((__off_t) -1)
struct dirent *readdir(DIR *dir){
register struct dirent64 *de64=readdir64(dir);
if(de64 == NULL)
return NULL;
else {
dir->de32.d_ino=de64->d_ino;
dir->de32.d_off=(de64->d_off > _MAX_OFF_T)?_MAX_OFF_T:de64->d_off;
dir->de32.d_reclen=de64->d_reclen;
dir->de32.d_type=de64->d_type;
strcpy(dir->de32.d_name,de64->d_name);
return &(dir->de32);
}
}
struct dirent64 *readdir64(DIR *dir){
register struct dirent64 *this;
this=((struct dirent64 *) (dir->buf + dir->bufpos));
if (dir->bufsize == 0 || (dir->bufpos += this->d_reclen) >= dir->bufsize) {
dir->bufsize = getdents64(dir->fd,(struct dirent64 *)dir->buf,PURE_DIRBUF_SIZE-1);
if (dir->bufsize <= 0)
return NULL;
else
dir->bufpos=0;
}
this=((struct dirent64 *) (dir->buf + dir->bufpos));
return this;
}
int dirfd(DIR *dir){
if (dir)
return dir->fd;
else
return -1;
}
void rewinddir(DIR *dir){
if (dir) {
lseek(dir->fd,0,SEEK_SET);
dir->bufsize=dir->bufpos=0;
}
}
void seekdir(DIR *dir, off_t offset){
if (dir) {
lseek(dir->fd,offset,SEEK_SET);
dir->bufsize=dir->bufpos=0;
}
}
off_t telldir(DIR *dir){
if (dir) {
off_t pos = lseek(dir->fd,0,SEEK_CUR);
if (pos != (off_t) -1)
return -1;
else
return pos + dir->bufpos;
} else
return -1;
}
/*
int scandir(const char *dir, struct dirent ***namelist,
int(*filter)(const struct dirent *),
int(*compar)(const struct dirent **, const struct dirent **)){
}*/