blob: b121791065af059c3aea7ce9ce97dc46e5f7ca53 [file] [log] [blame]
Przemyslaw Skibinski7a8a03c2016-12-21 15:08:44 +01001/**
2 * util.h - utility functions
3 *
4 * 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
inikep9c22e572016-05-05 11:53:42 +020042/*-****************************************
43* Sleep functions: Windows - Posix - others
44******************************************/
inikep31634032016-05-05 00:25:38 +020045#if defined(_WIN32)
inikep83c76b42016-04-28 13:16:01 +020046# include <windows.h>
inikep31634032016-05-05 00:25:38 +020047# define SET_HIGH_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)
inikep83c76b42016-04-28 13:16:01 +020048# define UTIL_sleep(s) Sleep(1000*s)
49# define UTIL_sleepMilli(milli) Sleep(milli)
Przemyslaw Skibinskib0f36632016-12-16 15:41:18 +010050#elif PLATFORM_POSIX_VERSION >= 0 /* Unix-like operating system */
inikep31634032016-05-05 00:25:38 +020051# include <unistd.h>
52# include <sys/resource.h> /* setpriority */
53# include <time.h> /* clock_t, nanosleep, clock, CLOCKS_PER_SEC */
Peter (Stig) Edwards04773ac2016-05-21 12:15:48 +010054# if defined(PRIO_PROCESS)
55# define SET_HIGH_PRIORITY setpriority(PRIO_PROCESS, 0, -20)
56# else
57# define SET_HIGH_PRIORITY /* disabled */
58# endif
inikep31634032016-05-05 00:25:38 +020059# define UTIL_sleep(s) sleep(s)
Przemyslaw Skibinskib0f36632016-12-16 15:41:18 +010060# if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 199309L)) || (PLATFORM_POSIX_VERSION >= 200112L) /* nanosleep requires POSIX.1-2001 */
inikep9c22e572016-05-05 11:53:42 +020061# define UTIL_sleepMilli(milli) { struct timespec t; t.tv_sec=0; t.tv_nsec=milli*1000000ULL; nanosleep(&t, NULL); }
62# else
63# define UTIL_sleepMilli(milli) /* disabled */
64# endif
inikep83c76b42016-04-28 13:16:01 +020065#else
inikep31634032016-05-05 00:25:38 +020066# define SET_HIGH_PRIORITY /* disabled */
67# define UTIL_sleep(s) /* disabled */
inikep83c76b42016-04-28 13:16:01 +020068# define UTIL_sleepMilli(milli) /* disabled */
inikep83c76b42016-04-28 13:16:01 +020069#endif
70
inikep31634032016-05-05 00:25:38 +020071
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +010072/* *************************************
73* Constants
74***************************************/
75#define LIST_SIZE_INCREASE (8*1024)
76
77
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +010078/*-****************************************
79* Compiler specifics
80******************************************/
Przemyslaw Skibinski2f6ccee2016-12-21 13:23:34 +010081#if defined(__INTEL_COMPILER)
82# pragma warning(disable : 177) /* disable: message #177: function was declared but never referenced, useful with UTIL_STATIC */
83#endif
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +010084#if defined(__GNUC__)
85# define UTIL_STATIC static __attribute__((unused))
86#elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
87# define UTIL_STATIC static inline
88#elif defined(_MSC_VER)
89# define UTIL_STATIC static __inline
90#else
91# define UTIL_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
92#endif
93
94
inikep31634032016-05-05 00:25:38 +020095/*-****************************************
96* Time functions
97******************************************/
cyan49735fba09f2017-01-20 12:23:30 -080098#if (PLATFORM_POSIX_VERSION >= 1)
99#include <unistd.h>
100#include <sys/times.h> /* times */
101 typedef U64 UTIL_time_t;
102 UTIL_STATIC void UTIL_initTimer(UTIL_time_t* ticksPerSecond) { *ticksPerSecond=sysconf(_SC_CLK_TCK); }
103 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; }
104 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL * (clockEnd - clockStart) / ticksPerSecond; }
105 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL * (clockEnd - clockStart) / ticksPerSecond; }
106#elif defined(_WIN32) /* Windows */
inikep83c76b42016-04-28 13:16:01 +0200107 typedef LARGE_INTEGER UTIL_time_t;
inikepaaaf9232016-05-09 16:19:25 +0200108 UTIL_STATIC void UTIL_initTimer(UTIL_time_t* ticksPerSecond) { if (!QueryPerformanceFrequency(ticksPerSecond)) fprintf(stderr, "ERROR: QueryPerformance not present\n"); }
109 UTIL_STATIC void UTIL_getTime(UTIL_time_t* x) { QueryPerformanceCounter(x); }
110 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart; }
111 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart; }
cyan49735fba09f2017-01-20 12:23:30 -0800112#else /* relies on standard C (note : clock_t measurements can be wrong when using multi-threading) */
113 typedef clock_t UTIL_time_t;
114 UTIL_STATIC void UTIL_initTimer(UTIL_time_t* ticksPerSecond) { *ticksPerSecond=0; }
115 UTIL_STATIC void UTIL_getTime(UTIL_time_t* x) { *x = clock(); }
116 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t ticksPerSecond, UTIL_time_t clockStart, UTIL_time_t clockEnd) { (void)ticksPerSecond; return 1000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
117 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_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 +0200118#endif
119
120
inikep83c76b42016-04-28 13:16:01 +0200121/* returns time span in microseconds */
122UTIL_STATIC U64 UTIL_clockSpanMicro( UTIL_time_t clockStart, UTIL_time_t ticksPerSecond )
123{
124 UTIL_time_t clockEnd;
inikepaaaf9232016-05-09 16:19:25 +0200125 UTIL_getTime(&clockEnd);
inikep83c76b42016-04-28 13:16:01 +0200126 return UTIL_getSpanTimeMicro(ticksPerSecond, clockStart, clockEnd);
127}
128
129
130UTIL_STATIC void UTIL_waitForNextTick(UTIL_time_t ticksPerSecond)
131{
132 UTIL_time_t clockStart, clockEnd;
inikepaaaf9232016-05-09 16:19:25 +0200133 UTIL_getTime(&clockStart);
Yann Collete162ace2016-05-20 11:24:35 +0200134 do {
135 UTIL_getTime(&clockEnd);
inikepd5ff2c32016-04-28 14:40:45 +0200136 } while (UTIL_getSpanTimeNano(ticksPerSecond, clockStart, clockEnd) == 0);
inikep83c76b42016-04-28 13:16:01 +0200137}
138
139
inikep31634032016-05-05 00:25:38 +0200140
141/*-****************************************
142* File functions
143******************************************/
ds7745f0c202017-02-10 18:37:57 +0100144#if defined(_MSC_VER)
Przemyslaw Skibinskib40884f2016-11-03 09:54:53 +0100145 #define chmod _chmod
-7ec315d2017-02-10 13:27:43 +0100146 typedef struct __stat64 stat_t;
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100147#else
148 typedef struct stat stat_t;
149#endif
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100150
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100151
152UTIL_STATIC int UTIL_setFileStat(const char *filename, stat_t *statbuf)
153{
154 int res = 0;
155 struct utimbuf timebuf;
156
Przemyslaw Skibinskib40884f2016-11-03 09:54:53 +0100157 timebuf.actime = time(NULL);
158 timebuf.modtime = statbuf->st_mtime;
159 res += utime(filename, &timebuf); /* set access and modification times */
160
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100161#if !defined(_WIN32)
162 res += chown(filename, statbuf->st_uid, statbuf->st_gid); /* Copy ownership */
163#endif
164
165 res += chmod(filename, statbuf->st_mode & 07777); /* Copy file permissions */
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100166
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100167 errno = 0;
168 return -res; /* number of errors is returned */
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100169}
170
171
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100172UTIL_STATIC int UTIL_getFileStat(const char* infilename, stat_t *statbuf)
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100173{
174 int r;
ds7745f0c202017-02-10 18:37:57 +0100175#if defined(_MSC_VER)
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100176 r = _stat64(infilename, statbuf);
177 if (r || !(statbuf->st_mode & S_IFREG)) return 0; /* No good... */
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100178#else
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100179 r = stat(infilename, statbuf);
180 if (r || !S_ISREG(statbuf->st_mode)) return 0; /* No good... */
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100181#endif
Przemyslaw Skibinskifcf22e32016-11-02 14:08:07 +0100182 return 1;
Przemyslaw Skibinskid872b642016-11-02 12:52:20 +0100183}
184
185
inikep69fcd7c2016-04-28 12:23:33 +0200186UTIL_STATIC U64 UTIL_getFileSize(const char* infilename)
187{
188 int r;
ds7745f0c202017-02-10 18:37:57 +0100189#if defined(_MSC_VER)
190 struct _stat64 statbuf;
inikep69fcd7c2016-04-28 12:23:33 +0200191 r = _stat64(infilename, &statbuf);
192 if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */
ds7745f0c202017-02-10 18:37:57 +0100193#elif defined(__MINGW32__) && defined (__MSVCRT__)
194 struct _stati64 statbuf;
195 r = _stati64(infilename, &statbuf);
196 if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */
inikep69fcd7c2016-04-28 12:23:33 +0200197#else
198 struct stat statbuf;
199 r = stat(infilename, &statbuf);
200 if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */
201#endif
202 return (U64)statbuf.st_size;
203}
204
205
inikep55d047a2016-04-28 16:50:13 +0200206UTIL_STATIC U64 UTIL_getTotalFileSize(const char** fileNamesTable, unsigned nbFiles)
207{
208 U64 total = 0;
209 unsigned n;
210 for (n=0; n<nbFiles; n++)
211 total += UTIL_getFileSize(fileNamesTable[n]);
212 return total;
213}
214
215
inikep31634032016-05-05 00:25:38 +0200216UTIL_STATIC int UTIL_doesFileExists(const char* infilename)
217{
218 int r;
ds7745f0c202017-02-10 18:37:57 +0100219#if defined(_MSC_VER)
-7ec315d2017-02-10 13:27:43 +0100220 struct __stat64 statbuf;
inikep31634032016-05-05 00:25:38 +0200221 r = _stat64(infilename, &statbuf);
222 if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */
223#else
224 struct stat statbuf;
225 r = stat(infilename, &statbuf);
226 if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */
227#endif
228 return 1;
229}
230
231
inikep69fcd7c2016-04-28 12:23:33 +0200232UTIL_STATIC U32 UTIL_isDirectory(const char* infilename)
233{
234 int r;
ds7745f0c202017-02-10 18:37:57 +0100235#if defined(_MSC_VER)
-7ec315d2017-02-10 13:27:43 +0100236 struct __stat64 statbuf;
inikep69fcd7c2016-04-28 12:23:33 +0200237 r = _stat64(infilename, &statbuf);
238 if (!r && (statbuf.st_mode & _S_IFDIR)) return 1;
239#else
240 struct stat statbuf;
241 r = stat(infilename, &statbuf);
242 if (!r && S_ISDIR(statbuf.st_mode)) return 1;
243#endif
244 return 0;
245}
246
inikep61739312016-09-15 18:58:18 +0200247/*
248 * A modified version of realloc().
249 * If UTIL_realloc() fails the original block is freed.
250*/
251UTIL_STATIC void *UTIL_realloc(void *ptr, size_t size)
252{
253 void *newptr = realloc(ptr, size);
254 if (newptr) return newptr;
255 free(ptr);
256 return NULL;
257}
258
inikep69fcd7c2016-04-28 12:23:33 +0200259
inikep31634032016-05-05 00:25:38 +0200260#ifdef _WIN32
inikep9c22e572016-05-05 11:53:42 +0200261# define UTIL_HAS_CREATEFILELIST
inikep31634032016-05-05 00:25:38 +0200262
inikep4dbf7f42016-05-11 14:11:00 +0200263UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd)
inikep31634032016-05-05 00:25:38 +0200264{
inikep1c5ba8a2016-09-13 13:13:10 +0200265 char* path;
266 int dirLength, fnameLength, pathLength, nbFiles = 0;
inikep31634032016-05-05 00:25:38 +0200267 WIN32_FIND_DATA cFile;
268 HANDLE hFile;
269
inikep9f25fcf2016-09-13 16:38:54 +0200270 dirLength = (int)strlen(dirName);
inikep1c5ba8a2016-09-13 13:13:10 +0200271 path = (char*) malloc(dirLength + 3);
272 if (!path) return 0;
273
274 memcpy(path, dirName, dirLength);
275 path[dirLength] = '\\';
276 path[dirLength+1] = '*';
277 path[dirLength+2] = 0;
inikep31634032016-05-05 00:25:38 +0200278
279 hFile=FindFirstFile(path, &cFile);
280 if (hFile == INVALID_HANDLE_VALUE) {
281 fprintf(stderr, "Cannot open directory '%s'\n", dirName);
282 return 0;
283 }
inikep1c5ba8a2016-09-13 13:13:10 +0200284 free(path);
inikep31634032016-05-05 00:25:38 +0200285
inikep4dbf7f42016-05-11 14:11:00 +0200286 do {
inikep9f25fcf2016-09-13 16:38:54 +0200287 fnameLength = (int)strlen(cFile.cFileName);
inikep1c5ba8a2016-09-13 13:13:10 +0200288 path = (char*) malloc(dirLength + fnameLength + 2);
289 if (!path) { FindClose(hFile); return 0; }
290 memcpy(path, dirName, dirLength);
291 path[dirLength] = '\\';
292 memcpy(path+dirLength+1, cFile.cFileName, fnameLength);
293 pathLength = dirLength+1+fnameLength;
294 path[pathLength] = 0;
inikep31634032016-05-05 00:25:38 +0200295 if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
296 if (strcmp (cFile.cFileName, "..") == 0 ||
inikep4dbf7f42016-05-11 14:11:00 +0200297 strcmp (cFile.cFileName, ".") == 0) continue;
inikepe416e302016-08-24 17:32:09 +0200298
inikep4dbf7f42016-05-11 14:11:00 +0200299 nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd); /* Recursively call "UTIL_prepareFileList" with the new path. */
inikep1c5ba8a2016-09-13 13:13:10 +0200300 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
inikep31634032016-05-05 00:25:38 +0200301 }
302 else if ((cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED)) {
inikep4dbf7f42016-05-11 14:11:00 +0200303 if (*bufStart + *pos + pathLength >= *bufEnd) {
304 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
inikep61739312016-09-15 18:58:18 +0200305 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
inikep4dbf7f42016-05-11 14:11:00 +0200306 *bufEnd = *bufStart + newListSize;
inikep1c5ba8a2016-09-13 13:13:10 +0200307 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
inikep4dbf7f42016-05-11 14:11:00 +0200308 }
309 if (*bufStart + *pos + pathLength < *bufEnd) {
310 strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos));
311 *pos += pathLength + 1;
312 nbFiles++;
313 }
inikep31634032016-05-05 00:25:38 +0200314 }
inikep1c5ba8a2016-09-13 13:13:10 +0200315 free(path);
inikep4dbf7f42016-05-11 14:11:00 +0200316 } while (FindNextFile(hFile, &cFile));
inikep31634032016-05-05 00:25:38 +0200317
318 FindClose(hFile);
319 return nbFiles;
320}
321
Przemyslaw Skibinski0b372052016-12-16 17:12:23 +0100322#elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */
inikep9c22e572016-05-05 11:53:42 +0200323# define UTIL_HAS_CREATEFILELIST
inikep31634032016-05-05 00:25:38 +0200324# include <dirent.h> /* opendir, readdir */
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +0100325# include <string.h> /* strerror, memcpy */
inikep31634032016-05-05 00:25:38 +0200326
inikep4dbf7f42016-05-11 14:11:00 +0200327UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd)
inikep31634032016-05-05 00:25:38 +0200328{
329 DIR *dir;
330 struct dirent *entry;
inikep1c5ba8a2016-09-13 13:13:10 +0200331 char* path;
332 int dirLength, fnameLength, pathLength, nbFiles = 0;
inikep31634032016-05-05 00:25:38 +0200333
inikep31634032016-05-05 00:25:38 +0200334 if (!(dir = opendir(dirName))) {
335 fprintf(stderr, "Cannot open directory '%s': %s\n", dirName, strerror(errno));
336 return 0;
337 }
Yann Collete162ace2016-05-20 11:24:35 +0200338
inikep9f25fcf2016-09-13 16:38:54 +0200339 dirLength = (int)strlen(dirName);
inikep7bc5c6b2016-07-26 11:07:37 +0200340 errno = 0;
inikep3eabe9b2016-05-12 17:15:41 +0200341 while ((entry = readdir(dir)) != NULL) {
inikep0bd0fae2016-05-05 13:10:57 +0200342 if (strcmp (entry->d_name, "..") == 0 ||
343 strcmp (entry->d_name, ".") == 0) continue;
inikep9f25fcf2016-09-13 16:38:54 +0200344 fnameLength = (int)strlen(entry->d_name);
inikep1c5ba8a2016-09-13 13:13:10 +0200345 path = (char*) malloc(dirLength + fnameLength + 2);
346 if (!path) { closedir(dir); return 0; }
347 memcpy(path, dirName, dirLength);
348 path[dirLength] = '/';
349 memcpy(path+dirLength+1, entry->d_name, fnameLength);
350 pathLength = dirLength+1+fnameLength;
351 path[pathLength] = 0;
352
inikep0bd0fae2016-05-05 13:10:57 +0200353 if (UTIL_isDirectory(path)) {
inikep4dbf7f42016-05-11 14:11:00 +0200354 nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd); /* Recursively call "UTIL_prepareFileList" with the new path. */
inikep1c5ba8a2016-09-13 13:13:10 +0200355 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
inikep31634032016-05-05 00:25:38 +0200356 } else {
inikep4dbf7f42016-05-11 14:11:00 +0200357 if (*bufStart + *pos + pathLength >= *bufEnd) {
358 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
inikep61739312016-09-15 18:58:18 +0200359 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
inikep4dbf7f42016-05-11 14:11:00 +0200360 *bufEnd = *bufStart + newListSize;
inikep1c5ba8a2016-09-13 13:13:10 +0200361 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
inikep4dbf7f42016-05-11 14:11:00 +0200362 }
363 if (*bufStart + *pos + pathLength < *bufEnd) {
364 strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos));
365 *pos += pathLength + 1;
366 nbFiles++;
367 }
inikep31634032016-05-05 00:25:38 +0200368 }
inikep1c5ba8a2016-09-13 13:13:10 +0200369 free(path);
inikepe416e302016-08-24 17:32:09 +0200370 errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */
inikep31634032016-05-05 00:25:38 +0200371 }
372
inikep7bc5c6b2016-07-26 11:07:37 +0200373 if (errno != 0) {
374 fprintf(stderr, "readdir(%s) error: %s\n", dirName, strerror(errno));
375 free(*bufStart);
376 *bufStart = NULL;
377 }
inikep31634032016-05-05 00:25:38 +0200378 closedir(dir);
379 return nbFiles;
380}
381
382#else
383
inikep4dbf7f42016-05-11 14:11:00 +0200384UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd)
inikep31634032016-05-05 00:25:38 +0200385{
inikep4dbf7f42016-05-11 14:11:00 +0200386 (void)bufStart; (void)bufEnd; (void)pos;
Przemyslaw Skibinskiead350b2016-12-21 09:04:59 +0100387 fprintf(stderr, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE)\n", dirName);
inikep31634032016-05-05 00:25:38 +0200388 return 0;
389}
390
inikepe416e302016-08-24 17:32:09 +0200391#endif /* #ifdef _WIN32 */
inikep31634032016-05-05 00:25:38 +0200392
Yann Collete162ace2016-05-20 11:24:35 +0200393/*
394 * UTIL_createFileList - takes a list of files and directories (params: inputNames, inputNamesNb), scans directories,
inikep0bdb6a82016-05-13 10:52:02 +0200395 * and returns a new list of files (params: return value, allocatedBuffer, allocatedNamesNb).
396 * After finishing usage of the list the structures should be freed with UTIL_freeFileList(params: return value, allocatedBuffer)
397 * In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called.
398 */
399UTIL_STATIC const char** UTIL_createFileList(const char **inputNames, unsigned inputNamesNb, char** allocatedBuffer, unsigned* allocatedNamesNb)
inikep31634032016-05-05 00:25:38 +0200400{
inikep4dbf7f42016-05-11 14:11:00 +0200401 size_t pos;
inikep0bdb6a82016-05-13 10:52:02 +0200402 unsigned i, nbFiles;
Yann Colletfda539f2016-12-12 01:03:23 +0100403 char* buf = (char*)malloc(LIST_SIZE_INCREASE);
404 char* bufend = buf + LIST_SIZE_INCREASE;
inikep0bdb6a82016-05-13 10:52:02 +0200405 const char** fileTable;
inikep31634032016-05-05 00:25:38 +0200406
inikep0bdb6a82016-05-13 10:52:02 +0200407 if (!buf) return NULL;
inikep9c22e572016-05-05 11:53:42 +0200408
inikep0bdb6a82016-05-13 10:52:02 +0200409 for (i=0, pos=0, nbFiles=0; i<inputNamesNb; i++) {
inikep957823f2016-05-25 15:30:55 +0200410 if (!UTIL_isDirectory(inputNames[i])) {
Yann Colletfda539f2016-12-12 01:03:23 +0100411 size_t const len = strlen(inputNames[i]);
inikep4dbf7f42016-05-11 14:11:00 +0200412 if (buf + pos + len >= bufend) {
413 ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE;
inikep61739312016-09-15 18:58:18 +0200414 buf = (char*)UTIL_realloc(buf, newListSize);
inikep4dbf7f42016-05-11 14:11:00 +0200415 bufend = buf + newListSize;
inikep0bdb6a82016-05-13 10:52:02 +0200416 if (!buf) return NULL;
inikep4dbf7f42016-05-11 14:11:00 +0200417 }
418 if (buf + pos + len < bufend) {
419 strncpy(buf + pos, inputNames[i], bufend - (buf + pos));
420 pos += len + 1;
421 nbFiles++;
422 }
Yann Collet415251c2016-08-01 14:26:49 +0200423 } else {
inikep4dbf7f42016-05-11 14:11:00 +0200424 nbFiles += UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend);
inikep0bdb6a82016-05-13 10:52:02 +0200425 if (buf == NULL) return NULL;
Yann Collet415251c2016-08-01 14:26:49 +0200426 } }
inikep31634032016-05-05 00:25:38 +0200427
inikep0bdb6a82016-05-13 10:52:02 +0200428 if (nbFiles == 0) { free(buf); return NULL; }
inikep31634032016-05-05 00:25:38 +0200429
inikep0bdb6a82016-05-13 10:52:02 +0200430 fileTable = (const char**)malloc((nbFiles+1) * sizeof(const char*));
431 if (!fileTable) { free(buf); return NULL; }
inikep31634032016-05-05 00:25:38 +0200432
Yann Colletfda539f2016-12-12 01:03:23 +0100433 for (i=0, pos=0; i<nbFiles; i++) {
inikep0bdb6a82016-05-13 10:52:02 +0200434 fileTable[i] = buf + pos;
435 pos += strlen(fileTable[i]) + 1;
inikep31634032016-05-05 00:25:38 +0200436 }
437
inikep0bdb6a82016-05-13 10:52:02 +0200438 if (buf + pos > bufend) { free(buf); free((void*)fileTable); return NULL; }
439
440 *allocatedBuffer = buf;
441 *allocatedNamesNb = nbFiles;
442
443 return fileTable;
inikep31634032016-05-05 00:25:38 +0200444}
445
446
inikep0bdb6a82016-05-13 10:52:02 +0200447UTIL_STATIC void UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer)
inikep31634032016-05-05 00:25:38 +0200448{
inikep0bdb6a82016-05-13 10:52:02 +0200449 if (allocatedBuffer) free(allocatedBuffer);
450 if (filenameTable) free((void*)filenameTable);
inikep31634032016-05-05 00:25:38 +0200451}
452
453
inikep69fcd7c2016-04-28 12:23:33 +0200454#if defined (__cplusplus)
455}
456#endif
457
458#endif /* UTIL_H_MODULE */