1 /****************************************************************************
2 
3 	Directory.h
4 
5 	This file contains the C definitions and declarations for the
6 	Directory.c directory iteration code.
7 
8 	This code is intended to be used as a convenient, machine
9 	independent interface to iterate through the contents of a
10 	directory.
11 
12  ****************************************************************************/
13 
14 /*
15  * Author:
16  * 	Brian Totty
17  * 	Department of Computer Science
18  * 	University Of Illinois at Urbana-Champaign
19  *	1304 West Springfield Avenue
20  * 	Urbana, IL 61801
21  *
22  * 	totty@cs.uiuc.edu
23  *
24  */
25 
26 #ifndef _DIRECTORY_H_
27 #define _DIRECTORY_H_
28 
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/param.h>
33 #include <sys/types.h>
34 #include <sys/stat.h>
35 #include <unistd.h>
36 
37 #if defined(SYSV) || defined(SVR4)
38 #define getwd(path) getcwd(path, MAXPATHLEN)
39 #endif
40 
41 #ifndef	NO_DIRENT
42 #include <dirent.h>
43 #else
44 #include <sys/dir.h>
45 #define	dirent direct
46 #endif
47 
48 #if defined(__DragonFly__) && !defined(MAXNAMLEN)
49 #define MAXNAMLEN 255
50 #endif
51 
52 #ifndef _SYS_NAME_MAX
53 #ifndef MAXNAMLEN
54 ERROR, ONE OF THESE MUST BE DEFINED
55 #else
56 #define	MAX_NAME_LENGTH	MAXNAMLEN
57 #endif
58 #else
59 #define	MAX_NAME_LENGTH	_SYS_NAME_MAX
60 #endif
61 
62 #ifndef TRUE
63 #define TRUE				1
64 #endif
65 
66 #ifndef FALSE
67 #define	FALSE				0
68 #endif
69 
70 #define	PERM_READ			4
71 #define	PERM_WRITE			2
72 #define	PERM_EXECUTE			1
73 
74 #define	F_TYPE_DIR			1
75 #define	F_TYPE_FILE			2
76 #define	F_TYPE_CHAR_SPECIAL		3
77 #define	F_TYPE_BLOCK_SPECIAL		4
78 #define	F_TYPE_SYM_LINK			5
79 #define	F_TYPE_SOCKET			6
80 #define	F_TYPE_FIFO			7
81 
82 /*--------------------------------------------------------------------------*
83 
84             D A T A    T Y P E    A C C E S S    M A C R O S
85 
86  *--------------------------------------------------------------------------*/
87 
88 	/* Directory: Directory Iterator */
89 
90 #define	DirectoryDir(dp)		((dp)->filep)
91 #define	DirectoryPath(dp)		((dp)->path)
92 
93 	/* FileInfo: Information About A File Or Link */
94 
95 #define	FileInfoProt(fi)		((fi)->protections)
96 #define FileInfoOrigMode(fi)		((fi)->orig_mode)
97 #define	FileInfoUserID(fi)		((fi)->user_id)
98 #define	FileInfoGroupID(fi)		((fi)->group_id)
99 #define	FileInfoFileSize(fi)		((fi)->size)
100 #define	FileInfoLastAccess(fi)		((fi)->last_access)
101 #define	FileInfoLastModify(fi)		((fi)->last_modify)
102 #define	FileInfoLastStatusChange(fi)	((fi)->last_status_change)
103 
104 #define	FIProt(fi)			FileInfoProt(fi)
105 #define FIOrigMode(fi)			FileInfoOrigMode(fi)
106 #define	FIUserID(fi)			FileInfoUserID(fi)
107 #define	FIGroupID(fi)			FileInfoGroupID(fi)
108 #define	FIFileSize(fi)			FileInfoFileSize(fi)
109 #define	FILastAccess(fi)		FileInfoLastAccess(fi)
110 #define	FILastModify(fi)		FileInfoLastModify(fi)
111 #define	FILastStatusChange(fi)		FileInfoLastStatusChange(fi)
112 
113 	/* FType: File Type Macros */
114 
115 #define	FTypeIsDir(ft)			((ft) == F_TYPE_DIR)
116 #define	FTypeIsFile(ft)			((ft) == F_TYPE_FILE)
117 #define	FTypeIsCharSpecial(ft)		((ft) == F_TYPE_CHAR_SPECIAL)
118 #define	FTypeIsBlockSpecial(ft)		((ft) == F_TYPE_BLOCK_SPECIAL)
119 #define	FTypeIsSymLink(ft)		((ft) == F_TYPE_SYM_LINK)
120 #define	FTypeIsSocket(ft)		((ft) == F_TYPE_SOCKET)
121 #define	FTypeIsFifo(ft)			((ft) == F_TYPE_FIFO)
122 
123 	/* DirEntry: Information About A Item In A Directory */
124 
125 #define	DirEntryFileName(fi)		((fi)->filename)
126 #define	DirEntryType(fi)		((fi)->file_type)
127 #define	DirEntrySelfInfo(fi)		(&((fi)->self_info))
128 #define	DirEntryActualInfo(fi)		(&((fi)->actual_info))
129 
130 #define	DirEntryIsBrokenLink(fi)	((fi)->broken_link)
131 #define	DirEntryIsDirectoryLink(fi)	((fi)->directory_link)
132 #define	DirEntryIsDir(fi)		(FTypeIsDir(DirEntryType(fi)))
133 #define	DirEntryIsFile(fi)		(FTypeIsFile(DirEntryType(fi)))
134 #define	DirEntryIsCharSpecial(fi)	(FTypeIsCharSpecial(DirEntryType(fi)))
135 #define	DirEntryIsBlockSpecial(fi)	(FTypeIsBlockSpecial(DirEntryType(fi)))
136 #define	DirEntryIsSymLink(fi)		(FTypeIsSymLink(DirEntryType(fi)))
137 #define	DirEntryIsSocket(fi)		(FTypeIsSocket(DirEntryType(fi)))
138 #define	DirEntryIsFifo(fi)		(FTypeIsFifo(DirEntryType(fi)))
139 #define	DirEntryLeadsToDir(fi)		(DirEntryIsDir(fi) ||		\
140 					 DirEntryIsDirectoryLink(fi))
141 
142 #define	DirEntryProt(d)			FIProt(DirEntrySelfInfo(d))
143 #define DirEntryOrigMode(d)		FIOrigMode(DirEntrySelfInfo(d))
144 #define	DirEntryUserID(d)		FIUserID(DirEntrySelfInfo(d))
145 #define	DirEntryGroupID(d)		FIGroupID(DirEntrySelfInfo(d))
146 #define	DirEntryFileSize(d)		FIFileSize(DirEntrySelfInfo(d))
147 #define	DirEntryLastAccess(d)		FILastAccess(DirEntrySelfInfo(d))
148 #define	DirEntryLastModify(d)		FILastModify(DirEntrySelfInfo(d))
149 #define	DirEntryLastStatusChange(d)	FILastStatusChange(DirEntrySelfInfo(d))
150 
151 /*--------------------------------------------------------------------------*
152 
153              D A T A    T Y P E    D E F I N I T I O N S
154 
155  *--------------------------------------------------------------------------*/
156 
157 	/* Directory: Directory Iterator */
158 
159 typedef struct
160 {
161 	DIR *filep;
162 	char path[MAXPATHLEN + 2];
163 } DIRECTORY;
164 
165 typedef DIRECTORY Directory;
166 
167 	/* FileInfo: Information About A File Or Link */
168 
169 typedef struct
170 {
171 	short protections;
172 	short orig_mode;
173 	short user_id;
174 	short group_id;
175 	long size;
176 	time_t last_access;
177 	time_t last_modify;
178 	time_t last_status_change;
179 } FILE_INFO;
180 
181 typedef	FILE_INFO FileInfo;
182 
183 	/* DirEntry: Information About A Item In A Directory */
184 
185 typedef struct
186 {
187 	char filename[MAX_NAME_LENGTH + 1];
188 	short file_type;
189 	short broken_link;
190 	short directory_link;
191 	FileInfo self_info;
192 	FileInfo actual_info;
193 } DIR_ENTRY;
194 
195 typedef DIR_ENTRY DirEntry;
196 
197 /*--------------------------------------------------------------------------*
198 
199         L O W    L E V E L    D I R E C T O R Y    I N T E R F A C E
200 
201  *--------------------------------------------------------------------------*/
202 
203 #if (!NeedFunctionPrototypes)
204 
205 int	DirectoryOpen();
206 void	DirectoryRestart();
207 void	DirectoryClose();
208 long	DirectoryTellPosition();
209 void	DirectorySetPosition();
210 int	DirectoryReadNextEntry();
211 char *	DirectoryPathExpand();
212 void	DirEntryDump();
213 
214 #else
215 
216 int	DirectoryOpen(char *dir_name, Directory *dp);
217 void	DirectoryRestart(Directory *dp);
218 void	DirectoryClose(Directory *dp);
219 long	DirectoryTellPosition(Directory *dp);
220 void	DirectorySetPosition(Directory *dp, long int pos);
221 int	DirectoryReadNextEntry(Directory *dp, DirEntry *de);
222 char *	DirectoryPathExpand(char *old_path, char *new_path);
223 void	DirEntryDump(FILE *fp, DirEntry *de);
224 
225 #endif
226 
227 #endif
228 
229 
230