xref: /csrg-svn/share/man/man5/dir.5 (revision 69189)
161609Sbostic.\" Copyright (c) 1983, 1991, 1993
261609Sbostic.\"	The Regents of the University of California.  All rights reserved.
320797Smckusick.\"
448832Scael.\" %sccs.include.redist.man%
520798Smckusick.\"
6*69189Smckusick.\"     @(#)dir.5	8.4 (Berkeley) 05/03/95
748832Scael.\"
848832Scael.Dd
948832Scael.Dt DIR 5
1048832Scael.Os BSD 4.2
1148832Scael.Sh NAME
1248832Scael.Nm dir ,
1348832Scael.Nm dirent
1448832Scael.Nd directory file format
1548832Scael.Sh SYNOPSIS
1648832Scael.Fd #include <sys/types.h>
1748832Scael.Fd #include <sys/dir.h>
1848832Scael.Sh DESCRIPTION
1965101SmckusickDirectories provide a convenient hierarchical method of grouping
2048832Scaelfiles while obscuring the underlying details of the storage medium.
2148832ScaelA directory file is differentiated from a plain file
2248832Scaelby a flag in its
2348832Scael.Xr inode 5
2448832Scaelentry.
2565101SmckusickIt consists of records (directory entries) each of which contains
2648832Scaelinformation about a file and a pointer to the file itself.
2748832ScaelDirectory entries may contain other directories
2848832Scaelas well as plain files; such nested directories are refered to as
2948832Scaelsubdirectories.
3048832ScaelA hierarchy of directories and files is formed in this manner
3166971Sbosticand is called a file system (or referred to as a file system tree).
3248832Scael.\" An entry in this tree,
3348832Scael.\" nested or not nested,
3448832Scael.\" is a pathname.
3548832Scael.Pp
3648832ScaelEach directory file contains two special directory entries; one is a pointer
3748832Scaelto the directory itself
3848832Scaelcalled dot
3948832Scael.Ql \&.
4048832Scaeland the other a pointer to its parent directory called dot-dot
4148832Scael.Ql \&.. .
4248832ScaelDot and dot-dot
4348832Scaelare valid pathnames, however,
4448832Scaelthe system root directory
4548832Scael.Ql / ,
4648832Scaelhas no parent and dot-dot points to itself like dot.
4748832Scael.Pp
4848832ScaelFile system nodes are ordinary directory files on which has
4948832Scaelbeen grafted a file system object, such as a physical disk or a
5048832Scaelpartitioned area of such a disk.
5148832Scael(See
5248832Scael.Xr mount 1
5348832Scaeland
5448832Scael.Xr mount 8 . )
5548832Scael.Pp
5648832ScaelThe directory entry format is defined in the file
5748832Scael.Aq dirent.h :
5848832Scael.Bd -literal
5948832Scael#ifndef _DIRENT_H_
6048832Scael#define _DIRENT_H_
6148832Scael
6220798Smckusick/*
63*69189Smckusick * The dirent structure defines the format of directory entries returned by
64*69189Smckusick * the getdirentries(2) system call.
65*69189Smckusick *
66*69189Smckusick * A directory entry has a struct dirent at the front of it, containing its
67*69189Smckusick * inode number, the length of the entry, and the length of the name
68*69189Smckusick * contained in the entry.  These are followed by the name padded to a 4
69*69189Smckusick * byte boundary with null bytes.  All names are guaranteed null terminated.
70*69189Smckusick * The maximum length of a name in a directory is MAXNAMLEN.
71*69189Smckusick */
7248832Scael
7348832Scaelstruct dirent {
74*69189Smckusick	u_int32_t d_fileno;		/* file number of entry */
75*69189Smckusick	u_int16_t d_reclen;		/* length of this record */
76*69189Smckusick	u_int8_t  d_type; 		/* file type, see below */
77*69189Smckusick	u_int8_t  d_namlen;		/* length of string in d_name */
7848832Scael#ifdef _POSIX_SOURCE
79*69189Smckusick	char	d_name[255 + 1];	/* name must be no longer than this */
8020798Smckusick#else
81*69189Smckusick#define	MAXNAMLEN	255
82*69189Smckusick	char	d_name[MAXNAMLEN + 1];	/* name must be no longer than this */
8320797Smckusick#endif
8448832Scael};
8520798Smckusick
86*69189Smckusick/*
87*69189Smckusick * File types
88*69189Smckusick */
89*69189Smckusick#define	DT_UNKNOWN	 0
90*69189Smckusick#define	DT_FIFO		 1
91*69189Smckusick#define	DT_CHR		 2
92*69189Smckusick#define	DT_DIR		 4
93*69189Smckusick#define	DT_BLK		 6
94*69189Smckusick#define	DT_REG		 8
95*69189Smckusick#define	DT_LNK		10
96*69189Smckusick#define	DT_SOCK		12
97*69189Smckusick#define	DT_WHT		14
98*69189Smckusick
99*69189Smckusick/*
100*69189Smckusick * Convert between stat structure types and directory types.
101*69189Smckusick */
102*69189Smckusick#define	IFTODT(mode)	(((mode) & 0170000) >> 12)
103*69189Smckusick#define	DTTOIF(dirtype)	((dirtype) << 12)
104*69189Smckusick
10548832Scael#ifdef _POSIX_SOURCE
10648832Scaeltypedef void *	DIR;
10748832Scael#else
10820798Smckusick
10948832Scael#define	d_ino		d_fileno	/* backward compatibility */
11020798Smckusick
11148832Scael/* definitions for library routines operating on directories. */
11248832Scael#define	DIRBLKSIZ	1024
11348832Scael
11448832Scael/* structure describing an open directory. */
11548832Scaeltypedef struct _dirdesc {
116*69189Smckusick	int	dd_fd;		/* file descriptor associated with directory */
117*69189Smckusick	long	dd_loc;		/* offset in current buffer */
118*69189Smckusick	long	dd_size;	/* amount of data returned by getdirentries */
119*69189Smckusick	char	*dd_buf;	/* data buffer */
120*69189Smckusick	int	dd_len;		/* size of data buffer */
121*69189Smckusick	long	dd_seek;	/* magic cookie returned by getdirentries */
122*69189Smckusick	long	dd_rewind;	/* magic cookie for rewinding */
123*69189Smckusick	int	dd_flags;	/* flags for readdir */
12448832Scael} DIR;
12548832Scael
12648832Scael#define	dirfd(dirp)	((dirp)->dd_fd)
12748832Scael
128*69189Smckusick/* flags for opendir2 */
129*69189Smckusick#define DTF_HIDEW	0x0001	/* hide whiteout entries */
130*69189Smckusick#define DTF_NODUP	0x0002	/* don't return duplicate names */
131*69189Smckusick#define DTF_REWIND	0x0004	/* rewind after reading union stack */
132*69189Smckusick#define __DTF_READALL	0x0008	/* everything has been read */
133*69189Smckusick
13448832Scael#ifndef NULL
13548832Scael#define	NULL	0
13648832Scael#endif
13748832Scael
13848832Scael#endif /* _POSIX_SOURCE */
13948832Scael
14048832Scael#ifndef KERNEL
14148832Scael
14248832Scael#include <sys/cdefs.h>
14348832Scael
144*69189Smckusick__BEGIN_DECLS
145*69189SmckusickDIR *opendir __P((const char *));
146*69189Smckusickstruct dirent *readdir __P((DIR *));
147*69189Smckusickvoid rewinddir __P((DIR *));
148*69189Smckusickint closedir __P((DIR *));
149*69189Smckusick#ifndef _POSIX_SOURCE
150*69189SmckusickDIR *__opendir2 __P((const char *, int));
151*69189Smckusicklong telldir __P((const DIR *));
152*69189Smckusickvoid seekdir __P((DIR *, long));
153*69189Smckusickint scandir __P((const char *, struct dirent ***,
154*69189Smckusick    int (*)(struct dirent *), int (*)(const void *, const void *)));
155*69189Smckusickint alphasort __P((const void *, const void *));
156*69189Smckusickint getdirentries __P((int, char *, int, long *));
157*69189Smckusick#endif /* not POSIX */
158*69189Smckusick__END_DECLS
159*69189Smckusick
16048832Scael#endif /* !KERNEL */
16148832Scael
16248832Scael#endif /* !_DIRENT_H_ */
16348832Scael.Ed
16448832Scael.Sh SEE ALSO
16548832Scael.Xr fs 5
16648832Scael.Xr inode 5
16748832Scael.Sh HISTORY
16848832ScaelA
16948832Scael.Nm
17048832Scaelfile format appeared in
17148832Scael.At v7 .
172