/****************************************************************************
 *
 * $Source: /usr/local/cvsroot/unixlib/source/clib/h/dirent,v $
 * $Date: 1997/10/09 11:30:19 $
 * $Revision: 1.8 $
 * $State: Exp $
 * $Author: unixlib $
 *
 ***************************************************************************/

/* POSIX Standard 5.1.2: Directory Operations <dirent.h> */

#ifndef __DIRENT_H
#define __DIRENT_H

#ifndef __UNIXLIB_FEATURES_H
#include <unixlib/features.h>
#endif
#ifndef __STDDEF_H
#include <stddef.h>
#endif
#ifndef __UNIXLIB_TYPES_H
#include <unixlib/types.h>
#endif
#ifndef __SYS_PARAM_H
#include <sys/param.h>
#endif

#ifdef __cplusplus
extern "C" {
#endif

/* MAXNAMELEN is the BSD name for what POSIX calls NAME_MAX.  */

/* Don't assume ADFS filename restrictions.
   Keep in sync with <limits.h>, _POSIX_NAME_MAX.  */
#define MAXNAMLEN	MAXPATHLEN

/* This isn't really how ADFS stores files in a directory, but
   since no I/O is permitted on directories anyway this doesn't
   really matter.  */

struct dirent
{
  __ino_t	d_fileno;		/* file number of entry */
  size_t	d_namlen;		/* length of d_name */
  unsigned char d_type;			/* file type, possibly unknown */
  char		d_name[MAXNAMLEN];	/* name */
};

/* For backwards compatibility with BSD.  */
#define d_ino	d_fileno

/* BSD file types for d_type.  */
enum
  {
    DT_UNKNOWN = 0,
    DT_FIFO = 1,
    DT_CHR = 2,
    DT_DIR = 4,
    DT_BLK = 6,
    DT_REG = 8,
    DT_LNK = 10,
    DT_SOCK = 12
  };

/* BSD macros to convert between stat structure types and
   directory types.  */
#define IFTODT(mode) (((mode) & 0170000) >> 12)
#define DTTOIF(dirtype) ((dirtype) << 12)

typedef struct __dir_stream DIR;

/* Magic number to fill __magic.  */
#define _DIRMAGIC 0xf7b2bace

struct __dir_stream
{
  unsigned int __magic; /* magic number to protect our streams */
  char *dd_name; /* directory name (unix format) */
  char *dd_name_can; /* canonicalised name (riscos format) */
  __off_t dd_off; /* offset of next read (used for seeking) */
  __off_t gbpb_off; /* offset used for os_gbpb */
  char *dir_cache; /* pointer to a cache of directory filenames */
  char *dir_cache_index; /* index into that cache for the next file  */
  char do_read; /* zero if we need to fill the directory cache */
  struct dirent *dirent; /* last directory entry read using readdir */
  DIR *next;
};

#if __INTEGRITY_CHECK
/* Nonzero if stream is a valid stream.  */
#define __validdir(stream) (stream != NULL && stream->__magic == _DIRMAGIC)
#else
#define __validdir(stream) (1)
#endif

/* Open a directory stream on name. Return a dir stream on
   the directory, or NULL if it could not be opened.  */
extern DIR *opendir (const char *__name);

/* Read a directory entry from dirp.
   Return a pointer to a struct dirent describing the entry,
   or NULL for EOF or error.  The storage returned may be overwritten
   by a later readdir call on the same DIR stream, or by closedir.  */
extern struct dirent *readdir (DIR *__dirp);

/* Reentrant version of readdir.  */
extern int readdir_r (DIR *__dirp, struct dirent *__entry,
		      struct dirent **__result);

/* Return the current position of dirp.  */
extern long telldir (DIR *__dirp);

/* Seek to position pos on dirp.  */
extern void seekdir (DIR *__dirp, __off_t __pos);

/* Rewind DIRP to the beginning of the directory.  */
extern void rewinddir (DIR *__dirp);

/* Close the directory stream dirp. Return 0 if successful,
   -1 if not.  */
extern int closedir (DIR *__dirp);

/* Function to compare two `struct dirent's alphabetically.  */
extern int alphasort (const ptr_t __a, const ptr_t __b);

#if 0
/* Scan the directory dir, calling 'select' on each directory entry.
   Entries for which 'select' returns nonzero are individually malloc'd,
   sorted using qsort with 'cmp', and collected in a malloc'd array in
   *namelist.  Returns the number of entries selected, or -1 on error.  */
extern int scandir (const char *__dir,
		    struct dirent ***__namelist,
		    int (*__select) (struct dirent *),
		    int (*__cmp) (const ptr_t, const ptr_t));

/* Read directory entries from fd into buf, reading at most nbytes.
   Reading starts at offset *basep, and *basep is updated with the new
   position after reading.  Returns the number of bytes read; zero when at
   end of directory; or -1 for errors.  */
extern ssize_t getdirentries (int __fd, char *__buf,
			      size_t __nbytes, __off_t *__basep);
#endif

#ifdef __cplusplus
}
#endif

#endif
