1 /*
2 util.h - utility functions
3 Copyright (C) 2016-present, Przemyslaw Skibinski, Yann Collet
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 along
16 with this program; if not, write to the Free Software Foundation, Inc.,
17 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
18 */
19
20 #ifndef UTIL_H_MODULE
21 #define UTIL_H_MODULE
22
23 #if defined (__cplusplus)
24 extern "C" {
25 #endif
26
27
28
29 /*-****************************************
30 * Dependencies
31 ******************************************/
32 #include "platform.h" /* PLATFORM_POSIX_VERSION */
33 #include <stddef.h> /* size_t, ptrdiff_t */
34 #include <stdlib.h> /* malloc */
35 #include <string.h> /* strlen, strncpy */
36 #include <stdio.h> /* fprintf */
37 #include <sys/types.h> /* stat, utime */
38 #include <sys/stat.h> /* stat */
39 #if defined(_MSC_VER)
40 # include <sys/utime.h> /* utime */
41 # include <io.h> /* _chmod */
42 #else
43 # include <unistd.h> /* chown, stat */
44 # include <utime.h> /* utime */
45 #endif
46 #include <time.h> /* time */
47 #include <errno.h>
48
49
50
51 /*-**************************************************************
52 * Basic Types
53 *****************************************************************/
54 #if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */) )
55 # include <stdint.h>
56 typedef uint8_t BYTE;
57 typedef uint16_t U16;
58 typedef int16_t S16;
59 typedef uint32_t U32;
60 typedef int32_t S32;
61 typedef uint64_t U64;
62 typedef int64_t S64;
63 #else
64 typedef unsigned char BYTE;
65 typedef unsigned short U16;
66 typedef signed short S16;
67 typedef unsigned int U32;
68 typedef signed int S32;
69 typedef unsigned long long U64;
70 typedef signed long long S64;
71 #endif
72
73
74 /* ************************************************************
75 * Avoid fseek()'s 2GiB barrier with MSVC, MacOS, *BSD, MinGW
76 ***************************************************************/
77 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
78 # define UTIL_fseek _fseeki64
79 #elif !defined(__64BIT__) && (PLATFORM_POSIX_VERSION >= 200112L) /* No point defining Large file for 64 bit */
80 # define UTIL_fseek fseeko
81 #elif defined(__MINGW32__) && defined(__MSVCRT__) && !defined(__STRICT_ANSI__) && !defined(__NO_MINGW_LFS)
82 # define UTIL_fseek fseeko64
83 #else
84 # define UTIL_fseek fseek
85 #endif
86
87
88 /*-****************************************
89 * Sleep functions: Windows - Posix - others
90 ******************************************/
91 #if defined(_WIN32)
92 # include <windows.h>
93 # define SET_REALTIME_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)
94 # define UTIL_sleep(s) Sleep(1000*s)
95 # define UTIL_sleepMilli(milli) Sleep(milli)
96 #elif PLATFORM_POSIX_VERSION >= 0 /* Unix-like operating system */
97 # include <unistd.h>
98 # include <sys/resource.h> /* setpriority */
99 # include <time.h> /* clock_t, nanosleep, clock, CLOCKS_PER_SEC */
100 # if defined(PRIO_PROCESS)
101 # define SET_REALTIME_PRIORITY setpriority(PRIO_PROCESS, 0, -20)
102 # else
103 # define SET_REALTIME_PRIORITY /* disabled */
104 # endif
105 # define UTIL_sleep(s) sleep(s)
106 # if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 199309L)) || (PLATFORM_POSIX_VERSION >= 200112L) /* nanosleep requires POSIX.1-2001 */
107 # define UTIL_sleepMilli(milli) { struct timespec t; t.tv_sec=0; t.tv_nsec=milli*1000000ULL; nanosleep(&t, NULL); }
108 # else
109 # define UTIL_sleepMilli(milli) /* disabled */
110 # endif
111 #else
112 # define SET_REALTIME_PRIORITY /* disabled */
113 # define UTIL_sleep(s) /* disabled */
114 # define UTIL_sleepMilli(milli) /* disabled */
115 #endif
116
117
118 /* *************************************
119 * Constants
120 ***************************************/
121 #define LIST_SIZE_INCREASE (8*1024)
122
123
124 /*-****************************************
125 * Compiler specifics
126 ******************************************/
127 #if defined(__INTEL_COMPILER)
128 # pragma warning(disable : 177) /* disable: message #177: function was declared but never referenced, useful with UTIL_STATIC */
129 #endif
130 #if defined(__GNUC__)
131 # define UTIL_STATIC static __attribute__((unused))
132 #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) /* C99 */)
133 # define UTIL_STATIC static inline
134 #elif defined(_MSC_VER)
135 # define UTIL_STATIC static __inline
136 #else
137 # define UTIL_STATIC static /* this version may generate warnings for unused static functions; disable the relevant warning */
138 #endif
139
140
141 /*-****************************************
142 * Time functions
143 ******************************************/
144 #if defined(_WIN32) /* Windows */
145
146 typedef LARGE_INTEGER UTIL_time_t;
UTIL_getTime(void)147 UTIL_STATIC UTIL_time_t UTIL_getTime(void) { UTIL_time_t x; QueryPerformanceCounter(&x); return x; }
UTIL_getSpanTimeMicro(UTIL_time_t clockStart,UTIL_time_t clockEnd)148 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
149 {
150 static LARGE_INTEGER ticksPerSecond;
151 static int init = 0;
152 if (!init) {
153 if (!QueryPerformanceFrequency(&ticksPerSecond))
154 fprintf(stderr, "ERROR: QueryPerformanceFrequency() failure\n");
155 init = 1;
156 }
157 return 1000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
158 }
UTIL_getSpanTimeNano(UTIL_time_t clockStart,UTIL_time_t clockEnd)159 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
160 {
161 static LARGE_INTEGER ticksPerSecond;
162 static int init = 0;
163 if (!init) {
164 if (!QueryPerformanceFrequency(&ticksPerSecond))
165 fprintf(stderr, "ERROR: QueryPerformanceFrequency() failure\n");
166 init = 1;
167 }
168 return 1000000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
169 }
170
171 #elif defined(__APPLE__) && defined(__MACH__)
172
173 #include <mach/mach_time.h>
174 typedef U64 UTIL_time_t;
UTIL_getTime(void)175 UTIL_STATIC UTIL_time_t UTIL_getTime(void) { return mach_absolute_time(); }
UTIL_getSpanTimeMicro(UTIL_time_t clockStart,UTIL_time_t clockEnd)176 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
177 {
178 static mach_timebase_info_data_t rate;
179 static int init = 0;
180 if (!init) {
181 mach_timebase_info(&rate);
182 init = 1;
183 }
184 return (((clockEnd - clockStart) * (U64)rate.numer) / ((U64)rate.denom)) / 1000ULL;
185 }
UTIL_getSpanTimeNano(UTIL_time_t clockStart,UTIL_time_t clockEnd)186 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
187 {
188 static mach_timebase_info_data_t rate;
189 static int init = 0;
190 if (!init) {
191 mach_timebase_info(&rate);
192 init = 1;
193 }
194 return ((clockEnd - clockStart) * (U64)rate.numer) / ((U64)rate.denom);
195 }
196
197 #elif (PLATFORM_POSIX_VERSION >= 200112L) && (defined __UCLIBC__ || (defined(__GLIBC__) && ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 17) || __GLIBC__ > 2) ) )
198
199 #include <time.h>
200 typedef struct timespec UTIL_time_t;
UTIL_getTime(void)201 UTIL_STATIC UTIL_time_t UTIL_getTime(void)
202 {
203 UTIL_time_t now;
204 if (clock_gettime(CLOCK_MONOTONIC, &now))
205 fprintf(stderr, "ERROR: Failed to get time\n"); /* we could also exit() */
206 return now;
207 }
UTIL_getSpanTime(UTIL_time_t begin,UTIL_time_t end)208 UTIL_STATIC UTIL_time_t UTIL_getSpanTime(UTIL_time_t begin, UTIL_time_t end)
209 {
210 UTIL_time_t diff;
211 if (end.tv_nsec < begin.tv_nsec) {
212 diff.tv_sec = (end.tv_sec - 1) - begin.tv_sec;
213 diff.tv_nsec = (end.tv_nsec + 1000000000ULL) - begin.tv_nsec;
214 } else {
215 diff.tv_sec = end.tv_sec - begin.tv_sec;
216 diff.tv_nsec = end.tv_nsec - begin.tv_nsec;
217 }
218 return diff;
219 }
UTIL_getSpanTimeMicro(UTIL_time_t begin,UTIL_time_t end)220 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t begin, UTIL_time_t end)
221 {
222 UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
223 U64 micro = 0;
224 micro += 1000000ULL * diff.tv_sec;
225 micro += diff.tv_nsec / 1000ULL;
226 return micro;
227 }
UTIL_getSpanTimeNano(UTIL_time_t begin,UTIL_time_t end)228 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t begin, UTIL_time_t end)
229 {
230 UTIL_time_t const diff = UTIL_getSpanTime(begin, end);
231 U64 nano = 0;
232 nano += 1000000000ULL * diff.tv_sec;
233 nano += diff.tv_nsec;
234 return nano;
235 }
236
237 #else /* relies on standard C (note : clock_t measurements can be wrong when using multi-threading) */
238
239 typedef clock_t UTIL_time_t;
UTIL_getTime(void)240 UTIL_STATIC UTIL_time_t UTIL_getTime(void) { return clock(); }
UTIL_getSpanTimeMicro(UTIL_time_t clockStart,UTIL_time_t clockEnd)241 UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
UTIL_getSpanTimeNano(UTIL_time_t clockStart,UTIL_time_t clockEnd)242 UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd) { return 1000000000ULL * (clockEnd - clockStart) / CLOCKS_PER_SEC; }
243 #endif
244
245
246 /* returns time span in microseconds */
UTIL_clockSpanMicro(UTIL_time_t clockStart)247 UTIL_STATIC U64 UTIL_clockSpanMicro(UTIL_time_t clockStart)
248 {
249 UTIL_time_t const clockEnd = UTIL_getTime();
250 return UTIL_getSpanTimeMicro(clockStart, clockEnd);
251 }
252
253 /* returns time span in nanoseconds */
UTIL_clockSpanNano(UTIL_time_t clockStart)254 UTIL_STATIC U64 UTIL_clockSpanNano(UTIL_time_t clockStart)
255 {
256 UTIL_time_t const clockEnd = UTIL_getTime();
257 return UTIL_getSpanTimeNano(clockStart, clockEnd);
258 }
259
UTIL_waitForNextTick(void)260 UTIL_STATIC void UTIL_waitForNextTick(void)
261 {
262 UTIL_time_t const clockStart = UTIL_getTime();
263 UTIL_time_t clockEnd;
264 do {
265 clockEnd = UTIL_getTime();
266 } while (UTIL_getSpanTimeNano(clockStart, clockEnd) == 0);
267 }
268
269
270
271 /*-****************************************
272 * File functions
273 ******************************************/
274 #if defined(_MSC_VER)
275 #define chmod _chmod
276 typedef struct __stat64 stat_t;
277 #else
278 typedef struct stat stat_t;
279 #endif
280
281
282 UTIL_STATIC int UTIL_isRegFile(const char* infilename);
283
284
UTIL_setFileStat(const char * filename,stat_t * statbuf)285 UTIL_STATIC int UTIL_setFileStat(const char *filename, stat_t *statbuf)
286 {
287 int res = 0;
288 struct utimbuf timebuf;
289
290 if (!UTIL_isRegFile(filename))
291 return -1;
292
293 timebuf.actime = time(NULL);
294 timebuf.modtime = statbuf->st_mtime;
295 res += utime(filename, &timebuf); /* set access and modification times */
296
297 #if !defined(_WIN32)
298 res += chown(filename, statbuf->st_uid, statbuf->st_gid); /* Copy ownership */
299 #endif
300
301 res += chmod(filename, statbuf->st_mode & 07777); /* Copy file permissions */
302
303 errno = 0;
304 return -res; /* number of errors is returned */
305 }
306
307
UTIL_getFileStat(const char * infilename,stat_t * statbuf)308 UTIL_STATIC int UTIL_getFileStat(const char* infilename, stat_t *statbuf)
309 {
310 int r;
311 #if defined(_MSC_VER)
312 r = _stat64(infilename, statbuf);
313 if (r || !(statbuf->st_mode & S_IFREG)) return 0; /* No good... */
314 #else
315 r = stat(infilename, statbuf);
316 if (r || !S_ISREG(statbuf->st_mode)) return 0; /* No good... */
317 #endif
318 return 1;
319 }
320
321
UTIL_isRegFile(const char * infilename)322 UTIL_STATIC int UTIL_isRegFile(const char* infilename)
323 {
324 stat_t statbuf;
325 return UTIL_getFileStat(infilename, &statbuf); /* Only need to know whether it is a regular file */
326 }
327
328
UTIL_isDirectory(const char * infilename)329 UTIL_STATIC U32 UTIL_isDirectory(const char* infilename)
330 {
331 int r;
332 stat_t statbuf;
333 #if defined(_MSC_VER)
334 r = _stat64(infilename, &statbuf);
335 if (!r && (statbuf.st_mode & _S_IFDIR)) return 1;
336 #else
337 r = stat(infilename, &statbuf);
338 if (!r && S_ISDIR(statbuf.st_mode)) return 1;
339 #endif
340 return 0;
341 }
342
343
UTIL_getFileSize(const char * infilename)344 UTIL_STATIC U64 UTIL_getFileSize(const char* infilename)
345 {
346 int r;
347 #if defined(_MSC_VER)
348 struct __stat64 statbuf;
349 r = _stat64(infilename, &statbuf);
350 if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */
351 #elif defined(__MINGW32__) && defined (__MSVCRT__)
352 struct _stati64 statbuf;
353 r = _stati64(infilename, &statbuf);
354 if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */
355 #else
356 struct stat statbuf;
357 r = stat(infilename, &statbuf);
358 if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */
359 #endif
360 return (U64)statbuf.st_size;
361 }
362
363
UTIL_getTotalFileSize(const char ** fileNamesTable,unsigned nbFiles)364 UTIL_STATIC U64 UTIL_getTotalFileSize(const char** fileNamesTable, unsigned nbFiles)
365 {
366 U64 total = 0;
367 unsigned n;
368 for (n=0; n<nbFiles; n++)
369 total += UTIL_getFileSize(fileNamesTable[n]);
370 return total;
371 }
372
373
374 /*
375 * A modified version of realloc().
376 * If UTIL_realloc() fails the original block is freed.
377 */
UTIL_realloc(void * ptr,size_t size)378 UTIL_STATIC void *UTIL_realloc(void *ptr, size_t size)
379 {
380 void *newptr = realloc(ptr, size);
381 if (newptr) return newptr;
382 free(ptr);
383 return NULL;
384 }
385
386
387 #ifdef _WIN32
388 # define UTIL_HAS_CREATEFILELIST
389
UTIL_prepareFileList(const char * dirName,char ** bufStart,size_t * pos,char ** bufEnd)390 UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd)
391 {
392 char* path;
393 int dirLength, fnameLength, pathLength, nbFiles = 0;
394 WIN32_FIND_DATAA cFile;
395 HANDLE hFile;
396
397 dirLength = (int)strlen(dirName);
398 path = (char*) malloc(dirLength + 3);
399 if (!path) return 0;
400
401 memcpy(path, dirName, dirLength);
402 path[dirLength] = '\\';
403 path[dirLength+1] = '*';
404 path[dirLength+2] = 0;
405
406 hFile=FindFirstFileA(path, &cFile);
407 if (hFile == INVALID_HANDLE_VALUE) {
408 fprintf(stderr, "Cannot open directory '%s'\n", dirName);
409 return 0;
410 }
411 free(path);
412
413 do {
414 fnameLength = (int)strlen(cFile.cFileName);
415 path = (char*) malloc(dirLength + fnameLength + 2);
416 if (!path) { FindClose(hFile); return 0; }
417 memcpy(path, dirName, dirLength);
418 path[dirLength] = '\\';
419 memcpy(path+dirLength+1, cFile.cFileName, fnameLength);
420 pathLength = dirLength+1+fnameLength;
421 path[pathLength] = 0;
422 if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
423 if (strcmp (cFile.cFileName, "..") == 0 ||
424 strcmp (cFile.cFileName, ".") == 0) continue;
425
426 nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd); /* Recursively call "UTIL_prepareFileList" with the new path. */
427 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
428 }
429 else if ((cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED)) {
430 if (*bufStart + *pos + pathLength >= *bufEnd) {
431 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
432 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
433 *bufEnd = *bufStart + newListSize;
434 if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; }
435 }
436 if (*bufStart + *pos + pathLength < *bufEnd) {
437 strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos));
438 *pos += pathLength + 1;
439 nbFiles++;
440 }
441 }
442 free(path);
443 } while (FindNextFileA(hFile, &cFile));
444
445 FindClose(hFile);
446 return nbFiles;
447 }
448
449 #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */
450 # define UTIL_HAS_CREATEFILELIST
451 # include <dirent.h> /* opendir, readdir */
452 # include <string.h> /* strerror, memcpy */
453
UTIL_prepareFileList(const char * dirName,char ** bufStart,size_t * pos,char ** bufEnd)454 UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd)
455 {
456 DIR *dir;
457 struct dirent *entry;
458 char* path;
459 int dirLength, fnameLength, pathLength, nbFiles = 0;
460
461 if (!(dir = opendir(dirName))) {
462 fprintf(stderr, "Cannot open directory '%s': %s\n", dirName, strerror(errno));
463 return 0;
464 }
465
466 dirLength = (int)strlen(dirName);
467 errno = 0;
468 while ((entry = readdir(dir)) != NULL) {
469 if (strcmp (entry->d_name, "..") == 0 ||
470 strcmp (entry->d_name, ".") == 0) continue;
471 fnameLength = (int)strlen(entry->d_name);
472 path = (char*) malloc(dirLength + fnameLength + 2);
473 if (!path) { closedir(dir); return 0; }
474 memcpy(path, dirName, dirLength);
475 path[dirLength] = '/';
476 memcpy(path+dirLength+1, entry->d_name, fnameLength);
477 pathLength = dirLength+1+fnameLength;
478 path[pathLength] = 0;
479
480 if (UTIL_isDirectory(path)) {
481 nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd); /* Recursively call "UTIL_prepareFileList" with the new path. */
482 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
483 } else {
484 if (*bufStart + *pos + pathLength >= *bufEnd) {
485 ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE;
486 *bufStart = (char*)UTIL_realloc(*bufStart, newListSize);
487 *bufEnd = *bufStart + newListSize;
488 if (*bufStart == NULL) { free(path); closedir(dir); return 0; }
489 }
490 if (*bufStart + *pos + pathLength < *bufEnd) {
491 strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos));
492 *pos += pathLength + 1;
493 nbFiles++;
494 }
495 }
496 free(path);
497 errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */
498 }
499
500 if (errno != 0) {
501 fprintf(stderr, "readdir(%s) error: %s\n", dirName, strerror(errno));
502 free(*bufStart);
503 *bufStart = NULL;
504 }
505 closedir(dir);
506 return nbFiles;
507 }
508
509 #else
510
UTIL_prepareFileList(const char * dirName,char ** bufStart,size_t * pos,char ** bufEnd)511 UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd)
512 {
513 (void)bufStart; (void)bufEnd; (void)pos;
514 fprintf(stderr, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE)\n", dirName);
515 return 0;
516 }
517
518 #endif /* #ifdef _WIN32 */
519
520 /*
521 * UTIL_createFileList - takes a list of files and directories (params: inputNames, inputNamesNb), scans directories,
522 * and returns a new list of files (params: return value, allocatedBuffer, allocatedNamesNb).
523 * After finishing usage of the list the structures should be freed with UTIL_freeFileList(params: return value, allocatedBuffer)
524 * In case of error UTIL_createFileList returns NULL and UTIL_freeFileList should not be called.
525 */
UTIL_createFileList(const char ** inputNames,unsigned inputNamesNb,char ** allocatedBuffer,unsigned * allocatedNamesNb)526 UTIL_STATIC const char** UTIL_createFileList(const char **inputNames, unsigned inputNamesNb, char** allocatedBuffer, unsigned* allocatedNamesNb)
527 {
528 size_t pos;
529 unsigned i, nbFiles;
530 char* buf = (char*)malloc(LIST_SIZE_INCREASE);
531 char* bufend = buf + LIST_SIZE_INCREASE;
532 const char** fileTable;
533
534 if (!buf) return NULL;
535
536 for (i=0, pos=0, nbFiles=0; i<inputNamesNb; i++) {
537 if (!UTIL_isDirectory(inputNames[i])) {
538 size_t const len = strlen(inputNames[i]);
539 if (buf + pos + len >= bufend) {
540 ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE;
541 buf = (char*)UTIL_realloc(buf, newListSize);
542 bufend = buf + newListSize;
543 if (!buf) return NULL;
544 }
545 if (buf + pos + len < bufend) {
546 strncpy(buf + pos, inputNames[i], bufend - (buf + pos));
547 pos += len + 1;
548 nbFiles++;
549 }
550 } else {
551 nbFiles += UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend);
552 if (buf == NULL) return NULL;
553 } }
554
555 if (nbFiles == 0) { free(buf); return NULL; }
556
557 fileTable = (const char**)malloc((nbFiles+1) * sizeof(const char*));
558 if (!fileTable) { free(buf); return NULL; }
559
560 for (i=0, pos=0; i<nbFiles; i++) {
561 fileTable[i] = buf + pos;
562 pos += strlen(fileTable[i]) + 1;
563 }
564
565 if (buf + pos > bufend) { free(buf); free((void*)fileTable); return NULL; }
566
567 *allocatedBuffer = buf;
568 *allocatedNamesNb = nbFiles;
569
570 return fileTable;
571 }
572
573
UTIL_freeFileList(const char ** filenameTable,char * allocatedBuffer)574 UTIL_STATIC void UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer)
575 {
576 if (allocatedBuffer) free(allocatedBuffer);
577 if (filenameTable) free((void*)filenameTable);
578 }
579
580
581 #if defined (__cplusplus)
582 }
583 #endif
584
585 #endif /* UTIL_H_MODULE */
586