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
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
|
/***********************************************************************
* *
* This software is part of the ast package *
* Copyright (c) 1985-2011 AT&T Intellectual Property *
* and is licensed under the *
* Eclipse Public License, Version 1.0 *
* by AT&T Intellectual Property *
* *
* A copy of the License is available at *
* http://www.eclipse.org/org/documents/epl-v10.html *
* (with md5 checksum b35adb5213ca9657e911e9befb180842) *
* *
* Information and Software Systems Research *
* AT&T Research *
* Florham Park NJ *
* *
* Glenn Fowler <gsf@research.att.com> *
* David Korn <dgk@research.att.com> *
* Phong Vo <kpv@research.att.com> *
* *
***********************************************************************/
#pragma prototyped
#include "dirlib.h"
#if _dir_ok || _lib_getdents
NoN(getdents)
#else
/*
* getdents
*
* read directory entries into directory block
*
* NOTE: directory entries must fit within DIRBLKSIZ boundaries
*/
#ifndef MAXNAMLEN
#define MAXNAMLEN 255
#endif
#if _lib_dirread
extern int dirread(int, char*, int);
#endif
#if _lib_getdirentries
extern int getdirentries(int, char*, int, long*);
#endif
ssize_t
getdents(int fd, void* buf, size_t siz)
{
struct stat st;
if (siz < DIRBLKSIZ)
{
errno = EINVAL;
return(-1);
}
if (fstat(fd, &st)) return(-1);
if (!S_ISDIR(st.st_mode))
{
#ifdef ENOTDIR
errno = ENOTDIR;
#else
errno = EBADF;
#endif
return(-1);
}
#if _lib_getdirentries
{
long off;
return(getdirentries(fd, buf, siz, &off));
}
#else
#if _lib_dirread
{
register char* sp; /* system */
register struct dirent* up; /* user */
char* u;
int n;
int m;
int i;
m = (siz * 6) / 10;
m = roundof(m, 8);
sp = (char*)buf + siz - m - 1;
if (!(n = dirread(fd, sp, m))) return(0);
if (n > 0)
{
up = (struct dirent*)buf;
sp[n] = 0;
while (sp < (char*)buf + siz - m + n)
{
i = 0;
while (*sp >= '0' && *sp <= '9')
i = 10 * i + *sp++ - '0';
while (*sp && *sp != '\t') sp++;
if (*sp++)
{
up->d_fileno = i;
u = up->d_name;
while ((*u = *sp++) && u < up->d_name + MAXNAMLEN) u++;
*u = 0;
up->d_reclen = sizeof(struct dirent) - sizeof(up->d_name) + (up->d_namlen = u - up->d_name) + 1;
up->d_reclen = roundof(up->d_reclen, 8);
up = (struct dirent*)((char*)up + up->d_reclen);
}
}
return((char*)up - (char*)buf);
}
}
#else
#if _mem_d_reclen_direct
return(read(fd, buf, siz));
#else
{
#define MAXREC roundof(sizeof(*up)-sizeof(up->d_name)+sizeof(sp->d_name)+1,8)
register struct direct* sp; /* system */
register struct dirent* up; /* user */
register char* s;
register char* u;
int n;
int m;
char tmp[sizeof(sp->d_name) + 1];
/*
* we assume sizeof(struct dirent) > sizeof(struct direct)
*/
up = (struct dirent*)buf;
n = (siz / MAXREC) * sizeof(struct direct);
if ((!(m = n & ~511) || m < MAXREC) && (!(m = n & ~255) || m < MAXREC)) m = n;
do
{
if ((n = read(fd, (char*)buf + siz - m, m)) <= 0) break;
sp = (struct direct*)((char*)buf + siz - m);
while (sp < (struct direct*)((char*)buf + siz - m + n))
{
if (sp->d_ino)
{
up->d_fileno = sp->d_ino;
s = sp->d_name;
u = tmp;
while (s < sp->d_name + sizeof(sp->d_name) && *s)
*u++ = *s++;
*u = 0;
strcpy(up->d_name, tmp);
up->d_reclen = sizeof(struct dirent) - sizeof(up->d_name) + (up->d_namlen = u - tmp) + 1;
up->d_reclen = roundof(up->d_reclen, 8);
up = (struct dirent*)((char*)up + up->d_reclen);
}
sp++;
}
} while (up == (struct dirent*)buf);
return((char*)up - (char*)buf);
}
#endif
#endif
#endif
}
#endif
|