1 /*  MikMod module player
2 	(c) 1998 - 2014 Miodrag Vallat and others - see file AUTHORS for
3 	complete list.
4 
5 	This program is free software; you can redistribute it and/or modify
6 	it under the terms of the GNU General Public License as published by
7 	the Free Software Foundation; either version 2 of the License, or
8 	(at your option) any later version.
9 
10 	This program is distributed in the hope that it will be useful,
11 	but WITHOUT ANY WARRANTY; without even the implied warranty of
12 	MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13 	GNU General Public License for more details.
14 
15 	You should have received a copy of the GNU General Public License
16 	along with this program; if not, write to the Free Software
17 	Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
18 	02111-1307, USA.
19 */
20 
21 /*==============================================================================
22 
23   Some utility functions
24 
25 ==============================================================================*/
26 
27 #ifndef MUTILITIES_H
28 #define MUTILITIES_H
29 
30 #ifdef _WIN32
31 #include <windows.h>
32 #endif
33 
34 #if defined(__OS2__)||defined(__EMX__)
35 #include <os2.h>
36 #ifndef HAVE_CONFIG_H
37 #define RETSIGTYPE void
38 #endif
39 #endif
40 
41 #if defined(__MORPHOS__) || defined(__AROS__) || defined(AMIGAOS)	|| \
42     defined(__amigaos__) || defined(__amigados__)			|| \
43     defined(AMIGA) || defined(_AMIGA) || defined(__AMIGA__)
44 #include <exec/types.h>
45 #define _mikmod_amiga 1
46 #endif
47 
48 #include <mikmod.h>	/* for BOOL */
49 
50 /*========== Constants */
51 
52 #ifdef HAVE_SYS_PARAM_H
53 #include <sys/param.h>
54 #endif
55 #ifdef HAVE_LIMITS_H
56 #include <limits.h>
57 #endif
58 
59 #ifndef PATH_MAX
60 #if defined(MAXPATHLEN) /* <sys/param.h> */
61 #define PATH_MAX MAXPATHLEN
62 #elif defined(_WIN32) && defined(_MAX_PATH)
63 #define PATH_MAX _MAX_PATH
64 #elif defined(_WIN32) && defined(MAX_PATH)
65 #define PATH_MAX MAX_PATH
66 #elif defined(__OS2__) && defined(CCHMAXPATH)
67 #define PATH_MAX CCHMAXPATH
68 #else
69 #define PATH_MAX 256
70 #endif
71 #endif /* PATH_MAX */
72 
73 #include <string.h>
74 
75 #define PATH_SEP '/'
76 #define PATH_SEP_STR "/"
77 
78 #if defined(__OS2__)||defined(__EMX__)||defined(__DJGPP__)||defined(_WIN32)
79 
80 #define PATH_SEP_SYS '\\'
81 #define PATH_SEP_SYS_STR "\\"
82 void path_conv(char *file);
83 char *path_conv_sys(const char *file);
84 char *path_conv_sys2(const char *file);
85 
86 #else
87 
88 #define PATH_SEP_SYS '/'
89 #define PATH_SEP_SYS_STR "/"
90 #define path_conv(file)
91 #define path_conv_sys(file) (file)
92 #define path_conv_sys2(file) (file)
93 
94 #endif
95 
96 #ifdef _mikmod_amiga
97 #define IS_PATH_SEP(c) ((c) == PATH_SEP || (c) == ':')
FIND_FIRST_DIRSEP(const char * _the_path)98 static inline char *FIND_FIRST_DIRSEP(const char *_the_path) {
99     char *p = strchr(_the_path, ':');
100     if (p != NULL) return p;
101     return strchr(_the_path, PATH_SEP);
102 }
FIND_LAST_DIRSEP(const char * _the_path)103 static inline char *FIND_LAST_DIRSEP (const char *_the_path) {
104     char *p = strrchr(_the_path, PATH_SEP);
105     if (p != NULL) return p;
106     return strchr(_the_path, ':');
107 }
108 #else
109 #define IS_PATH_SEP(c) ((c) == PATH_SEP)
110 #define FIND_FIRST_DIRSEP(p) strchr((p), PATH_SEP)
111 #define FIND_LAST_DIRSEP(p) strrchr((p), PATH_SEP)
112 #endif
113 
114 /*========== Types */
115 
116 /* pointer-sized signed int (ssize_t/intptr_t) : */
117 #if defined(_WIN64) /* win64 is LLP64, not LP64  */
118 typedef long long       SINTPTR_T;
119 #else
120 /* long should be pointer-sized for all others : */
121 typedef long            SINTPTR_T;
122 #endif
123 
124 /*========== Variables */
125 
126 /* storage buffer length - used everywhere */
127 #define STORAGELEN	320
128 extern char storage[STORAGELEN+2];
129 
130 /*========== Routines and macros */
131 
132 #undef MIN
133 #define MIN(a, b) ((a) < (b) ? (a) : (b))
134 
135 #define BTST(v, m) ((v) & (m) ? 1 : 0)
136 
137 #ifdef _WIN32
138 #define stat              _stat
139 #ifndef S_ISDIR
140 #define S_ISDIR(st_mode)  ((st_mode & _S_IFDIR) == _S_IFDIR)
141 #endif
142 #ifndef S_ISCHR
143 #define S_ISCHR(st_mode)  ((st_mode & _S_IFCHR) == _S_IFCHR)
144 #endif
145 #ifndef S_ISFIFO
146 #define S_ISFIFO(st_mode) ((st_mode & _S_IFIFO) == _S_IFIFO)
147 #endif
148 #endif
149 
150 #if defined(__EMX__)||defined(_WIN32)
151 #undef S_ISBLK /* MinGW sys/stat.h does define S_ISBLK */
152 #define S_ISBLK(st_mode)  0
153 #endif
154 
155 #if defined(__OS2__)||defined(__EMX__)||defined(__DJGPP__)||defined(_WIN32)
156 #undef S_ISLNK /* djgpp-v2.04 does define S_ISLNK (and has lstat, too..) */
157 #define lstat             stat
158 #define S_ISSOCK(st_mode) 0
159 #define S_ISLNK(st_mode)  0
160 #endif
161 
162 #if defined(_WIN32)&&!defined(__MINGW32__)&&!defined(__WATCOMC__)
163 typedef struct dirent {
164 	char name[PATH_MAX+1];
165 	unsigned long* handle;
166 	int filecnt;
167 	char d_name[PATH_MAX+1];
168 } DIR;
169 
170 DIR* opendir (const char* dirName);
171 struct dirent *readdir (DIR* dir);
172 int closedir (DIR* dir);
173 #endif /* dirent _WIN32 */
174 
175 /* allocate memory for a formated string and do a sprintf */
176 char *str_sprintf2(const char *fmt, const char *arg1, const char *arg2);
177 char *str_sprintf(const char *fmt, const char *arg);
178 
179 /* tmpl: file name template ending in 'XXXXXX' without path or NULL
180    name_used: if !=NULL pointer to name of temp file, must be freed
181    return: file descriptor or -1 */
182 int get_tmp_file (const char *tmpl, char **name_used);
183 
184 /* allocate and return a name for a temporary file
185    (under UNIX not used because of tempnam race condition) */
186 #if defined(__OS2__)||defined(__EMX__)||defined(__DJGPP__)||defined(_WIN32)||defined(_mikmod_amiga)
187 char *get_tmp_name(void);
188 #endif
189 
190 BOOL file_exist(const char *file);
191 /* determines if a given path is absolute or relative */
192 BOOL path_relative(const char *path);
193 
194 /* allocate and return a filename including the path for a config file
195    'name': filename without the path */
196 char *get_cfg_name(const char *name);
197 
198 /* Return precise time in milliseconds */
199 unsigned long Time1000(void);
200 
201 #if defined(__OS2__)||defined(__EMX__)||defined(__DJGPP__)||defined(_WIN32)||defined(_mikmod_amiga)
202 #define filecmp strcasecmp
203 #else
204 #define filecmp strcmp
205 #endif
206 #if defined(__OS2__)||defined(__EMX__)||(defined(_WIN32)&&!defined(__MINGW32__))
207 #define strcasecmp(s,t) stricmp(s,t)
208 #endif
209 
210 #ifdef HAVE_VSNPRINTF
211 # ifdef _WIN32
212 #  define VSNPRINTF _vsnprintf
213 # else
214 #  define VSNPRINTF vsnprintf
215 # endif
216 #else
217 #define VSNPRINTF(str,size,format,ap) vsprintf(str,format,ap)
218 #endif
219 
220 #ifndef HAVE_SNPRINTF
221 #define SNPRINTF mik_snprintf
222 int mik_snprintf(char *buffer, size_t n, const char *format, ...);
223 #else
224 # ifdef _WIN32
225 #  define SNPRINTF _snprintf
226 # else
227 #  define SNPRINTF snprintf
228 # endif
229 #endif
230 
231 /* Return newly malloced version and cmdline for the driver
232    with the number drvno. */
233 BOOL driver_get_info (int drvno, char **version, char **cmdline);
234 
235 #endif /* MUTILITIES_H */
236 
237 /* ex:set ts=4: */
238