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
|
/*
* @(#) dir.h 1.4 87/11/06 Public Domain.
*
* A public domain implementation of BSD directory routines for
* MS-DOS. Written by Michael Rendell ({uunet,utai}michael@garfield),
* August 1987
*
* Enhanced and ported to OS/2 by Kai Uwe Rommel; added scandir() prototype
* December 1989, February 1990
*/
#define MAXNAMLEN 12
#define MAXPATHLEN 128
#define A_RONLY 0x01
#define A_HIDDEN 0x02
#define A_SYSTEM 0x04
#define A_LABEL 0x08
#define A_DIR 0x10
#define A_ARCHIVE 0x20
struct direct
{
ino_t d_ino; /* a bit of a farce */
int d_reclen; /* more farce */
int d_namlen; /* length of d_name */
char d_name[MAXNAMLEN + 1]; /* null terminated */
long d_size; /* size in bytes */
int d_mode; /* DOS or OS/2 file attributes */
};
/* The fields d_size and d_mode are extensions by me (Kai Uwe Rommel).
* The find_first and find_next calls deliver this data without any extra cost.
* If this data is needed, these fields save a lot of extra calls to stat()
* (each stat() again performs a find_first call !).
*/
struct _dircontents
{
char *_d_entry;
long _d_size;
int _d_mode;
struct _dircontents *_d_next;
};
typedef struct _dirdesc
{
int dd_id; /* uniquely identify each open directory */
long dd_loc; /* where we are in directory entry is this */
struct _dircontents *dd_contents; /* pointer to contents of dir */
struct _dircontents *dd_cp; /* pointer to current position */
}
DIR;
extern DIR *opendir(char *);
extern struct direct *readdir(DIR *);
extern void seekdir(DIR *, long);
extern long telldir(DIR *);
extern void closedir(DIR *);
#define rewinddir(dirp) seekdir(dirp, 0L)
extern int scandir(char *, struct direct ***,
int (*)(struct direct *),
int (*)(struct direct *, struct direct *));
extern int getfmode(char *);
extern int setfmode(char *, unsigned);
/*
NAME
opendir, readdir, telldir, seekdir, rewinddir, closedir -
directory operations
SYNTAX
#include <sys/types.h>
#include <sys/dir.h>
DIR *opendir(filename)
char *filename;
struct direct *readdir(dirp)
DIR *dirp;
long telldir(dirp)
DIR *dirp;
seekdir(dirp, loc)
DIR *dirp;
long loc;
rewinddir(dirp)
DIR *dirp;
int closedir(dirp)
DIR *dirp;
DESCRIPTION
The opendir library routine opens the directory named by
filename and associates a directory stream with it. A
pointer is returned to identify the directory stream in sub-
sequent operations. The pointer NULL is returned if the
specified filename can not be accessed, or if insufficient
memory is available to open the directory file.
The readdir routine returns a pointer to the next directory
entry. It returns NULL upon reaching the end of the direc-
tory or on detecting an invalid seekdir operation. The
readdir routine uses the getdirentries system call to read
directories. Since the readdir routine returns NULL upon
reaching the end of the directory or on detecting an error,
an application which wishes to detect the difference must
set errno to 0 prior to calling readdir.
The telldir routine returns the current location associated
with the named directory stream. Values returned by telldir
are good only for the lifetime of the DIR pointer from which
they are derived. If the directory is closed and then reo-
pened, the telldir value may be invalidated due to
undetected directory compaction.
The seekdir routine sets the position of the next readdir
operation on the directory stream. Only values returned by
telldir should be used with seekdir.
The rewinddir routine resets the position of the named
directory stream to the beginning of the directory.
The closedir routine closes the named directory stream and
returns a value of 0 if successful. Otherwise, a value of -1
is returned and errno is set to indicate the error. All
resources associated with this directory stream are
released.
EXAMPLE
The following sample code searches a directory for the entry
name.
len = strlen(name);
dirp = opendir(".");
for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
if (dp->d_namlen == len && !strcmp(dp->d_name, name)) {
closedir(dirp);
return FOUND;
}
closedir(dirp);
return NOT_FOUND;
SEE ALSO
close(2), getdirentries(2), lseek(2), open(2), read(2),
dir(5)
*/
|