Repository navigation
Expand file tree
/
Copy path_readdir.c
More file actions
executable file
·59 lines (50 loc) · 1.38 KB
/
Copy path_readdir.c
File metadata and controls
executable file
·59 lines (50 loc) · 1.38 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
/* readdir() Author: Kees J. Bot
* 24 Apr 1989
*/
#define nil 0
#include <lib.h>
#define read _read
#define readdir _readdir
#include <sys/types.h>
#include <sys/stat.h>
#include <dirent.h>
#include <unistd.h>
#include <stdlib.h>
#include <fcntl.h>
#include <limits.h>
#include <errno.h>
#include <string.h>
#define v7ent(p) ((struct _v7_direct *) (p))
#define V7_EXTENT (sizeof(struct _v7_direct) / sizeof(struct _fl_direct) - 1)
struct dirent *readdir(DIR *dp)
/* Return the next entry in a directory. Handle V7 and FLEX format dirs. */
{
struct dirent *e;
if (dp == nil) { errno= EBADF; return nil; }
do {
if (dp->_count <= 0) {
/* Read the next directory block. */
dp->_count= read(dp->_fd, dp->_buf, sizeof(dp->_buf));
if (dp->_count <= 0) return nil;
dp->_count/= sizeof(dp->_buf[0]);
dp->_ptr= dp->_buf;
/* Extent is zero of the first flex entry. */
if (dp->_v7 == (char)-1) dp->_v7= dp->_buf[0].d_extent;
}
if (!dp->_v7) {
/* FLEX. */
e= (struct dirent *) dp->_ptr;
} else {
/* V7: transform to FLEX. */
e= (struct dirent *) dp->_v7f;
e->d_ino= v7ent(dp->_ptr)->d_ino;
e->d_extent= V7_EXTENT;
memcpy(e->d_name, v7ent(dp->_ptr)->d_name, DIRSIZ);
e->d_name[DIRSIZ]= 0;
}
dp->_ptr+= 1 + e->d_extent;
dp->_count-= 1 + e->d_extent;
dp->_pos+= (1 + e->d_extent) * sizeof(*dp->_ptr);
} while (e->d_ino == 0);
return e;
}