DIRECTORY

Section: C Library Functions (3)
Updated: imported
Index Return to Main Contents
 

NAME

opendir, readdir, telldir, seekdir, rewinddir, closedir - high-level directory operations  

SYNOPSIS

#include <sys/types.h>
#include <ndir.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;

closedir(dirp)
DIR *dirp;  

DESCRIPTION

This library provides high-level primitives for directory scanning, similar to those available for 4.2BSD's (very different) directory system. It incidentally provides easy portability to and from 4.2BSD (insofar as such portability is not compromised by other 4.2/VAX dependencies).

Opendir opens the directory named by filename and associates a directory stream with it. Opendir returns a pointer to be used to identify the directory stream in subsequent operations. The pointer NULL is returned if filename cannot be accessed or is not a directory.

Readdir returns a pointer to the next directory entry. It returns NULL upon reaching the end of the directory or detecting an invalid seekdir operation.

Telldir returns the current location associated with the named directory stream.

Seekdir sets the position of the next readdir operation on the directory stream. The new position reverts to the one associated with the directory stream when the telldir operation was performed. 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 reopened, the telldir value may be invalidated due to undetected directory compaction in 4.2BSD. It is safe to use a previous telldir value immediately after a call to opendir and before any calls to readdir.

Rewinddir resets the position of the named directory stream to the beginning of the directory.

Closedir causes the named directory stream to be closed, and the structure associated with the DIR pointer to be freed.

A direct structure is as follows:

struct  direct {
        /* unsigned */ long     d_ino;  /* inode number of entry */
        unsigned short  d_reclen;       /* length of this record */
        unsigned short  d_namlen;       /* length of string in d_name */
        char    d_name[MAXNAMLEN + 1];  /* name must be no longer than this */
};

The d_reclen field is meaningless in non-4.2BSD systems and should be ignored. The use of a long for d_ino is also a 4.2BSDism; ino_t (see types(5)) should be used elsewhere. The macro DIRSIZ(dp) gives the minimum memory size needed to hold the direct value pointed to by dp, with the minimum necessary allocation for d_name.

The preferred way to search the current directory for entry ``name'' is:

        len = strlen(name);
        dirp = opendir(".");
        if (dirp == NULL) {
                fprintf(stderr, "%s: can't read directory .\n", argv[0]);
                return NOT_FOUND;
        }
        while ((dp = readdir(dirp)) != NULL)
                if (dp->d_namlen == len && strcmp(dp->d_name, name) == 0) {
                        closedir(dirp);
                        return FOUND;
                }
        closedir(dirp);
        return NOT_FOUND;
 

SEE ALSO

open(2), close(2), read(2), lseek(2)  

HISTORY

Written by Kirk McKusick at Berkeley (ucbvax!mckusick). Miscellaneous bug fixes from elsewhere. The size of the data structure has been decreased to avoid excessive space waste under V7 (where filenames are 14 characters at most). For obscure historical reasons, the include file is also available as <ndir/sys/dir.h>. The Berkeley version lived in a separate library (-lndir), whereas ours is part of the C library, although the separate library is retained to maximize compatibility.

This manual page has been substantially rewritten to be informative in the absence of a 4.2BSD manual.  

BUGS

The DIRSIZ macro actually wastes a bit of space due to some padding requirements that are an artifact of 4.2BSD.

The returned value of readdir points to a static area that will be overwritten by subsequent calls.

There are some unfortunate name conflicts with the real V7 directory structure definitions.


 

Index

NAME
SYNOPSIS
DESCRIPTION
SEE ALSO
HISTORY
BUGS

This document was created by man2html, using the manual pages.
Time: 17:45:04 GMT, July 31, 2024