dirwrap.h 7.02 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
/* dirwrap.h */

/* Directory system-call wrappers */

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
11
 * Copyright 2002 Rob Swindell - http://www.synchro.net/copyright.html		*
12
 *																			*
13
14
 * This library is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU Lesser General Public License		*
15
16
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
17
18
 * See the GNU Lesser General Public License for more details: lgpl.txt or	*
 * http://www.fsf.org/copyleft/lesser.html									*
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
 *																			*
 * Anonymous FTP access to the most recent released source is available at	*
 * ftp://vert.synchro.net, ftp://cvs.synchro.net and ftp://ftp.synchro.net	*
 *																			*
 * Anonymous CVS access to the development source and modification history	*
 * is available at cvs.synchro.net:/cvsroot/sbbs, example:					*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs login			*
 *     (just hit return, no password is necessary)							*
 * cvs -d :pserver:anonymous@cvs.synchro.net:/cvsroot/sbbs checkout src		*
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * You are encouraged to submit any modifications (preferably in Unix diff	*
 * format) via e-mail to mods@synchro.net									*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

#ifndef _DIRWRAP_H
#define _DIRWRAP_H

41
42
#include <stdlib.h>		/* _fullpath() on Win32 */

43
#if defined(__unix__)
44
45
46
	#include <sys/param.h>	/* PATH_MAX */
#endif

47
#include "gen_defs.h"	/* ulong */
48
#include "wrapdll.h"	/* DLLEXPORT and DLLCALL */
49

