dirwrap.h 8.64 KB
Newer Older
1
/* Directory system-call wrappers */
2
// vi: tabstop=4
3
4
5
6
7
8
9

/* $Id$ */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
10
 * Copyright 2011 Rob Swindell - http://www.synchro.net/copyright.html		*
11
 *																			*
12
13
 * This library is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU Lesser General Public License		*
14
15
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
16
17
 * See the GNU Lesser General Public License for more details: lgpl.txt or	*
 * http://www.fsf.org/copyleft/lesser.html									*
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
 *																			*
 * 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

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

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

46
47
48
49
#if defined(INCLUDE_PATHS_H)
	#include <paths.h>		/* _PATHS_* macros */
#endif

50
#include "gen_defs.h"	/* ulong */
51
#include "wrapdll.h"	/* DLLEXPORT and DLLCALL */
52

53
#if defined(__cplusplus)
54
55
56
57
58
59
60
extern "C" {
#endif

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

61
#if defined(__unix__)
62
63

	#define ALLFILES "*"	/* matches all files in a directory */
64
65
	#include <sys/types.h>
	#include <sys/stat.h>
66
	#include <glob.h>		/* POSIX.2 directory pattern matching function */
67
	#define MKDIR(dir)		mkdir(dir,0777)
68

69
70
71
72
	#if defined(__CYGWIN__)
		#define DLLEXPORT	/* CygWin's glob.h #undef's DLLEXPORT */
	#endif

73
74
75
76
77
78
79
80
#else

	/* Values for the second argument to access.
	   These may be OR'd together.  */
	#define R_OK    4               /* Test for read permission.  */
	#define W_OK    2               /* Test for write permission.  */
	#define X_OK    1               /* Test for execute permission.  */
	#define F_OK    0               /* Test for existence.  */
81

82
83
	#include <direct.h>		/* mkdir() */

84
	#define ALLFILES "*.*"	/* matches all files in a directory */
rswindell's avatar
rswindell committed
85
86
87
88
89
	#ifdef __WATCOMC__
		#define MKDIR(dir)		mkdir(dir)
	#else
		#define MKDIR(dir)		_mkdir(dir)
	#endif
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
122
123
124
125
126
127
	/* 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

128
129
#define FULLPATH(a,r,l)		_fullpath(a,r,l)

130
131
132
/*****************************/
/* POSIX Directory Functions */
/*****************************/
133
#if defined(_MSC_VER) || defined(__DMC__)
134
135
	#include <io.h>		/* _finddata_t */

136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
	/* 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.
	 */
deuce's avatar
deuce committed
158
159
160
161
	DLLEXPORT DIR* DLLCALL opendir  (const char *__dirname);
	DLLEXPORT struct dirent* DLLCALL readdir  (DIR *__dir);
	DLLEXPORT int DLLCALL closedir (DIR *__dir);
	DLLEXPORT void DLLCALL rewinddir(DIR *__dir);
162
#elif !defined(__WATCOMC__)
163
	#include <dirent.h>	/* POSIX directory functions */
164
165
#endif

166

167
168
169
170
171
172
173
174
175
176
/**********/
/* Macros */
/**********/

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

#if defined(__unix__)
177
178
179
180
181
182
183
184
185
186
187
	#define PATH_DELIM			'/'
	#define IS_PATH_DELIM(x)	(x=='/')

	/* These may be pre-defined in paths.h (BSD) */
	#ifndef _PATH_TMP
	#define _PATH_TMP			"/tmp/"
	#endif
	#ifndef _PATH_DEVNULL
	#define _PATH_DEVNULL		"/dev/null"
	#endif

188
#else /* MS-DOS based OS */
189
190
191
192
193
194

	#define PATH_DELIM			'\\'
	#define IS_PATH_DELIM(x)	((x)=='/' || (x)=='\\')
	#define _PATH_TMP			getenv("TEMP")
	#define _PATH_DEVNULL		"NUL"

195
196
197
198
199
200
201
202
203
204
205
206
#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
207
208
209
#endif

#if defined(_MSC_VER) || defined(__MINGW32__)
210
	#define CHMOD(s,m)		_chmod(s,m)
211
#elif defined(__BORLANDC__) && !defined(__unix__)
212
213
214
	#define CHMOD(p,a)		_rtl_chmod(p,1,a) 	/* _chmod obsolete in 4.x */
#else	
	#define CHMOD(s,m)		chmod(s,m)
215
216
217
#endif

/* General file system wrappers for all platforms and compilers */
218
DLLEXPORT BOOL		DLLCALL fexist(const char *filespec);
219
DLLEXPORT BOOL		DLLCALL fexistcase(char *filespec);	/* fixes upr/lwr case fname */
220
DLLEXPORT off_t		DLLCALL flength(const char *filename);
221
DLLEXPORT time_t	DLLCALL fdate(const char *filename);
222
DLLEXPORT time_t	DLLCALL fcdate(const char* filename);
223
DLLEXPORT int		DLLCALL setfdate(const char* filename, time_t t);
224
DLLEXPORT BOOL		DLLCALL	isdir(const char *filename);
225
226
DLLEXPORT BOOL		DLLCALL	isabspath(const char *filename);
DLLEXPORT BOOL		DLLCALL isfullpath(const char* filename);
227
DLLEXPORT char*		DLLCALL getfname(const char* path);
228
DLLEXPORT char*		DLLCALL getfext(const char* path);
229
DLLEXPORT int		DLLCALL getfattr(const char* filename);
230
DLLEXPORT ulong		DLLCALL getfilecount(const char *inpath, const char* spec);
231
DLLEXPORT long		DLLCALL	getdirsize(const char* path, BOOL include_subdirs, BOOL subdir_only);
232
DLLEXPORT ulong		DLLCALL getdisksize(const char* path, ulong unit);
233
DLLEXPORT ulong		DLLCALL getfreediskspace(const char* path, ulong unit);
234
DLLEXPORT ulong		DLLCALL delfiles(const char *inpath, const char *spec);
235
DLLEXPORT char*		DLLCALL backslash(char* path);
236
DLLEXPORT BOOL 		DLLCALL wildmatch(const char *fname, const char *spec, BOOL path);
237
DLLEXPORT BOOL 		DLLCALL wildmatchi(const char *fname, const char *spec, BOOL path);
rswindell's avatar
rswindell committed
238
DLLEXPORT int		DLLCALL	mkpath(const char* path);
239

240

241
242
243
#if defined(__unix__)
DLLEXPORT void DLLCALL _splitpath(const char *path, char *drive, char *dir, 
								  char *fname, char *ext);
244
DLLEXPORT char * DLLCALL _fullpath(char *target, const char *path, size_t size);
rswindell's avatar
rswindell committed
245
DLLEXPORT int DLLCALL removecase(const char *path);
246
247
#else
	#define	removecase(x)	remove(x)
248
249
#endif

250
#if defined(__cplusplus)
251
252
253
254
}
#endif

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