|
|
1.1 root 1: .\" Copyright (c) 1983 Regents of the University of California.
2: .\" All rights reserved.
3: .\"
4: .\" Redistribution and use in source and binary forms are permitted provided
5: .\" that: (1) source distributions retain this entire copyright notice and
6: .\" comment, and (2) distributions including binaries display the following
7: .\" acknowledgement: ``This product includes software developed by the
8: .\" University of California, Berkeley and its contributors'' in the
9: .\" documentation or other materials provided with the distribution and in
10: .\" all advertising materials mentioning features or use of this software.
11: .\" Neither the name of the University nor the names of its contributors may
12: .\" be used to endorse or promote products derived from this software without
13: .\" specific prior written permission.
14: .\" THIS SOFTWARE IS PROVIDED ``AS IS'' AND WITHOUT ANY EXPRESS OR IMPLIED
15: .\" WARRANTIES, INCLUDING, WITHOUT LIMITATION, THE IMPLIED WARRANTIES OF
16: .\" MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE.
17: .\"
18: .\" @(#)directory.3 6.6 (Berkeley) 6/23/90
19: .\"
20: .TH DIRECTORY 3 "June 23, 1990"
21: .UC 5
22: .SH NAME
23: opendir, readdir, telldir, seekdir, rewinddir, closedir, dirfd \- directory operations
24: .SH SYNOPSIS
25: .nf
26: .ft B
27: #include <sys/types.h>
28: #include <dirent.h>
29:
30: DIR *
31: opendir(const char *filename);
32:
33: struct direct
34: *readdir(DIR * dirp);
35:
36: long
37: telldir(const DIR *dirp);
38:
39: void
40: seekdir(DIR *dirp, long loc);
41:
42: void
43: rewinddir(DIR *dirp);
44:
45: int
46: closedir(DIR *dirp);
47:
48: int
49: dirfd(DIR *dirp)
50: .ft R
51: .fi
52: .SH DESCRIPTION
53: .I Opendir
54: opens the directory named by
55: .I filename
56: and associates a
57: .I directory stream
58: with it.
59: .I Opendir
60: returns a pointer to be used to identify the
61: .I directory stream
62: in subsequent operations. The pointer
63: .SM
64: .B NULL
65: is returned if
66: .I filename
67: cannot be accessed, or if it cannot
68: .IR malloc (3)
69: enough memory to hold the whole thing.
70: .PP
71: .I Readdir
72: returns a pointer to the next directory entry. It returns
73: .B NULL
74: upon reaching the end of the directory or detecting an invalid
75: .I seekdir
76: operation.
77: .PP
78: .I Telldir
79: returns the current location associated with the named
80: .I directory stream.
81: .PP
82: .I Seekdir
83: sets the position of the next
84: .I readdir
85: operation on the
86: .I directory stream.
87: The new position reverts to the one associated with the
88: .I directory stream
89: when the
90: .I telldir
91: operation was performed. Values returned by
92: .I telldir
93: are good only for the lifetime of the DIR pointer from which they are derived.
94: If the directory is closed and then reopened, the
95: .I telldir
96: value may be invalidated due to undetected directory compaction.
97: It is safe to use a previous
98: .I telldir
99: value immediately after a call to
100: .I opendir
101: and before any calls to
102: .I readdir.
103: .PP
104: .I Rewinddir
105: resets the position of the named
106: .I directory stream
107: to the beginning of the directory.
108: .PP
109: .I Closedir
110: closes the named
111: .I directory stream
112: and frees the structure associated with the DIR pointer,
113: returning 0 on success.
114: On failure, -1 is returned and errno is set to indicate the error.
115: .PP
116: .I Dirfd
117: returns the integer file descriptor associated with the named
118: .I directory stream,
119: see open(2).
120: .PP
121: Sample code which searchs a directory for entry ``name'' is:
122: .PP
123: .nf
124: .RS
125: len = strlen(name);
126: dirp = opendir(".");
127: for (dp = readdir(dirp); dp != NULL; dp = readdir(dirp))
128: if (dp->d_namlen == len && !strcmp(dp->d_name, name)) {
129: (void)closedir(dirp);
130: return FOUND;
131: }
132: (void)closedir(dirp);
133: return NOT_FOUND;
134: .RE
135: .fi
136: .SH "SEE ALSO"
137: open(2), close(2), read(2), lseek(2), dir(5)
This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.