50
#if defined(__cplusplus)
51
52
53
54
55
56
57
extern "C" {
#endif

/****************/
/* RTL-specific */
/****************/

58
#if defined(__unix__)
59
60
61

	#define ALLFILES "*"	/* matches all files in a directory */
	#include <glob.h>		/* POSIX.2 directory pattern matching function */
62
	#define MKDIR(dir)		mkdir(dir,0777)
63

64
65
66
67
68
69
	#if defined(__OpenBSD__) && defined(_THREADWRAP_H)
		/* realpath() not threadsafe on OpenBSD */
	    #define FULLPATH(a,r,l) realpath_r(r,a)	/* defined in threadwrap.c */
	#else
		#define FULLPATH(a,r,l)	realpath(r,a)
	#endif
70
71
72
73

#else	

	#define ALLFILES "*.*"	/* matches all files in a directory */
rswindell's avatar
rswindell committed
74
75
76
77
78
79
	#ifdef __WATCOMC__
		#define MKDIR(dir)		mkdir(dir)
	#else
		#define MKDIR(dir)		_mkdir(dir)
	#endif
	#define FULLPATH(a,r,l)		_fullpath(a,r,l)
80

81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
	/* glob-compatible findfirst/findnext wrapper */

	typedef struct 
	{
			size_t gl_pathc;			/* Count of paths matched so far */
			char **gl_pathv;			/* List of matched pathnames. */
			size_t gl_offs;				/* Slots to reserve in 'gl_pathv'. */
	} glob_t;

	/* Bits set in the FLAGS argument to `glob'.  */
	#define GLOB_ERR        (1 << 0)	/* Return on read errors.  */
	#define GLOB_MARK       (1 << 1)	/* Append a slash to each name.  */
	#define GLOB_NOSORT     (1 << 2)	/* Don't sort the names.  */
	#define GLOB_DOOFFS     (1 << 3)	/* Insert PGLOB->gl_offs NULLs.  */
	#define GLOB_NOCHECK    (1 << 4)	/* If nothing matches, return the pattern.  */
	#define GLOB_APPEND     (1 << 5)	/* Append to results of a previous call.  */
	#define GLOB_NOESCAPE   (1 << 6)	/* Backslashes don't quote metacharacters.  */
	#define GLOB_PERIOD     (1 << 7)	/* Leading `.' can be matched by metachars.  */
	#define GLOB_MAGCHAR    (1 << 8)	/* Set in gl_flags if any metachars seen.  */
	#define GLOB_ALTDIRFUNC (1 << 9)	/* Use gl_opendir et al functions.  */
	#define GLOB_BRACE      (1 << 10)	/* Expand "{a,b}" to "a" "b".  */
	#define GLOB_NOMAGIC    (1 << 11)	/* If no magic chars, return the pattern.  */
	#define GLOB_TILDE      (1 << 12)	/* Expand ~user and ~ to home directories. */
	#define GLOB_ONLYDIR    (1 << 13)	/* Match only directories.  */
	#define GLOB_TILDE_CHECK (1 << 14)	/* Like GLOB_TILDE but return an error
										  if the user name is not available.  */
	/* Error returns from `glob'.  */
	#define GLOB_NOSPACE    1       /* Ran out of memory.  */
	#define GLOB_ABORTED    2       /* Read error.  */
	#define GLOB_NOMATCH    3       /* No matches found.  */
	#define GLOB_NOSYS      4       /* Not implemented.  */

	DLLEXPORT int	DLLCALL	glob(const char *pattern, int flags, void* unused, glob_t*);
	DLLEXPORT void	DLLCALL globfree(glob_t*);

#endif

/*****************************/
/* POSIX Directory Functions */
/*****************************/
#if defined(_MSC_VER)
122
123
	#include <io.h>		/* _finddata_t */

124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
	/* dirent structure returned by readdir().
	 */
	struct dirent
	{	
		char        d_name[260];	/* filename */
	};

	/* DIR type returned by opendir().  The members of this structure
	 * must not be accessed by application programs.
	 */
	typedef struct
	{
		char				filespec[260];
		struct dirent		dirent;
		long				handle;
		struct _finddata_t	finddata;
		BOOL				end;		/* End of directory flag */
	} DIR;


	/* Prototypes.
	 */
	DIR            *	opendir  (const char *__dirname);
	struct dirent  *	readdir  (DIR *__dir);
	int                 closedir (DIR *__dir);
	void                rewinddir(DIR *__dir);
rswindell's avatar
rswindell committed
150
151
#elif defined(__WATCOMC__)
	#include <direct.h>	/* opendir, etc defined here in Watcom */
152
153
#else
	#include <dirent.h>	/* POSIX directory functions */
154
155
#endif

156

157
158
159
160
161
162
163
164
165
166
/**********/
/* Macros */
/**********/

/* POSIX readdir convenience macro */
#ifndef DIRENT
#define DIRENT struct dirent	
#endif

#if defined(__unix__)
167
	#define BACKSLASH	'/'
168
#else /* MS-DOS based OS */
169
170
171
172
173
174
175
176
177
178
179
180
181
	#define BACKSLASH	'\\'
#endif

#if !defined(MAX_PATH)	/* maximum path length */
	#if defined MAXPATHLEN
		#define MAX_PATH MAXPATHLEN	/* clib.h */
	#elif defined PATH_MAX
		#define MAX_PATH PATH_MAX
	#elif defined _MAX_PATH
		#define MAX_PATH _MAX_PATH
	#else
		#define MAX_PATH 260		
	#endif
182
183
184
#endif

#if defined(_MSC_VER) || defined(__MINGW32__)
185
	#define CHMOD(s,m)		_chmod(s,m)
186
#elif defined(__BORLANDC__) && !defined(__unix__)
187
188
189
	#define CHMOD(p,a)		_rtl_chmod(p,1,a) 	/* _chmod obsolete in 4.x */
#else	
	#define CHMOD(s,m)		chmod(s,m)
190
191
192
#endif

/* General file system wrappers for all platforms and compilers */
193
DLLEXPORT BOOL		DLLCALL fexist(const char *filespec);
194
DLLEXPORT BOOL		DLLCALL fexistcase(char *filespec);	/* fixes upr/lwr case fname */
195
196
197
198
199
DLLEXPORT long		DLLCALL flength(const char *filename);
DLLEXPORT time_t	DLLCALL fdate(const char *filename);
DLLEXPORT BOOL		DLLCALL	isdir(const char *filename);
DLLEXPORT char*		DLLCALL getfname(const char* path);
DLLEXPORT int		DLLCALL getfattr(const char* filename);
200
DLLEXPORT ulong		DLLCALL getfreediskspace(const char* path, ulong unit);
201
DLLEXPORT ulong		DLLCALL delfiles(char *inpath, char *spec);
202

203
204
205
206
207
#if defined(__unix__)
DLLEXPORT void DLLCALL _splitpath(const char *path, char *drive, char *dir, 
								  char *fname, char *ext);
#endif

208
#if defined(__cplusplus)
209
210
211
212
}
#endif

#endif	/* Don't add anything after this line */