man(1) Manual page archive


     DIRECTORY(3)                                         DIRECTORY(3)

     NAME
          opendir, readdir, telldir, seekdir, closedir - 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;

          closedir(dirp)
          DIR *dirp;

     DESCRIPTION
          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 oper-
          ations.  The pointer value 0 is returned if filename cannot
          be accessed or is not a directory.

          Readdir returns a pointer to the next directory entry.  It
          returns 0 upon reaching the end of the directory or detect-
          ing 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 opera-
          tion was performed.  Values returned by telldir are good
          only for the lifetime of the DIR pointer from which they are
          derived.

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

          struct direct {
                 u_long d_ino;               inode for the entry

     DIRECTORY(3)                                         DIRECTORY(3)

                 short  d_reclen;            don't use
                 short  d_namlen;            equivalent to strlen(d_name)
                 char   d_name[MAXNAMLEN+1]; null-terminated entry name
          };

          The preferred way to search the current directory is:
                  DIR *dirp;
                  dirp = opendir(".");
                  for(dp = readdir(dirp); dp != 0; dp = readdir(dir))
                          if(strcmp(dp->d_name, name) == 0)
                                  break;
                  closedir(dirp);
                  /* found name if dp != 0 */

     SEE ALSO
          dir(5), open(2), dirread(2), read(2), lseek(2), ftw(3)

     BUGS
          The return values point to static data whose content is
          overwritten by each call.