| Przemyslaw Skibinski | 7a8a03c | 2016-12-21 15:08:44 +0100 | [diff] [blame] | 1 | /** |
| 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 | */ |
| inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 11 | |
| inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 12 | #ifndef UTIL_H_MODULE |
| 13 | #define UTIL_H_MODULE |
| 14 | |
| 15 | #if defined (__cplusplus) |
| 16 | extern "C" { |
| 17 | #endif |
| 18 | |
| inikep | 9c22e57 | 2016-05-05 11:53:42 +0200 | [diff] [blame] | 19 | |
| Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 20 | |
| inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 21 | /*-**************************************** |
| 22 | * Dependencies |
| 23 | ******************************************/ |
| Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 24 | #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 Skibinski | b40884f | 2016-11-03 09:54:53 +0100 | [diff] [blame] | 30 | #if defined(_MSC_VER) |
| Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 31 | # include <sys/utime.h> /* utime */ |
| 32 | # include <io.h> /* _chmod */ |
| Przemyslaw Skibinski | b40884f | 2016-11-03 09:54:53 +0100 | [diff] [blame] | 33 | #else |
| Yann Collet | fda539f | 2016-12-12 01:03:23 +0100 | [diff] [blame] | 34 | # include <unistd.h> /* chown, stat */ |
| 35 | # include <utime.h> /* utime */ |
| Przemyslaw Skibinski | b40884f | 2016-11-03 09:54:53 +0100 | [diff] [blame] | 36 | #endif |
| Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 37 | #include <time.h> /* time */ |
| Przemyslaw Skibinski | b40884f | 2016-11-03 09:54:53 +0100 | [diff] [blame] | 38 | #include <errno.h> |
| Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 39 | #include "mem.h" /* U32, U64 */ |
| inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 40 | |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 41 | |
| inikep | 9c22e57 | 2016-05-05 11:53:42 +0200 | [diff] [blame] | 42 | /*-**************************************** |
| 43 | * Sleep functions: Windows - Posix - others |
| 44 | ******************************************/ |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 45 | #if defined(_WIN32) |
| inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 46 | # include <windows.h> |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 47 | # define SET_HIGH_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS) |
| inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 48 | # define UTIL_sleep(s) Sleep(1000*s) |
| 49 | # define UTIL_sleepMilli(milli) Sleep(milli) |
| Przemyslaw Skibinski | b0f3663 | 2016-12-16 15:41:18 +0100 | [diff] [blame] | 50 | #elif PLATFORM_POSIX_VERSION >= 0 /* Unix-like operating system */ |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 51 | # include <unistd.h> |
| 52 | # include <sys/resource.h> /* setpriority */ |
| 53 | # include <time.h> /* clock_t, nanosleep, clock, CLOCKS_PER_SEC */ |
| Peter (Stig) Edwards | 04773ac | 2016-05-21 12:15:48 +0100 | [diff] [blame] | 54 | # if defined(PRIO_PROCESS) |
| 55 | # define SET_HIGH_PRIORITY setpriority(PRIO_PROCESS, 0, -20) |
| 56 | # else |
| 57 | # define SET_HIGH_PRIORITY /* disabled */ |
| 58 | # endif |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 59 | # define UTIL_sleep(s) sleep(s) |
| Przemyslaw Skibinski | b0f3663 | 2016-12-16 15:41:18 +0100 | [diff] [blame] | 60 | # if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 199309L)) || (PLATFORM_POSIX_VERSION >= 200112L) /* nanosleep requires POSIX.1-2001 */ |
| inikep | 9c22e57 | 2016-05-05 11:53:42 +0200 | [diff] [blame] | 61 | # 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 |
| inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 65 | #else |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 66 | # define SET_HIGH_PRIORITY /* disabled */ |
| 67 | # define UTIL_sleep(s) /* disabled */ |
| inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 68 | # define UTIL_sleepMilli(milli) /* disabled */ |
| inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 69 | #endif |
| 70 | |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 71 | |
| Przemyslaw Skibinski | ead350b | 2016-12-21 09:04:59 +0100 | [diff] [blame] | 72 | /* ************************************* |
| 73 | * Constants |
| 74 | ***************************************/ |
| 75 | #define LIST_SIZE_INCREASE (8*1024) |
| 76 | |
| 77 | |
| Przemyslaw Skibinski | ead350b | 2016-12-21 09:04:59 +0100 | [diff] [blame] | 78 | /*-**************************************** |
| 79 | * Compiler specifics |
| 80 | ******************************************/ |
| Przemyslaw Skibinski | 2f6ccee | 2016-12-21 13:23:34 +0100 | [diff] [blame] | 81 | #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 Skibinski | ead350b | 2016-12-21 09:04:59 +0100 | [diff] [blame] | 84 | #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 | |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 95 | /*-**************************************** |
| 96 | * Time functions |
| 97 | ******************************************/ |
| cyan4973 | 5fba09f | 2017-01-20 12:23:30 -0800 | [diff] [blame] | 98 | #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 */ |
| inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 107 | typedef LARGE_INTEGER UTIL_time_t; |
| inikep | aaaf923 | 2016-05-09 16:19:25 +0200 | [diff] [blame] | 108 | 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; } |
| cyan4973 | 5fba09f | 2017-01-20 12:23:30 -0800 | [diff] [blame] | 112 | #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; } |
| inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 118 | #endif |
| 119 | |
| 120 | |
| inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 121 | /* returns time span in microseconds */ |
| 122 | UTIL_STATIC U64 UTIL_clockSpanMicro( UTIL_time_t clockStart, UTIL_time_t ticksPerSecond ) |
| 123 | { |
| 124 | UTIL_time_t clockEnd; |
| inikep | aaaf923 | 2016-05-09 16:19:25 +0200 | [diff] [blame] | 125 | UTIL_getTime(&clockEnd); |
| inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 126 | return UTIL_getSpanTimeMicro(ticksPerSecond, clockStart, clockEnd); |
| 127 | } |
| 128 | |
| 129 | |
| 130 | UTIL_STATIC void UTIL_waitForNextTick(UTIL_time_t ticksPerSecond) |
| 131 | { |
| 132 | UTIL_time_t clockStart, clockEnd; |
| inikep | aaaf923 | 2016-05-09 16:19:25 +0200 | [diff] [blame] | 133 | UTIL_getTime(&clockStart); |
| Yann Collet | e162ace | 2016-05-20 11:24:35 +0200 | [diff] [blame] | 134 | do { |
| 135 | UTIL_getTime(&clockEnd); |
| inikep | d5ff2c3 | 2016-04-28 14:40:45 +0200 | [diff] [blame] | 136 | } while (UTIL_getSpanTimeNano(ticksPerSecond, clockStart, clockEnd) == 0); |
| inikep | 83c76b4 | 2016-04-28 13:16:01 +0200 | [diff] [blame] | 137 | } |
| 138 | |
| 139 | |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 140 | |
| 141 | /*-**************************************** |
| 142 | * File functions |
| 143 | ******************************************/ |
| ds77 | 45f0c20 | 2017-02-10 18:37:57 +0100 | [diff] [blame^] | 144 | #if defined(_MSC_VER) |
| Przemyslaw Skibinski | b40884f | 2016-11-03 09:54:53 +0100 | [diff] [blame] | 145 | #define chmod _chmod |
| - | 7ec315d | 2017-02-10 13:27:43 +0100 | [diff] [blame] | 146 | typedef struct __stat64 stat_t; |
| Przemyslaw Skibinski | fcf22e3 | 2016-11-02 14:08:07 +0100 | [diff] [blame] | 147 | #else |
| 148 | typedef struct stat stat_t; |
| 149 | #endif |
| Przemyslaw Skibinski | d872b64 | 2016-11-02 12:52:20 +0100 | [diff] [blame] | 150 | |
| Przemyslaw Skibinski | fcf22e3 | 2016-11-02 14:08:07 +0100 | [diff] [blame] | 151 | |
| 152 | UTIL_STATIC int UTIL_setFileStat(const char *filename, stat_t *statbuf) |
| 153 | { |
| 154 | int res = 0; |
| 155 | struct utimbuf timebuf; |
| 156 | |
| Przemyslaw Skibinski | b40884f | 2016-11-03 09:54:53 +0100 | [diff] [blame] | 157 | timebuf.actime = time(NULL); |
| 158 | timebuf.modtime = statbuf->st_mtime; |
| 159 | res += utime(filename, &timebuf); /* set access and modification times */ |
| 160 | |
| Przemyslaw Skibinski | fcf22e3 | 2016-11-02 14:08:07 +0100 | [diff] [blame] | 161 | #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 Skibinski | d872b64 | 2016-11-02 12:52:20 +0100 | [diff] [blame] | 166 | |
| Przemyslaw Skibinski | fcf22e3 | 2016-11-02 14:08:07 +0100 | [diff] [blame] | 167 | errno = 0; |
| 168 | return -res; /* number of errors is returned */ |
| Przemyslaw Skibinski | d872b64 | 2016-11-02 12:52:20 +0100 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | |
| Przemyslaw Skibinski | fcf22e3 | 2016-11-02 14:08:07 +0100 | [diff] [blame] | 172 | UTIL_STATIC int UTIL_getFileStat(const char* infilename, stat_t *statbuf) |
| Przemyslaw Skibinski | d872b64 | 2016-11-02 12:52:20 +0100 | [diff] [blame] | 173 | { |
| 174 | int r; |
| ds77 | 45f0c20 | 2017-02-10 18:37:57 +0100 | [diff] [blame^] | 175 | #if defined(_MSC_VER) |
| Przemyslaw Skibinski | fcf22e3 | 2016-11-02 14:08:07 +0100 | [diff] [blame] | 176 | r = _stat64(infilename, statbuf); |
| 177 | if (r || !(statbuf->st_mode & S_IFREG)) return 0; /* No good... */ |
| Przemyslaw Skibinski | d872b64 | 2016-11-02 12:52:20 +0100 | [diff] [blame] | 178 | #else |
| Przemyslaw Skibinski | fcf22e3 | 2016-11-02 14:08:07 +0100 | [diff] [blame] | 179 | r = stat(infilename, statbuf); |
| 180 | if (r || !S_ISREG(statbuf->st_mode)) return 0; /* No good... */ |
| Przemyslaw Skibinski | d872b64 | 2016-11-02 12:52:20 +0100 | [diff] [blame] | 181 | #endif |
| Przemyslaw Skibinski | fcf22e3 | 2016-11-02 14:08:07 +0100 | [diff] [blame] | 182 | return 1; |
| Przemyslaw Skibinski | d872b64 | 2016-11-02 12:52:20 +0100 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | |
| inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 186 | UTIL_STATIC U64 UTIL_getFileSize(const char* infilename) |
| 187 | { |
| 188 | int r; |
| ds77 | 45f0c20 | 2017-02-10 18:37:57 +0100 | [diff] [blame^] | 189 | #if defined(_MSC_VER) |
| 190 | struct _stat64 statbuf; |
| inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 191 | r = _stat64(infilename, &statbuf); |
| 192 | if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */ |
| ds77 | 45f0c20 | 2017-02-10 18:37:57 +0100 | [diff] [blame^] | 193 | #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... */ |
| inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 197 | #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 | |
| inikep | 55d047a | 2016-04-28 16:50:13 +0200 | [diff] [blame] | 206 | UTIL_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 | |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 216 | UTIL_STATIC int UTIL_doesFileExists(const char* infilename) |
| 217 | { |
| 218 | int r; |
| ds77 | 45f0c20 | 2017-02-10 18:37:57 +0100 | [diff] [blame^] | 219 | #if defined(_MSC_VER) |
| - | 7ec315d | 2017-02-10 13:27:43 +0100 | [diff] [blame] | 220 | struct __stat64 statbuf; |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 221 | 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 | |
| inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 232 | UTIL_STATIC U32 UTIL_isDirectory(const char* infilename) |
| 233 | { |
| 234 | int r; |
| ds77 | 45f0c20 | 2017-02-10 18:37:57 +0100 | [diff] [blame^] | 235 | #if defined(_MSC_VER) |
| - | 7ec315d | 2017-02-10 13:27:43 +0100 | [diff] [blame] | 236 | struct __stat64 statbuf; |
| inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 237 | 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 | |
| inikep | 6173931 | 2016-09-15 18:58:18 +0200 | [diff] [blame] | 247 | /* |
| 248 | * A modified version of realloc(). |
| 249 | * If UTIL_realloc() fails the original block is freed. |
| 250 | */ |
| 251 | UTIL_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 | |
| inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 259 | |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 260 | #ifdef _WIN32 |
| inikep | 9c22e57 | 2016-05-05 11:53:42 +0200 | [diff] [blame] | 261 | # define UTIL_HAS_CREATEFILELIST |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 262 | |
| inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 263 | UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd) |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 264 | { |
| inikep | 1c5ba8a | 2016-09-13 13:13:10 +0200 | [diff] [blame] | 265 | char* path; |
| 266 | int dirLength, fnameLength, pathLength, nbFiles = 0; |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 267 | WIN32_FIND_DATA cFile; |
| 268 | HANDLE hFile; |
| 269 | |
| inikep | 9f25fcf | 2016-09-13 16:38:54 +0200 | [diff] [blame] | 270 | dirLength = (int)strlen(dirName); |
| inikep | 1c5ba8a | 2016-09-13 13:13:10 +0200 | [diff] [blame] | 271 | 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; |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 278 | |
| 279 | hFile=FindFirstFile(path, &cFile); |
| 280 | if (hFile == INVALID_HANDLE_VALUE) { |
| 281 | fprintf(stderr, "Cannot open directory '%s'\n", dirName); |
| 282 | return 0; |
| 283 | } |
| inikep | 1c5ba8a | 2016-09-13 13:13:10 +0200 | [diff] [blame] | 284 | free(path); |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 285 | |
| inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 286 | do { |
| inikep | 9f25fcf | 2016-09-13 16:38:54 +0200 | [diff] [blame] | 287 | fnameLength = (int)strlen(cFile.cFileName); |
| inikep | 1c5ba8a | 2016-09-13 13:13:10 +0200 | [diff] [blame] | 288 | 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; |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 295 | if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) { |
| 296 | if (strcmp (cFile.cFileName, "..") == 0 || |
| inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 297 | strcmp (cFile.cFileName, ".") == 0) continue; |
| inikep | e416e30 | 2016-08-24 17:32:09 +0200 | [diff] [blame] | 298 | |
| inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 299 | nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd); /* Recursively call "UTIL_prepareFileList" with the new path. */ |
| inikep | 1c5ba8a | 2016-09-13 13:13:10 +0200 | [diff] [blame] | 300 | if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; } |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 301 | } |
| 302 | else if ((cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED)) { |
| inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 303 | if (*bufStart + *pos + pathLength >= *bufEnd) { |
| 304 | ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE; |
| inikep | 6173931 | 2016-09-15 18:58:18 +0200 | [diff] [blame] | 305 | *bufStart = (char*)UTIL_realloc(*bufStart, newListSize); |
| inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 306 | *bufEnd = *bufStart + newListSize; |
| inikep | 1c5ba8a | 2016-09-13 13:13:10 +0200 | [diff] [blame] | 307 | if (*bufStart == NULL) { free(path); FindClose(hFile); return 0; } |
| inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 308 | } |
| 309 | if (*bufStart + *pos + pathLength < *bufEnd) { |
| 310 | strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos)); |
| 311 | *pos += pathLength + 1; |
| 312 | nbFiles++; |
| 313 | } |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 314 | } |
| inikep | 1c5ba8a | 2016-09-13 13:13:10 +0200 | [diff] [blame] | 315 | free(path); |
| inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 316 | } while (FindNextFile(hFile, &cFile)); |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 317 | |
| 318 | FindClose(hFile); |
| 319 | return nbFiles; |
| 320 | } |
| 321 | |
| Przemyslaw Skibinski | 0b37205 | 2016-12-16 17:12:23 +0100 | [diff] [blame] | 322 | #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L) /* opendir, readdir require POSIX.1-2001 */ |
| inikep | 9c22e57 | 2016-05-05 11:53:42 +0200 | [diff] [blame] | 323 | # define UTIL_HAS_CREATEFILELIST |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 324 | # include <dirent.h> /* opendir, readdir */ |
| Przemyslaw Skibinski | ead350b | 2016-12-21 09:04:59 +0100 | [diff] [blame] | 325 | # include <string.h> /* strerror, memcpy */ |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 326 | |
| inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 327 | UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd) |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 328 | { |
| 329 | DIR *dir; |
| 330 | struct dirent *entry; |
| inikep | 1c5ba8a | 2016-09-13 13:13:10 +0200 | [diff] [blame] | 331 | char* path; |
| 332 | int dirLength, fnameLength, pathLength, nbFiles = 0; |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 333 | |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 334 | if (!(dir = opendir(dirName))) { |
| 335 | fprintf(stderr, "Cannot open directory '%s': %s\n", dirName, strerror(errno)); |
| 336 | return 0; |
| 337 | } |
| Yann Collet | e162ace | 2016-05-20 11:24:35 +0200 | [diff] [blame] | 338 | |
| inikep | 9f25fcf | 2016-09-13 16:38:54 +0200 | [diff] [blame] | 339 | dirLength = (int)strlen(dirName); |
| inikep | 7bc5c6b | 2016-07-26 11:07:37 +0200 | [diff] [blame] | 340 | errno = 0; |
| inikep | 3eabe9b | 2016-05-12 17:15:41 +0200 | [diff] [blame] | 341 | while ((entry = readdir(dir)) != NULL) { |
| inikep | 0bd0fae | 2016-05-05 13:10:57 +0200 | [diff] [blame] | 342 | if (strcmp (entry->d_name, "..") == 0 || |
| 343 | strcmp (entry->d_name, ".") == 0) continue; |
| inikep | 9f25fcf | 2016-09-13 16:38:54 +0200 | [diff] [blame] | 344 | fnameLength = (int)strlen(entry->d_name); |
| inikep | 1c5ba8a | 2016-09-13 13:13:10 +0200 | [diff] [blame] | 345 | 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 | |
| inikep | 0bd0fae | 2016-05-05 13:10:57 +0200 | [diff] [blame] | 353 | if (UTIL_isDirectory(path)) { |
| inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 354 | nbFiles += UTIL_prepareFileList(path, bufStart, pos, bufEnd); /* Recursively call "UTIL_prepareFileList" with the new path. */ |
| inikep | 1c5ba8a | 2016-09-13 13:13:10 +0200 | [diff] [blame] | 355 | if (*bufStart == NULL) { free(path); closedir(dir); return 0; } |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 356 | } else { |
| inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 357 | if (*bufStart + *pos + pathLength >= *bufEnd) { |
| 358 | ptrdiff_t newListSize = (*bufEnd - *bufStart) + LIST_SIZE_INCREASE; |
| inikep | 6173931 | 2016-09-15 18:58:18 +0200 | [diff] [blame] | 359 | *bufStart = (char*)UTIL_realloc(*bufStart, newListSize); |
| inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 360 | *bufEnd = *bufStart + newListSize; |
| inikep | 1c5ba8a | 2016-09-13 13:13:10 +0200 | [diff] [blame] | 361 | if (*bufStart == NULL) { free(path); closedir(dir); return 0; } |
| inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 362 | } |
| 363 | if (*bufStart + *pos + pathLength < *bufEnd) { |
| 364 | strncpy(*bufStart + *pos, path, *bufEnd - (*bufStart + *pos)); |
| 365 | *pos += pathLength + 1; |
| 366 | nbFiles++; |
| 367 | } |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 368 | } |
| inikep | 1c5ba8a | 2016-09-13 13:13:10 +0200 | [diff] [blame] | 369 | free(path); |
| inikep | e416e30 | 2016-08-24 17:32:09 +0200 | [diff] [blame] | 370 | errno = 0; /* clear errno after UTIL_isDirectory, UTIL_prepareFileList */ |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 371 | } |
| 372 | |
| inikep | 7bc5c6b | 2016-07-26 11:07:37 +0200 | [diff] [blame] | 373 | if (errno != 0) { |
| 374 | fprintf(stderr, "readdir(%s) error: %s\n", dirName, strerror(errno)); |
| 375 | free(*bufStart); |
| 376 | *bufStart = NULL; |
| 377 | } |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 378 | closedir(dir); |
| 379 | return nbFiles; |
| 380 | } |
| 381 | |
| 382 | #else |
| 383 | |
| inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 384 | UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char** bufStart, size_t* pos, char** bufEnd) |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 385 | { |
| inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 386 | (void)bufStart; (void)bufEnd; (void)pos; |
| Przemyslaw Skibinski | ead350b | 2016-12-21 09:04:59 +0100 | [diff] [blame] | 387 | fprintf(stderr, "Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE)\n", dirName); |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 388 | return 0; |
| 389 | } |
| 390 | |
| inikep | e416e30 | 2016-08-24 17:32:09 +0200 | [diff] [blame] | 391 | #endif /* #ifdef _WIN32 */ |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 392 | |
| Yann Collet | e162ace | 2016-05-20 11:24:35 +0200 | [diff] [blame] | 393 | /* |
| 394 | * UTIL_createFileList - takes a list of files and directories (params: inputNames, inputNamesNb), scans directories, |
| inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 395 | * 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 | */ |
| 399 | UTIL_STATIC const char** UTIL_createFileList(const char **inputNames, unsigned inputNamesNb, char** allocatedBuffer, unsigned* allocatedNamesNb) |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 400 | { |
| inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 401 | size_t pos; |
| inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 402 | unsigned i, nbFiles; |
| Yann Collet | fda539f | 2016-12-12 01:03:23 +0100 | [diff] [blame] | 403 | char* buf = (char*)malloc(LIST_SIZE_INCREASE); |
| 404 | char* bufend = buf + LIST_SIZE_INCREASE; |
| inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 405 | const char** fileTable; |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 406 | |
| inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 407 | if (!buf) return NULL; |
| inikep | 9c22e57 | 2016-05-05 11:53:42 +0200 | [diff] [blame] | 408 | |
| inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 409 | for (i=0, pos=0, nbFiles=0; i<inputNamesNb; i++) { |
| inikep | 957823f | 2016-05-25 15:30:55 +0200 | [diff] [blame] | 410 | if (!UTIL_isDirectory(inputNames[i])) { |
| Yann Collet | fda539f | 2016-12-12 01:03:23 +0100 | [diff] [blame] | 411 | size_t const len = strlen(inputNames[i]); |
| inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 412 | if (buf + pos + len >= bufend) { |
| 413 | ptrdiff_t newListSize = (bufend - buf) + LIST_SIZE_INCREASE; |
| inikep | 6173931 | 2016-09-15 18:58:18 +0200 | [diff] [blame] | 414 | buf = (char*)UTIL_realloc(buf, newListSize); |
| inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 415 | bufend = buf + newListSize; |
| inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 416 | if (!buf) return NULL; |
| inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 417 | } |
| 418 | if (buf + pos + len < bufend) { |
| 419 | strncpy(buf + pos, inputNames[i], bufend - (buf + pos)); |
| 420 | pos += len + 1; |
| 421 | nbFiles++; |
| 422 | } |
| Yann Collet | 415251c | 2016-08-01 14:26:49 +0200 | [diff] [blame] | 423 | } else { |
| inikep | 4dbf7f4 | 2016-05-11 14:11:00 +0200 | [diff] [blame] | 424 | nbFiles += UTIL_prepareFileList(inputNames[i], &buf, &pos, &bufend); |
| inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 425 | if (buf == NULL) return NULL; |
| Yann Collet | 415251c | 2016-08-01 14:26:49 +0200 | [diff] [blame] | 426 | } } |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 427 | |
| inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 428 | if (nbFiles == 0) { free(buf); return NULL; } |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 429 | |
| inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 430 | fileTable = (const char**)malloc((nbFiles+1) * sizeof(const char*)); |
| 431 | if (!fileTable) { free(buf); return NULL; } |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 432 | |
| Yann Collet | fda539f | 2016-12-12 01:03:23 +0100 | [diff] [blame] | 433 | for (i=0, pos=0; i<nbFiles; i++) { |
| inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 434 | fileTable[i] = buf + pos; |
| 435 | pos += strlen(fileTable[i]) + 1; |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 436 | } |
| 437 | |
| inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 438 | if (buf + pos > bufend) { free(buf); free((void*)fileTable); return NULL; } |
| 439 | |
| 440 | *allocatedBuffer = buf; |
| 441 | *allocatedNamesNb = nbFiles; |
| 442 | |
| 443 | return fileTable; |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 444 | } |
| 445 | |
| 446 | |
| inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 447 | UTIL_STATIC void UTIL_freeFileList(const char** filenameTable, char* allocatedBuffer) |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 448 | { |
| inikep | 0bdb6a8 | 2016-05-13 10:52:02 +0200 | [diff] [blame] | 449 | if (allocatedBuffer) free(allocatedBuffer); |
| 450 | if (filenameTable) free((void*)filenameTable); |
| inikep | 3163403 | 2016-05-05 00:25:38 +0200 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | |
| inikep | 69fcd7c | 2016-04-28 12:23:33 +0200 | [diff] [blame] | 454 | #if defined (__cplusplus) |
| 455 | } |
| 456 | #endif |
| 457 | |
| 458 | #endif /* UTIL_H_MODULE */ |