util.h 22 KB

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