blob: 9d23cbb8e935477d7fde462c45f51864dcfe9dab [file] [log] [blame]
Przemyslaw Skibinski7a8a03c2016-12-21 15:08:44 +01001/**
2 * util.h - utility functions
Sean Purcell680e4e02017-03-23 11:52:09 -07003 *
Przemyslaw Skibinski7a8a03c2016-12-21 15:08:44 +01004 * Copyright (c) 2016-present, Przemyslaw Skibinski, Yann Collet, Facebook, Inc.
5 * All rights reserved.
6 *
7 * This source code is licensed under the BSD-style license found in the
8 * LICENSE file in the root directory of this source tree. An additional grant
9 * of patent rights can be found in the PATENTS file in the same directory.
10 */
inikep69fcd7c2016-04-28 12:23:33 +020011
inikep69fcd7c2016-04-28 12:23:33 +020012#ifndef UTIL_H_MODULE
13#define UTIL_H_MODULE
14
15#if defined (__cplusplus)
16extern "C" {
17#endif
18
inikep9c22e572016-05-05 11:53:42 +020019
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010020
inikep69fcd7c2016-04-28 12:23:33 +020021/*-****************************************
22* Dependencies
23******************************************/
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010024#include "platform.h" /* PLATFORM_POSIX_VERSION */
25#include <stdlib.h> /* malloc */
26#include <stddef.h> /* size_t, ptrdiff_t */
27#include <stdio.h> /* fprintf */
28#include <sys/types.h> /* stat, utime */
29#include <sys/stat.h> /* stat */
Przemyslaw Skibinskib40884f2016-11-03 09:54:53 +010030#if defined(_MSC_VER)
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010031# include <sys/utime.h> /* utime */
32# include <io.h> /* _chmod */
Przemyslaw Skibinskib40884f2016-11-03 09:54:53 +010033#else
Yann Colletfda539f2016-12-12 01:03:23 +010034# include <unistd.h> /* chown, stat */
35# include <utime.h> /* utime */
Przemyslaw Skibinskib40884f2016-11-03 09:54:53 +010036#endif
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010037#include <time.h> /* time */
Przemyslaw Skibinskib40884f2016-11-03 09:54:53 +010038#include <errno.h>
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010039#include "mem.h" /* U32, U64 */
inikep69fcd7c2016-04-28 12:23:33 +020040
inikep31634032016-05-05 00:25:38 +020041
Przemyslaw Skibinski6e59b3c2017-02-15 17:03:16 +010042/* ************************************************************
43* Avoid fseek()'s 2GiB barrier with MSVC, MacOS, *BSD, MinGW
44***************************************************************/
45#if defined(_MSC_VER) && (_MSC_VER >= 1400)
46# define UTIL_fseek _fseeki64
47#elif !defined(__64BIT__) && (PLATFORM_POSIX_VERSION >= 200112L) /* No point defining Large file for 64 bit */
48# define UTIL_fseek fseeko
49#elif defined(__MINGW32__) && defined(__MSVCRT__) && !defined(__STRICT_ANSI__) && !defined(__NO_MINGW_LFS)
50# define UTIL_fseek fseeko64
51#else
52# define UTIL_fseek fseek
53#endif
54
55
inikep9c22e572016-05-05 11:53:42 +020056/*-****************************************
57* Sleep functions: Windows - Posix - others
58******************************************/
inikep31634032016-05-05 00:25:38 +020059#if defined(_WIN32)
inikep83c76b42016-04-28 13:16:01 +020060# include <windows.h>
Przemyslaw Skibinski94abd6a2017-02-07 16:36:19 +010061# define SET_REALTIME_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)
inikep83c76b42016-04-28 13:16:01 +020062# define UTIL_sleep(s) Sleep(1000*s)
63# define UTIL_sleepMilli(milli) Sleep(milli)
Przemyslaw Skibinskib0f36632016-12-16 15:41:18 +010064#elif PLATFORM_POSIX_VERSION >= 0 /* Unix-like operating system */
inikep31634032016-05-05 00:25:38 +020065# include <unistd.h>
66# include <sys/resource.h> /* setpriority */
67# include <time.h> /* clock_t, nanosleep, clock, CLOCKS_PER_SEC */
Peter (Stig) Edwards04773ac2016-05-21 12:15:48 +010068# if defined(PRIO_PROCESS)
Przemyslaw Skibinski94abd6a2017-02-07 16:36:19 +010069# define SET_REALTIME_PRIORITY setpriority(PRIO_PROCESS, 0, -20)
Peter (Stig) Edwards04773ac2016-05-21 12:15:48 +010070# else
Przemyslaw Skibinski94abd6a2017-02-07 16:36:19 +010071# define SET_REALTIME_PRIORITY /* disabled */
Peter (Stig) Edwards04773ac2016-05-21 12:15:48 +010072# endif
inikep31634032016-05-05 00:25:38 +020073# define UTIL_sleep(s) sleep(s)
Przemyslaw Skibinskib0f36632016-12-16 15:41:18 +010074# if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 199309L)) || (PLATFORM_POSIX_VERSION >= 200112L) /* nanosleep requires POSIX.1-2001 */
inikep9c22e572016-05-05 11:53:42 +020075# define UTIL_sleepMilli(milli) { struct timespec t; t.tv_sec=0; t.tv_nsec=milli*1000000ULL; nanosleep(&t, NULL); }
76# else
77# define UTIL_sleepMilli(milli) /* disabled */
78# endif
inikep83c76b42016-04-28 13:16:01 +020079#else
Przemyslaw Skibinski94abd6a2017-02-07 16:36:19 +010080# define SET_REALTIME_PRIORITY /* disabled */
inikep31634032016-05-05 00:25:38 +020081# define UTIL_sleep(s) /* disabled */
inikep83c76b42016-04-28 13:16:01 +020082# define UTIL_sleepMilli(milli) /* disabled */
inikep83c76b42016-04-28 13:16:01 +020083#endif
84
inikep31634032016-05-05 00:25:38 +020085
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +010086/* *************************************
87* Constants
88***************************************/
89#define LIST_SIZE_INCREASE (8*1024)
90
91
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +010092/*-****************************************
93* Compiler specifics
94******************************************/
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010095#if defined(__INTEL_COMPILER)
96# pragma warning(disable : 177) /* disable: message #177: function was declared but never referenced, useful with UTIL_STATIC */
97#endif
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +010098#if defined(__GNUC__)
99# define UTIL_STATIC static __attribute__((unused))
100#elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
101# define UTIL_STATIC static inline
102#elif defined(_MSC_VER)
103# define UTIL_STATIC static __inline
104#else
105# define UTIL_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
106#endif
107
108
inikep31634032016-05-05 00:25:38 +0200109/*-****************************************
110* Time functions
111******************************************/
Przemyslaw Skibinski83775d92017-02-20 11:11:50 +0100112#if defined(_WIN32) /* Windows */
Przemyslaw Skibinskie052c602017-02-20 11:27:11 +0100113 typedef LARGE_INTEGER UTIL_freq_t;
inikep83c76b42016-04-28 13:16:01 +0200114 typedef LARGE_INTEGER UTIL_time_t;
Przemyslaw Skibinskie052c602017-02-20 11:27:11 +0100115 UTIL_STATIC void UTIL_initTimer(UTIL_freq_t* ticksPerSecond) { if (!QueryPerformanceFrequency(ticksPerSecond)) fprintf(stderr, "ERROR: QueryPerformance not present\n"); }
inikepaaaf9232016-05-09 16:19:25 +0200116 UTIL_STATIC void UTIL_getTime(UTIL_time_t* x) { QueryPerformanceCounter(x); }
Przemyslaw Skibinskie052c602017-02-20 11:27:11 +0100117 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_freq_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart; }
118 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_freq_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart; }
Przemyslaw Skibinskida4a0f32017-02-20 12:18:15 +0100119#elif defined(__APPLE__) && defined(__MACH__)
120 #include <mach/mach_time.h>
121 typedef mach_timebase_info_data_t UTIL_freq_t;
122 typedef U64 UTIL_time_t;
Przemyslaw Skibinski74dcd8d2017-02-21 12:22:05 +0100123 UTIL_STATIC void UTIL_initTimer(UTIL_freq_t* rate) { mach_timebase_info(rate); }
Przemyslaw Skibinskida4a0f32017-02-20 12:18:15 +0100124 UTIL_STATIC void UTIL_getTime(UTIL_time_t* x) { *x = mach_absolute_time(); }
125 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_freq_t rate, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return (((clockEnd - clockStart) * (U64)rate.numer) / ((U64)rate.denom))/1000ULL; }
126 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_freq_t rate, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return ((clockEnd - clockStart) * (U64)rate.numer) / ((U64)rate.denom); }
Przemyslaw Skibinski1b593332017-02-21 07:33:45 +0100127#elif (PLATFORM_POSIX_VERSION >= 200112L)
128 #include <sys/times.h> /* times */
129 typedef U64 UTIL_freq_t;
130 typedef U64 UTIL_time_t;
131 UTIL_STATIC void UTIL_initTimer(UTIL_freq_t* ticksPerSecond) { *ticksPerSecond=sysconf(_SC_CLK_TCK); }
132 UTIL_STATIC void UTIL_getTime(UTIL_time_t* x) { struct tms junk; clock_t newTicks = (clock_t) times(&junk); (void)junk; *x = (UTIL_time_t)newTicks; }
133 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_freq_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL * (clockEnd - clockStart) / ticksPerSecond; }
134 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_freq_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL * (clockEnd - clockStart) / ticksPerSecond; }
cyan49735fba09f2017-01-20 12:23:30 -0800135#else /* relies on standard C (note : clock_t measurements can be wrong when using multi-threading) */
Przemyslaw Skibinskie052c602017-02-20 11:27:11 +0100136 typedef clock_t UTIL_freq_t;
cyan49735fba09f2017-01-20 12:23:30 -0800137 typedef clock_t UTIL_time_t;
Przemyslaw Skibinskie052c602017-02-20 11:27:11 +0100138 UTIL_STATIC void UTIL_initTimer(UTIL_freq_t* ticksPerSecond) { *ticksPerSecond=0; }
cyan49735fba09f2017-01-20 12:23:30 -0800139 UTIL_STATIC void UTIL_getTime(UTIL_time_t* x) { *x = clock(); }
Przemyslaw Skibinskie052c602017-02-20 11:27:11 +0100140 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_freq_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { (void)ticksPerSecond; return 1000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
141 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_freq_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { (void)ticksPerSecond; return 1000000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
inikep83c76b42016-04-28 13:16:01 +0200142#endif
143
144
inikep83c76b42016-04-28 13:16:01 +0200145/* returns time span in microseconds */
Przemyslaw Skibinskie052c602017-02-20 11:27:11 +0100146UTIL_STATIC U64 UTIL_clockSpanMicro( UTIL_time_t clockStart, UTIL_freq_t ticksPerSecond )
inikep83c76b42016-04-28 13:16:01 +0200147{
148 UTIL_time_t clockEnd;
inikepaaaf9232016-05-09 16:19:25 +0200149 UTIL_getTime(&clockEnd);
inikep83c76b42016-04-28 13:16:01 +0200150 return UTIL_getSpanTimeMicro(ticksPerSecond, clockStart, clockEnd);
151}
152
153
Przemyslaw Skibinskie052c602017-02-20 11:27:11 +0100154UTIL_STATIC void UTIL_waitForNextTick(UTIL_freq_t ticksPerSecond)
inikep83c76b42016-04-28 13:16:01 +0200155{
156 UTIL_time_t clockStart, clockEnd;
inikepaaaf9232016-05-09 16:19:25 +0200157 UTIL_getTime(&clockStart);
Yann Collete162ace2016-05-20 11:24:35 +0200158 do {
159 UTIL_getTime(&clockEnd);
inikepd5ff2c32016-04-28 14:40:45 +0200160 } while (UTIL_getSpanTimeNano(ticksPerSecond, clockStart, clockEnd) == 0);
inikep83c76b42016-04-28 13:16:01 +0200161}
162
163
inikep31634032016-05-05 00:25:38 +0200164
165/*-****************************************
166* File functions
167******************************************/
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100168#if defined(_MSC_VER)
Przemyslaw Skibinskib40884f2016-11-03 09:54:53 +0100169 #define chmod _chmod
-7ec315d2017-02-10 13:27:43 +0100170 typedef struct __stat64 stat_t;
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100171#else
172 typedef struct stat stat_t;
173#endif
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100174
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100175
176UTIL_STATIC int UTIL_setFileStat(const char *filename, stat_t *statbuf)
177{
178 int res = 0;
179 struct utimbuf timebuf;
180
Przemyslaw Skibinskib40884f2016-11-03 09:54:53 +0100181 timebuf.actime = time(NULL);
182 timebuf.modtime = statbuf->st_mtime;
183 res += utime(filename, &timebuf); /* set access and modification times */
184
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100185#if !defined(_WIN32)
186 res += chown(filename, statbuf->st_uid, statbuf->st_gid); /* Copy ownership */
187#endif
188
189 res += chmod(filename, statbuf->st_mode & 07777); /* Copy file permissions */
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100190
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100191 errno = 0;
192 return -res; /* number of errors is returned */
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100193}
194
195
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100196UTIL_STATIC int UTIL_getFileStat(const char* infilename, stat_t *statbuf)
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100197{
198 int r;
199#if defined(_MSC_VER)
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100200 r = _stat64(infilename, statbuf);
201 if (r || !(statbuf->st_mode & S_IFREG)) return 0; /* No good... */
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100202#else
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100203 r = stat(infilename, statbuf);
204 if (r || !S_ISREG(statbuf->st_mode)) return 0; /* No good... */
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100205#endif
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100206 return 1;
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100207}
208
Przemyslaw Skibinski442c75f2017-02-14 09:38:51 +0100209
Sean Purcell0f5c95a2017-02-07 16:33:48 -0800210UTIL_STATIC int UTIL_isRegFile(const char* infilename)
211{
212 stat_t statbuf;
213 return UTIL_getFileStat(infilename, &statbuf); /* Only need to know whether it is a regular file */
214}
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100215
Przemyslaw Skibinski442c75f2017-02-14 09:38:51 +0100216
217UTIL_STATIC U32 UTIL_isDirectory(const char* infilename)
218{
219 int r;
220 stat_t statbuf;
221#if defined(_MSC_VER)
222 r = _stat64(infilename, &statbuf);
223 if (!r && (statbuf.st_mode & _S_IFDIR)) return 1;
224#else
225 r = stat(infilename, &statbuf);
226 if (!r && S_ISDIR(statbuf.st_mode)) return 1;
227#endif
228 return 0;
229}
230
Sean Purcell680e4e02017-03-23 11:52:09 -0700231UTIL_STATIC U32 UTIL_isLink(const char* infilename)
232{
233#if defined(_WIN32)
234 /* no symlinks on windows */
235 (void)infilename;
236#else
237 int r;
238 stat_t statbuf;
239 r = lstat(infilename, &statbuf);
240 if (!r && S_ISLNK(statbuf.st_mode)) return 1;
241#endif
242 return 0;
243}
244
Przemyslaw Skibinski442c75f2017-02-14 09:38:51 +0100245
inikep69fcd7c2016-04-28 12:23:33 +0200246UTIL_STATIC U64 UTIL_getFileSize(const char* infilename)
247{
248 int r;
249#if defined(_MSC_VER)
Przemyslaw Skibinski35bf23c2017-02-13 13:57:29 +0100250 struct __stat64 statbuf;
inikep69fcd7c2016-04-28 12:23:33 +0200251 r = _stat64(infilename, &statbuf);
252 if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */
ds7745f0c202017-02-10 18:37:57 +0100253#elif defined(__MINGW32__) && defined (__MSVCRT__)
254 struct _stati64 statbuf;
255 r = _stati64(infilename, &statbuf);
256 if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */
inikep69fcd7c2016-04-28 12:23:33 +0200257#else
258 struct stat statbuf;
259 r = stat(infilename, &statbuf);
260 if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */
261#endif
262 return (U64)statbuf.st_size;
263}
264
265
inikep55d047a2016-04-28 16:50:13 +0200266UTIL_STATIC U64 UTIL_getTotalFileSize(const char** fileNamesTable, unsigned nbFiles)
267{
268 U64 total = 0;
269 unsigned n;
270 for (n=0; n<nbFiles; n++)
271 total += UTIL_getFileSize(fileNamesTable[n]);
272 return total;
273}
274
275
inikep61739312016-09-15 18:58:18 +0200276/*
277 * A modified version of realloc().
278 * If UTIL_realloc() fails the original block is freed.
279*/
280UTIL_STATIC void *UTIL_realloc(void *ptr, size_t size)
281{
282 void *newptr = realloc(ptr, size);
283 if (newptr) return newptr;
284 free(ptr);
285 return NULL;
286}
287
Sean Purcelldee08ca2017-03-23 12:09:35 -0700288static int g_utilDisplayLevel;
289#define UTIL_DISPLAY(...) fprintf(stderr, __VA_ARGS__)
290#define UTIL_DISPLAYLEVEL(l, ...) { if (g_utilDisplayLevel>=l) { UTIL_DISPLAY(__VA_ARGS__); } }
291
inikep31634032016-05-05 00:25:38 +0200292#ifdef _WIN32
inikep9c22e572016-05-05 11:53:42 +0200293# define UTIL_HAS_CREATEFILELIST
inikep31634032016-05-05 00:25:38 +0200294
Sean Purcell680e4e02017-03-23 11:52:09 -0700295UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
inikep31634032016-05-05 00:25:38 +0200296{
inikep1c5ba8a2016-09-13 13:13:10 +0200297 char* path;
298 int dirLength, fnameLength, pathLength, nbFiles = 0;
Przemyslaw Skibinskiacb6e572017-02-15 17:13:35 +0100299 WIN32_FIND_DATAA cFile;
inikep31634032016-05-05 00:25:38 +0200300 HANDLE hFile;
301
inikep9f25fcf2016-09-13 16:38:54 +0200302 dirLength = (int)strlen(dirName);
inikep1c5ba8a2016-09-13 13:13:10 +0200303 path = (char*) malloc(dirLength + 3);
304 if (!path) return 0;
305
306 memcpy(path, dirName, dirLength);
307 path[dirLength] = '\\';
308 path[dirLength+1] = '*';
309 path[dirLength+2] = 0;
inikep31634032016-05-05 00:25:38 +0200310
Przemyslaw Skibinskiacb6e572017-02-15 17:13:35 +0100311 hFile=FindFirstFileA(path, &cFile);
inikep31634032016-05-05 00:25:38 +0200312 if (hFile == INVALID_HANDLE_VALUE) {
313 fprintf(stderr, "Cannot open directory '%s'\n", dirName);
314 return 0;
315 }
inikep1c5ba8a2016-09-13 13:13:10 +0200316 free(path);
inikep31634032016-05-05 00:25:38 +0200317
inikep4dbf7f42016-05-11 14:11:00 +0200318 do {
inikep9f25fcf2016-09-13 16:38:54 +0200319 fnameLength = (int)strlen(cFile.cFileName);
inikep1c5ba8a2016-09-13 13:13:10 +0200320 path = (char*) malloc(dirLength + fnameLength + 2);
321 if (!path) { FindClose(hFile); return 0; }
322 memcpy(path, dirName, dirLength);
323 path[dirLength] = '\\';
324 memcpy(path+dirLength+1, cFile.cFileName, fnameLength);
325 pathLength = dirLength+1+fnameLength;
326 path[pathLength] = 0;
inikep31634032016-05-05 00:25:38 +0200327 if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
328 if (strcmp (cFile.cFileName, "..") == 0 ||
inikep4dbf7f42016-05-11 14:11:00 +0200329 strcmp (cFile.cFileName, ".") == 0) continue;
inikepe416e302016-08-24 17:32:09 +0200330
Sean Purcell680e4e02017-03-23 11:52:09 -0700331 nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks); /* Recursively call "UTIL_prepareFileList" with the new path. */
inikep1c5ba8a2016-09-13 13:13:10 +0200332 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
inikep31634032016-05-05 00:25:38 +0200333 }
334 else if ((cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED)) {
inikep4dbf7f42016-05-11 14:11:00 +0200335 if (*bufStart + *pos + pathLength >= *bufEnd) {
336 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
inikep61739312016-09-15 18:58:18 +0200337 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
inikep4dbf7f42016-05-11 14:11:00 +0200338 *bufEnd = *bufStart + newListSize;
inikep1c5ba8a2016-09-13 13:13:10 +0200339 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
inikep4dbf7f42016-05-11 14:11:00 +0200340 }
341 if (*bufStart + *pos + pathLength < *bufEnd) {
342 strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos));
343 *pos += pathLength + 1;
344 nbFiles++;
345 }
inikep31634032016-05-05 00:25:38 +0200346 }
inikep1c5ba8a2016-09-13 13:13:10 +0200347 free(path);
Przemyslaw Skibinskiacb6e572017-02-15 17:13:35 +0100348 } while (FindNextFileA(hFile, &cFile));
inikep31634032016-05-05 00:25:38 +0200349
350 FindClose(hFile);
351 return nbFiles;
352}
353
Przemyslaw Skibinski0b372052016-12-16 17:12:23 +0100354#elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */
inikep9c22e572016-05-05 11:53:42 +0200355# define UTIL_HAS_CREATEFILELIST
inikep31634032016-05-05 00:25:38 +0200356# include <dirent.h> /* opendir, readdir */
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +0100357# include <string.h> /* strerror, memcpy */
inikep31634032016-05-05 00:25:38 +0200358
Sean Purcell680e4e02017-03-23 11:52:09 -0700359UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
inikep31634032016-05-05 00:25:38 +0200360{
361 DIR *dir;
362 struct dirent *entry;
inikep1c5ba8a2016-09-13 13:13:10 +0200363 char* path;
364 int dirLength, fnameLength, pathLength, nbFiles = 0;
inikep31634032016-05-05 00:25:38 +0200365
inikep31634032016-05-05 00:25:38 +0200366 if (!(dir = opendir(dirName))) {
367 fprintf(stderr, "Cannot open directory '%s': %s\n", dirName, strerror(errno));
368 return 0;
369 }
Yann Collete162ace2016-05-20 11:24:35 +0200370
inikep9f25fcf2016-09-13 16:38:54 +0200371 dirLength = (int)strlen(dirName);
inikep7bc5c6b2016-07-26 11:07:37 +0200372 errno = 0;
inikep3eabe9b2016-05-12 17:15:41 +0200373 while ((entry = readdir(dir)) != NULL) {
inikep0bd0fae2016-05-05 13:10:57 +0200374 if (strcmp (entry->d_name, "..") == 0 ||
375 strcmp (entry->d_name, ".") == 0) continue;
inikep9f25fcf2016-09-13 16:38:54 +0200376 fnameLength = (int)strlen(entry->d_name);
inikep1c5ba8a2016-09-13 13:13:10 +0200377 path = (char*) malloc(dirLength + fnameLength + 2);
378 if (!path) { closedir(dir); return 0; }
379 memcpy(path, dirName, dirLength);
Sean Purcell680e4e02017-03-23 11:52:09 -0700380
inikep1c5ba8a2016-09-13 13:13:10 +0200381 path[dirLength] = '/';
382 memcpy(path+dirLength+1, entry->d_name, fnameLength);
383 pathLength = dirLength+1+fnameLength;
384 path[pathLength] = 0;
385
Sean Purcell680e4e02017-03-23 11:52:09 -0700386 if (!followLinks && UTIL_isLink(path)) {
387 UTIL_DISPLAYLEVEL(2, "Warning : %s is a symbolic link, ignoring\n", path);
388 continue;
389 }
390
inikep0bd0fae2016-05-05 13:10:57 +0200391 if (UTIL_isDirectory(path)) {
Sean Purcell680e4e02017-03-23 11:52:09 -0700392 nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd, followLinks); /* Recursively call "UTIL_prepareFileList" with the new path. */
inikep1c5ba8a2016-09-13 13:13:10 +0200393 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
inikep31634032016-05-05 00:25:38 +0200394 } else {
inikep4dbf7f42016-05-11 14:11:00 +0200395 if (*bufStart + *pos + pathLength >= *bufEnd) {
396 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
inikep61739312016-09-15 18:58:18 +0200397 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
inikep4dbf7f42016-05-11 14:11:00 +0200398 *bufEnd = *bufStart + newListSize;
inikep1c5ba8a2016-09-13 13:13:10 +0200399 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
inikep4dbf7f42016-05-11 14:11:00 +0200400 }
401 if (*bufStart + *pos + pathLength < *bufEnd) {
402 strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos));
403 *pos += pathLength + 1;
404 nbFiles++;
405 }
inikep31634032016-05-05 00:25:38 +0200406 }
inikep1c5ba8a2016-09-13 13:13:10 +0200407 free(path);
inikepe416e302016-08-24 17:32:09 +0200408 errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */
inikep31634032016-05-05 00:25:38 +0200409 }
410
inikep7bc5c6b2016-07-26 11:07:37 +0200411 if (errno != 0) {
412 fprintf(stderr, "readdir(%s) error: %s\n", dirName, strerror(errno));
413 free(*bufStart);
414 *bufStart = NULL;
415 }
inikep31634032016-05-05 00:25:38 +0200416 closedir(dir);
417 return nbFiles;
418}
419
420#else
421
Sean Purcell680e4e02017-03-23 11:52:09 -0700422UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd, int followLinks)
inikep31634032016-05-05 00:25:38 +0200423{
inikep4dbf7f42016-05-11 14:11:00 +0200424 (void)bufStart; (void)bufEnd; (void)pos;
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +0100425 fprintf(stderr, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE)\n", dirName);
inikep31634032016-05-05 00:25:38 +0200426 return 0;
427}
428
inikepe416e302016-08-24 17:32:09 +0200429#endif /* #ifdef _WIN32 */
inikep31634032016-05-05 00:25:38 +0200430
Yann Collete162ace2016-05-20 11:24:35 +0200431/*
432 * UTIL_createFileList - takes a list of files and directories (params: inputNames, inputNamesNb), scans directories,
inikep0bdb6a82016-05-13 10:52:02 +0200433 * and returns a new list of files (params: return value, allocatedBuffer, allocatedNamesNb).
434 * After finishing usage of the list the structures should be freed with UTIL_freeFileList(params: return value, allocatedBuffer)
435 * In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called.
436 */
Sean Purcell680e4e02017-03-23 11:52:09 -0700437UTIL_STATIC const char** UTIL_createFileList(const char **inputNames, unsigned inputNamesNb, char** allocatedBuffer, unsigned* allocatedNamesNb, int followLinks)
inikep31634032016-05-05 00:25:38 +0200438{
inikep4dbf7f42016-05-11 14:11:00 +0200439 size_t pos;
inikep0bdb6a82016-05-13 10:52:02 +0200440 unsigned i, nbFiles;
Yann Colletfda539f2016-12-12 01:03:23 +0100441 char* buf = (char*)malloc(LIST_SIZE_INCREASE);
442 char* bufend = buf + LIST_SIZE_INCREASE;
inikep0bdb6a82016-05-13 10:52:02 +0200443 const char** fileTable;
inikep31634032016-05-05 00:25:38 +0200444
inikep0bdb6a82016-05-13 10:52:02 +0200445 if (!buf) return NULL;
inikep9c22e572016-05-05 11:53:42 +0200446
inikep0bdb6a82016-05-13 10:52:02 +0200447 for (i=0, pos=0, nbFiles=0; i<inputNamesNb; i++) {
inikep957823f2016-05-25 15:30:55 +0200448 if (!UTIL_isDirectory(inputNames[i])) {
Yann Colletfda539f2016-12-12 01:03:23 +0100449 size_t const len = strlen(inputNames[i]);
inikep4dbf7f42016-05-11 14:11:00 +0200450 if (buf + pos + len >= bufend) {
451 ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE;
inikep61739312016-09-15 18:58:18 +0200452 buf = (char*)UTIL_realloc(buf, newListSize);
inikep4dbf7f42016-05-11 14:11:00 +0200453 bufend = buf + newListSize;
inikep0bdb6a82016-05-13 10:52:02 +0200454 if (!buf) return NULL;
inikep4dbf7f42016-05-11 14:11:00 +0200455 }
456 if (buf + pos + len < bufend) {
457 strncpy(buf + pos, inputNames[i], bufend - (buf + pos));
458 pos += len + 1;
459 nbFiles++;
460 }
Yann Collet415251c2016-08-01 14:26:49 +0200461 } else {
Sean Purcell680e4e02017-03-23 11:52:09 -0700462 nbFiles += UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend, followLinks);
inikep0bdb6a82016-05-13 10:52:02 +0200463 if (buf == NULL) return NULL;
Yann Collet415251c2016-08-01 14:26:49 +0200464 } }
inikep31634032016-05-05 00:25:38 +0200465
inikep0bdb6a82016-05-13 10:52:02 +0200466 if (nbFiles == 0) { free(buf); return NULL; }
inikep31634032016-05-05 00:25:38 +0200467
inikep0bdb6a82016-05-13 10:52:02 +0200468 fileTable = (const char**)malloc((nbFiles+1) * sizeof(const char*));
469 if (!fileTable) { free(buf); return NULL; }
inikep31634032016-05-05 00:25:38 +0200470
Yann Colletfda539f2016-12-12 01:03:23 +0100471 for (i=0, pos=0; i<nbFiles; i++) {
inikep0bdb6a82016-05-13 10:52:02 +0200472 fileTable[i] = buf + pos;
473 pos += strlen(fileTable[i]) + 1;
inikep31634032016-05-05 00:25:38 +0200474 }
475
inikep0bdb6a82016-05-13 10:52:02 +0200476 if (buf + pos > bufend) { free(buf); free((void*)fileTable); return NULL; }
477
478 *allocatedBuffer = buf;
479 *allocatedNamesNb = nbFiles;
480
481 return fileTable;
inikep31634032016-05-05 00:25:38 +0200482}
483
484
inikep0bdb6a82016-05-13 10:52:02 +0200485UTIL_STATIC void UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer)
inikep31634032016-05-05 00:25:38 +0200486{
inikep0bdb6a82016-05-13 10:52:02 +0200487 if (allocatedBuffer) free(allocatedBuffer);
488 if (filenameTable) free((void*)filenameTable);
inikep31634032016-05-05 00:25:38 +0200489}
490
491
inikep69fcd7c2016-04-28 12:23:33 +0200492#if defined (__cplusplus)
493}
494#endif
495
496#endif /* UTIL_H_MODULE */