home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume4 / unaxcess / part1 / dir.h next >
Encoding:
C/C++ Source or Header  |  1986-11-30  |  3.1 KB  |  91 lines

  1. /*
  2.  *
  3.  *                N O T I C E
  4.  *
  5.  * This file is NOT a copyrighted part of the UNaXcess distribution.  These
  6.  * are directory-reading routines which are compatible with the Berkeley Unix
  7.  * (4.2BSD, 4.3BSD) strectory routines.  They come from the Usenet news
  8.  * distribution and are in the public domain.
  9.  *
  10.  * To get the best use of them:  install the file "dir.h" in /usr/include
  11.  * -- standard usage calls it "ndir.h", and make a random archive of dir.o and
  12.  * put it in /usr/lib/libndir.a .  It is then available with "-lndir".
  13.  *
  14.  * Bell System {III, V} sites, just make an archive -- it is only one file
  15.  * anyway.  Other sites will have to run ranlib on the archive to keep ld
  16.  * happy.
  17.  */
  18.  
  19. /*    dir.h    4.4    82/07/25    */
  20.  
  21. #ifdef BSD
  22. #include <sys/dir.h>
  23. #else
  24.  
  25. /*
  26.  * A directory consists of some number of blocks of DIRBLKSIZ
  27.  * bytes, where DIRBLKSIZ is chosen such that it can be transferred
  28.  * to disk in a single atomic operation (e.g. 512 bytes on most machines).
  29.  *
  30.  * Each DIRBLKSIZ byte block contains some number of directory entry
  31.  * structures, which are of variable length.  Each directory entry has
  32.  * a struct direct at the front of it, containing its inode number,
  33.  * the length of the entry, and the length of the name contained in
  34.  * the entry.  These are followed by the name padded to a 4 byte boundary
  35.  * with null bytes.  All names are guaranteed null terminated.
  36.  * The maximum length of a name in a directory is MAXNAMLEN.
  37.  *
  38.  * The macro DIRSIZ(dp) gives the amount of space required to represent
  39.  * a directory entry.  Free space in a directory is represented by
  40.  * entries which have dp->d_reclen >= DIRSIZ(dp).  All DIRBLKSIZ bytes
  41.  * in a directory block are claimed by the directory entries.  This
  42.  * usually results in the last entry in a directory having a large
  43.  * dp->d_reclen.  When entries are deleted from a directory, the
  44.  * space is returned to the previous entry in the same directory
  45.  * block by increasing its dp->d_reclen.  If the first entry of
  46.  * a directory block is free, then its dp->d_ino is set to 0.
  47.  * Entries other than the first in a directory do not normally have
  48.  * dp->d_ino set to 0.
  49.  */
  50. #define DIRBLKSIZ    512
  51. #define    MAXNAMLEN    255
  52.  
  53. struct    direct {
  54.     long    d_ino;            /* inode number of entry */
  55.     short    d_reclen;        /* length of this record */
  56.     short    d_namlen;        /* length of string in d_name */
  57.     char    d_name[MAXNAMLEN + 1];    /* name must be no longer than this */
  58. };
  59.  
  60. /*
  61.  * The DIRSIZ macro gives the minimum record length which will hold
  62.  * the directory entry.  This requires the amount of space in struct direct
  63.  * without the d_name field, plus enough space for the name with a terminating
  64.  * null byte (dp->d_namlen+1), rounded up to a 4 byte boundary.
  65.  */
  66. #ifdef DIRSIZ
  67. #undef DIRSIZ
  68. #endif
  69. #define DIRSIZ(dp) \
  70.     ((sizeof (struct direct) - (MAXNAMLEN+1)) + (((dp)->d_namlen+1 + 3) &~ 3))
  71.  
  72. #ifndef KERNEL
  73. /*
  74.  * Definitions for library routines operating on directories.
  75.  */
  76. typedef struct _dirdesc {
  77.     int    dd_fd;
  78.     long    dd_loc;
  79.     long    dd_size;
  80.     char    dd_buf[DIRBLKSIZ];
  81. } DIR;
  82. #ifndef NULL
  83. #define NULL 0
  84. #endif
  85. extern    DIR *opendir();
  86. extern    struct direct *readdir();
  87. extern    closedir();
  88. #endif KERNEL
  89.  
  90. #endif BSD
  91.