Synchronet now requires the libarchive development package (e.g. libarchive-dev on Debian-based Linux distros, libarchive.org for more info) to build successfully.

dirwrap.h 7.89 KB
Newer Older
1 2 3 4 5 6
/* Directory system-call wrappers */

/****************************************************************************
 * @format.tab-size 4		(Plain Text/Source Code File Header)			*
 * @format.use-tabs true	(see http://www.synchro.net/ptsc_hdr.html)		*
 *																			*
7
 * Copyright Rob Swindell - http://www.synchro.net/copyright.html			*
8
 *																			*
9 10
 * This library is free software; you can redistribute it and/or			*
 * modify it under the terms of the GNU Lesser General Public License		*
11 12
 * as published by the Free Software Foundation; either version 2			*
 * of the License, or (at your option) any later version.					*
13 14
 * See the GNU Lesser General Public License for more details: lgpl.txt or	*
 * http://www.fsf.org/copyleft/lesser.html									*
15 16 17 18 19 20 21 22 23 24
 *																			*
 * For Synchronet coding style and modification guidelines, see				*
 * http://www.synchro.net/source.html										*
 *																			*
 * Note: If this box doesn't appear square, then you need to fix your tabs.	*
 ****************************************************************************/

#ifndef _DIRWRAP_H
#define _DIRWRAP_H

25 26
#include <stdlib.h>		/* _fullpath() on Win32 */

27
#if defined(__unix__)
28 29 30
	#include <sys/param.h>	/* PATH_MAX */
#endif

31 32 33 34
#if defined(INCLUDE_PATHS_H)
	#include <paths.h>		/* _PATHS_* macros */
#endif

35
#include "gen_defs.h"	/* ulong */
36
#include "wrapdll.h"	/* DLLEXPORT */
37

38
#if defined(__cplusplus)
39 40 41
extern "C" {
#endif

42 43
#define ALLFILES "*"	/* matches all files in a directory */

44 45 46 47
/****************/
/* RTL-specific */
/****************/

48
#if defined(__unix__)
49

50 51
	#include <sys/types.h>
	#include <sys/stat.h>
52
	#include <glob.h>		/* POSIX.2 directory pattern matching function */
53
	#define MKDIR(dir)		mkdir(dir,0777)
54

55 56 57 58
	#if defined(__CYGWIN__)
		#define DLLEXPORT	/* CygWin's glob.h #undef's DLLEXPORT */
	#endif

59 60 61 62 63 64 65 66
#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.  */
67

68 69
	#include <direct.h>		/* mkdir() */

rswindell's avatar
rswindell committed
70 71 72 73 74
	#ifdef __WATCOMC__
		#define MKDIR(dir)		mkdir(dir)
	#else
		#define MKDIR(dir)		_mkdir(dir)
	#endif
75

76 77 78 79 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
	/* 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.  */

108 109
	DLLEXPORT int	glob(const char *pattern, int flags, void* unused, glob_t*);
	DLLEXPORT void	globfree(glob_t*);
110 111 112

#endif

113 114
#define FULLPATH(a,r,l)		_fullpath(a,r,l)

115 116 117
/*****************************/
/* POSIX Directory Functions */
/*****************************/
118
#if defined(_MSC_VER) || defined(__DMC__)
119 120
	#include <io.h>		/* _finddata_t */

121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142
	/* 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.
	 */
143 144 145 146
	DLLEXPORT DIR* opendir  (const char *__dirname);
	DLLEXPORT struct dirent* readdir  (DIR *__dir);
	DLLEXPORT int closedir (DIR *__dir);
	DLLEXPORT void rewinddir(DIR *__dir);
147
#elif !defined(__WATCOMC__)
148
	#include <dirent.h>	/* POSIX directory functions */
149 150
#endif

151

152 153 154 155 156 157 158 159 160 161
/**********/
/* Macros */
/**********/

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

#if defined(__unix__)
162 163 164 165 166 167 168 169 170 171 172
	#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

173
#else /* MS-DOS based OS */
174 175 176 177 178 179

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

180 181 182 183 184 185 186 187 188 189 190 191
#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
192 193 194
#endif

#if defined(_MSC_VER) || defined(__MINGW32__)
195
	#define CHMOD(s,m)		_chmod(s,m)
196
#elif defined(__BORLANDC__) && !defined(__unix__)
197 198 199
	#define CHMOD(p,a)		_rtl_chmod(p,1,a) 	/* _chmod obsolete in 4.x */
#else	
	#define CHMOD(s,m)		chmod(s,m)
200 201 202
#endif

/* General file system wrappers for all platforms and compilers */
203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226
DLLEXPORT BOOL		fexist(const char *filespec);
DLLEXPORT BOOL		fexistcase(char *filespec);	/* fixes upr/lwr case fname */
DLLEXPORT off_t		flength(const char *filename);
DLLEXPORT time_t	fdate(const char *filename);
DLLEXPORT time_t	fcdate(const char* filename);
DLLEXPORT int		setfdate(const char* filename, time_t t);
DLLEXPORT BOOL		isdir(const char *filename);
DLLEXPORT BOOL		isabspath(const char *filename);
DLLEXPORT BOOL		isfullpath(const char* filename);
DLLEXPORT char*		getfname(const char* path);
DLLEXPORT char*		getfext(const char* path);
DLLEXPORT int		getfattr(const char* filename);
DLLEXPORT int		getfmode(const char* filename);
DLLEXPORT ulong		getfilecount(const char *path);
DLLEXPORT char*		getdirname(const char* path);
DLLEXPORT long		getdirsize(const char* path, BOOL include_subdirs, BOOL subdir_only);
DLLEXPORT ulong		getdisksize(const char* path, ulong unit);
DLLEXPORT ulong		getfreediskspace(const char* path, ulong unit);
DLLEXPORT uint64_t	getfilesizetotal(const char *path);
DLLEXPORT long		delfiles(const char *inpath, const char *spec, size_t keep);
DLLEXPORT char*		backslash(char* path);
DLLEXPORT BOOL 		wildmatch(const char *fname, const char *spec, BOOL path, BOOL case_sensitive);
DLLEXPORT BOOL 		wildmatchi(const char *fname, const char *spec, BOOL path);
DLLEXPORT int		mkpath(const char* path);
227

228

229
#if defined(__unix__)
230
DLLEXPORT void _splitpath(const char *path, char *drive, char *dir, 
231
								  char *fname, char *ext);
232 233
DLLEXPORT char * _fullpath(char *target, const char *path, size_t size);
DLLEXPORT int removecase(const char *path);
234 235
#else
	#define	removecase(x)	remove(x)
236 237
#endif

238 239 240 241
#if !defined _WIN32
DLLEXPORT BOOL		CopyFile(const char* src, const char* dest, BOOL failIfExists);
#endif

242
#if defined(__cplusplus)
243 244 245 246
}
#endif

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