2 sed 's/^X//' >'directory.3' <<'!'
3 X.TH DIRECTORY 3 imported
6 Xopendir, readdir, telldir, seekdir, rewinddir, closedir \- high-level directory operations
8 X.B #include <sys/types.h>
14 X.B *opendir(filename)
31 X.B seekdir(dirp, loc)
47 XThis library provides high-level primitives for directory scanning,
48 Xsimilar to those available for 4.2BSD's (very different) directory system.
49 X.\"The purpose of this library is to simulate
50 X.\"the new flexible length directory names of 4.2bsd UNIX
51 X.\"on top of the old directory structure of v7.
52 XIt incidentally provides easy portability to and from 4.2BSD (insofar
53 Xas such portability is not compromised by other 4.2/VAX dependencies).
54 X.\"It allows programs to be converted immediately
55 X.\"to the new directory access interface,
56 X.\"so that they need only be relinked
57 X.\"when moved to 4.2bsd.
58 X.\"It is obtained with the loader option
62 Xopens the directory named by
68 Xreturns a pointer to be used to identify the
70 Xin subsequent operations.
76 Xcannot be accessed or is not a directory.
79 Xreturns a pointer to the next directory entry.
82 Xupon reaching the end of the directory or detecting
88 Xreturns the current location associated with the named
92 Xsets the position of the next
96 XThe new position reverts to the one associated with the
100 Xoperation was performed.
103 Xare good only for the lifetime of the DIR pointer from
104 Xwhich they are derived.
105 XIf the directory is closed and then reopened,
108 Xvalue may be invalidated
109 Xdue to undetected directory compaction in 4.2BSD.
110 XIt is safe to use a previous
112 Xvalue immediately after a call to
114 Xand before any calls to
118 Xresets the position of the named
120 Xto the beginning of the directory.
126 Xand the structure associated with the DIR pointer to be freed.
130 Xstructure is as follows:
135 X /* unsigned */ long d_ino; /* inode number of entry */
136 X unsigned short d_reclen; /* length of this record */
137 X unsigned short d_namlen; /* length of string in d_name */
138 X char d_name[MAXNAMLEN + 1]; /* name must be no longer than this */
145 Xfield is meaningless in non-4.2BSD systems and should be ignored.
150 Xis also a 4.2BSDism;
154 Xshould be used elsewhere.
157 Xgives the minimum memory size needed to hold the
161 Xwith the minimum necessary allocation for
164 XThe preferred way to search the current directory for entry ``name'' is:
168 X len = strlen(name);
169 X dirp = opendir(".");
170 X if (dirp == NULL) {
171 X fprintf(stderr, "%s: can't read directory .\\n", argv[0]);
174 X while ((dp = readdir(dirp)) != NULL)
175 X if (dp->d_namlen == len && strcmp(dp->d_name, name) == 0) {
183 X.\"This library is accessed by specifying ``-lndir'' as the
184 X.\"last argument to the compile line, e.g.:
186 X.\" cc -I/usr/include/ndir -o prog prog.c -lndir
194 XKirk McKusick at Berkeley (ucbvax!mckusick).
195 XMiscellaneous bug fixes from elsewhere.
196 XThe size of the data structure has been decreased to avoid excessive
197 Xspace waste under V7 (where filenames are 14 characters at most).
198 XFor obscure historical reasons, the include file is also available
200 X.IR <ndir/sys/dir.h> .
201 XThe Berkeley version lived in a separate library (\fI\-lndir\fR),
203 Xpart of the C library, although the separate library is retained to
204 Xmaximize compatibility.
206 XThis manual page has been substantially rewritten to be informative in
207 Xthe absence of a 4.2BSD manual.
211 Xmacro actually wastes a bit of space due to some padding requirements
212 Xthat are an artifact of 4.2BSD.
214 XThe returned value of
216 Xpoints to a static area that will be overwritten by subsequent calls.
218 XThere are some unfortunate name conflicts with the \fIreal\fR V7
219 Xdirectory structure definitions.
222 sed 's/^X//' >'dir.h' <<'!'
223 X/* dir.h 4.4 82/07/25 */
226 X * A directory consists of some number of blocks of DIRBLKSIZ
227 X * bytes, where DIRBLKSIZ is chosen such that it can be transferred
228 X * to disk in a single atomic operation (e.g. 512 bytes on most machines).
230 X * Each DIRBLKSIZ byte block contains some number of directory entry
231 X * structures, which are of variable length. Each directory entry has
232 X * a struct direct at the front of it, containing its inode number,
233 X * the length of the entry, and the length of the name contained in
234 X * the entry. These are followed by the name padded to a 4 byte boundary
235 X * with null bytes. All names are guaranteed null terminated.
236 X * The maximum length of a name in a directory is MAXNAMLEN.
238 X * The macro DIRSIZ(dp) gives the amount of space required to represent
239 X * a directory entry. Free space in a directory is represented by
240 X * entries which have dp->d_reclen >= DIRSIZ(dp). All DIRBLKSIZ bytes
241 X * in a directory block are claimed by the directory entries. This
242 X * usually results in the last entry in a directory having a large
243 X * dp->d_reclen. When entries are deleted from a directory, the
244 X * space is returned to the previous entry in the same directory
245 X * block by increasing its dp->d_reclen. If the first entry of
246 X * a directory block is free, then its dp->d_ino is set to 0.
247 X * Entries other than the first in a directory do not normally have
248 X * dp->d_ino set to 0.
250 X#define DIRBLKSIZ 512
252 X#define MAXNAMLEN 255
254 X#define MAXNAMLEN 14
258 X /* unsigned */ long d_ino; /* inode number of entry */
259 X unsigned short d_reclen; /* length of this record */
260 X unsigned short d_namlen; /* length of string in d_name */
261 X char d_name[MAXNAMLEN + 1]; /* name must be no longer than this */
265 X * The DIRSIZ macro gives the minimum record length which will hold
266 X * the directory entry. This requires the amount of space in struct direct
267 X * without the d_name field, plus enough space for the name with a terminating
268 X * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
271 X#define DIRSIZ(dp) \
272 X ((sizeof (struct direct) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
276 X * Definitions for library routines operating on directories.
278 Xtypedef struct _dirdesc {
282 X char dd_buf[DIRBLKSIZ];
287 Xextern DIR *opendir();
288 Xextern struct direct *readdir();
289 Xextern long telldir();
291 Xextern void seekdir();
292 Xextern void closedir();
294 X#define rewinddir(dirp) seekdir((dirp), (long)0)
298 sed 's/^X//' >'makefile' <<'!'
299 XDIR = closedir.o opendir.o readdir.o seekdir.o telldir.o
300 XCFLAGS=-O -I. -Dvoid=int
309 X cp dir.h /usr/include/ndir.h
315 sed 's/^X//' >'closedir.c' <<'!'
316 Xstatic char sccsid[] = "@(#)closedir.c 4.2 3/10/82";
318 X#include <sys/types.h>
322 X * close a directory.
326 X register DIR *dirp;
328 X close(dirp->dd_fd);
331 X free((char *)dirp);
335 sed 's/^X//' >'opendir.c' <<'!'
336 X/* Copyright (c) 1982 Regents of the University of California */
338 Xstatic char sccsid[] = "@(#)opendir.c 4.4 11/12/82";
340 X#include <sys/types.h>
341 X#include <sys/stat.h>
345 X * open a directory.
351 X register DIR *dirp;
353 X struct stat statbuf;
356 X if ((fd = open(name, 0)) == -1)
358 X if (fstat(fd, &statbuf) == -1 || !(statbuf.st_mode & S_IFDIR)) {
362 X if ((dirp = (DIR *)malloc(sizeof(DIR))) == NULL) {
368 X dirp->dd_size = 0; /* so that telldir will work before readdir */
373 sed 's/^X//' >'readdir.c' <<'!'
374 X/* Copyright (c) 1982 Regents of the University of California */
376 Xstatic char sccsid[] = "@(#)readdir.c 4.3 8/8/82";
378 X#include <sys/types.h>
382 X * read an old stlye directory entry and present it as a new one
388 X char od_name[ODIRSIZ];
392 X * get next entry in a directory.
396 X register DIR *dirp;
398 X register struct olddirect *dp;
399 X static struct direct dir;
402 X if (dirp->dd_loc == 0) {
403 X dirp->dd_size = read(dirp->dd_fd, dirp->dd_buf,
405 X if (dirp->dd_size <= 0) {
410 X if (dirp->dd_loc >= dirp->dd_size) {
414 X dp = (struct olddirect *)(dirp->dd_buf + dirp->dd_loc);
415 X dirp->dd_loc += sizeof(struct olddirect);
416 X if (dp->od_ino == 0)
418 X dir.d_ino = dp->od_ino;
419 X strncpy(dir.d_name, dp->od_name, ODIRSIZ);
420 X dir.d_name[ODIRSIZ] = '\0'; /* insure null termination */
421 X dir.d_namlen = strlen(dir.d_name);
422 X dir.d_reclen = DIRBLKSIZ;
428 sed 's/^X//' >'seekdir.c' <<'!'
429 Xstatic char sccsid[] = "@(#)seekdir.c 4.9 3/25/83";
431 X#include <sys/param.h>
435 X * seek to an entry in a directory.
436 X * Only values returned by "telldir" should be passed to seekdir.
440 X register DIR *dirp;
443 X long curloc, base, offset;
445 X extern long lseek();
447 X curloc = telldir(dirp);
450 X base = loc & ~(DIRBLKSIZ - 1);
451 X offset = loc & (DIRBLKSIZ - 1);
452 X (void) lseek(dirp->dd_fd, base, 0);
455 X while (dirp->dd_loc < offset) {
456 X dp = readdir(dirp);
463 sed 's/^X//' >'telldir.c' <<'!'
464 Xstatic char sccsid[] = "@(#)telldir.c 4.1 2/21/82";
466 X#include <sys/types.h>
470 X * return a pointer into a directory
478 X return (lseek(dirp->dd_fd, 0L, 1) - dirp->dd_size + dirp->dd_loc);