/****************************************************************************
 *
 * $Source: /cvsroot/riscossmbserver/smbserver/src/h/dirent,v $
 * $Date: 1999/05/16 13:22:12 $
 * $Revision: 1.1 $
 * $State: Exp $
 * $Author: david $
 *
 * $Log: dirent,v $
 * Revision 1.1  1999/05/16 13:22:12  david
 * Initial revision
 *
 * Revision 1.3  1996/11/06 22:01:41  unixlib
 * Yet more changes by NB, PB and SC.
 *
 * Revision 1.2  1996/10/30 21:58:58  unixlib
 * Massive changes made by Nick Burret and Peter Burwood.
 *
 * Revision 1.1  1996/04/19 21:02:57  simon
 * Initial revision
 *
 ***************************************************************************/

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

#ifndef __DIRENT_H
#define __DIRENT_H

#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
  {
  __off_t 	d_off;		/* offset of next disk directory entry */
  __ino_t 	d_fileno;		/* file number of entry */
  size_t	d_reclen;		/* length of this record */
  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)


#define DIRSIZ(dp) (((sizeof(struct dirent) - \
	MAXNAMLEN + ((dp)->d_namlen + 1)) + \
	(sizeof(int) - 1)) & ~(sizeof(int) - 1))

typedef struct
  {
  int		dd_fd;		/* file descriptor (unused) */
  char		*dd_name;	/* directory name */
  size_t	dd_loc; 	/* buffer offset */
  size_t	dd_size;	/* amount of valid data in buffer */
  size_t	dd_bsize;	/* buffer size */
  size_t	dd_off; 	/* directory offset */
  int		dd_off2;	/* offset of next read */
  char		*dd_buf;	/* directory data buffer */
  } __DIR;

#define DIR __DIR

/* 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.  */
extern struct dirent *readdir (DIR *dirp);

/* 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.  */
#define rewinddir(d) (seekdir((d),(long)0),0)
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, const ptr_t);

#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
