23 #if defined (__cplusplus)
38 #include <sys/types.h>
41 # include <sys/utime.h>
45 # if PLATFORM_POSIX_VERSION < 200809L
49 # include <sys/stat.h>
61 #if !defined (__VMS) && (defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) ) )
71 typedef unsigned char BYTE;
72 typedef unsigned short U16;
73 typedef signed short S16;
74 typedef unsigned int U32;
75 typedef signed int S32;
76 typedef unsigned long long U64;
77 typedef signed long long S64;
84 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
85 # define UTIL_fseek _fseeki64
86 #elif !defined(__64BIT__) && (PLATFORM_POSIX_VERSION >= 200112L)
87 # define UTIL_fseek fseeko
88 #elif defined(__MINGW32__) && defined(__MSVCRT__) && !defined(__STRICT_ANSI__) && !defined(__NO_MINGW_LFS)
89 # define UTIL_fseek fseeko64
91 # define UTIL_fseek fseek
100 # define SET_REALTIME_PRIORITY SetPriorityClass(GetCurrentProcess(), REALTIME_PRIORITY_CLASS)
101 # define UTIL_sleep(s) Sleep(1000*s)
102 # define UTIL_sleepMilli(milli) Sleep(milli)
103 #elif PLATFORM_POSIX_VERSION >= 0
105 # include <sys/resource.h>
107 # if defined(PRIO_PROCESS)
108 # define SET_REALTIME_PRIORITY setpriority(PRIO_PROCESS, 0, -20)
110 # define SET_REALTIME_PRIORITY
112 # define UTIL_sleep(s) sleep(s)
113 # if (defined(__linux__) && (PLATFORM_POSIX_VERSION >= 199309L)) || (PLATFORM_POSIX_VERSION >= 200112L)
114 # define UTIL_sleepMilli(milli) { struct timespec t; t.tv_sec=0; t.tv_nsec=milli*1000000ULL; nanosleep(&t, NULL); }
116 # define UTIL_sleepMilli(milli)
119 # define SET_REALTIME_PRIORITY
120 # define UTIL_sleep(s)
121 # define UTIL_sleepMilli(milli)
128 #if defined(_MSC_VER)
129 # define UTIL_TYPE_stat __stat64
130 # define UTIL_stat _stat64
131 # define UTIL_fstat _fstat64
132 # define UTIL_STAT_MODE_ISREG(st_mode) ((st_mode) & S_IFREG)
133 #elif defined(__MINGW32__) && defined (__MSVCRT__)
134 # define UTIL_TYPE_stat _stati64
135 # define UTIL_stat _stati64
136 # define UTIL_fstat _fstati64
137 # define UTIL_STAT_MODE_ISREG(st_mode) ((st_mode) & S_IFREG)
139 # define UTIL_TYPE_stat stat
140 # define UTIL_stat stat
141 # define UTIL_fstat fstat
142 # define UTIL_STAT_MODE_ISREG(st_mode) (S_ISREG(st_mode))
149 #if defined(_MSC_VER)
150 # define UTIL_fileno _fileno
152 # define UTIL_fileno fileno
158 #define LIST_SIZE_INCREASE (8*1024)
164 #if defined(__INTEL_COMPILER)
165 # pragma warning(disable : 177)
167 #if defined(__GNUC__)
168 # define UTIL_STATIC static __attribute__((unused))
169 #elif defined (__cplusplus) || (defined (__STDC_VERSION__) && (__STDC_VERSION__ >= 199901L) )
170 # define UTIL_STATIC static inline
171 #elif defined(_MSC_VER)
172 # define UTIL_STATIC static __inline
174 # define UTIL_STATIC static
187 static LARGE_INTEGER ticksPerSecond;
190 if (!QueryPerformanceFrequency(&ticksPerSecond))
191 fprintf(stderr,
"ERROR: QueryPerformanceFrequency() failure\n");
194 return 1000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
198 static LARGE_INTEGER ticksPerSecond;
201 if (!QueryPerformanceFrequency(&ticksPerSecond))
202 fprintf(stderr,
"ERROR: QueryPerformanceFrequency() failure\n");
205 return 1000000000ULL*(clockEnd.QuadPart - clockStart.QuadPart)/ticksPerSecond.QuadPart;
208 #elif defined(__APPLE__) && defined(__MACH__)
210 #include <mach/mach_time.h>
215 static mach_timebase_info_data_t rate;
218 mach_timebase_info(&rate);
221 return (((clockEnd - clockStart) * (
U64)rate.numer) / ((
U64)rate.denom)) / 1000ULL;
225 static mach_timebase_info_data_t rate;
228 mach_timebase_info(&rate);
231 return ((clockEnd - clockStart) * (
U64)rate.numer) / ((
U64)rate.denom);
234 #elif (PLATFORM_POSIX_VERSION >= 200112L) && (defined __UCLIBC__ || (defined(__GLIBC__) && ((__GLIBC__ == 2 && __GLIBC_MINOR__ >= 17) || __GLIBC__ > 2) ) )
241 if (clock_gettime(CLOCK_MONOTONIC, &now))
242 fprintf(stderr,
"ERROR: Failed to get time\n");
248 if (
end.tv_nsec < begin.tv_nsec) {
249 diff.tv_sec = (
end.tv_sec - 1) - begin.tv_sec;
250 diff.tv_nsec = (
end.tv_nsec + 1000000000ULL) - begin.tv_nsec;
252 diff.tv_sec =
end.tv_sec - begin.tv_sec;
253 diff.tv_nsec =
end.tv_nsec - begin.tv_nsec;
261 micro += 1000000ULL * diff.tv_sec;
262 micro += diff.tv_nsec / 1000ULL;
269 nano += 1000000000ULL * diff.tv_sec;
270 nano += diff.tv_nsec;
311 #if defined(_MSC_VER)
313 typedef struct __stat64
stat_t;
330 #if defined(_WIN32) || (PLATFORM_POSIX_VERSION < 200809L)
333 timebuf.modtime = statbuf->st_mtime;
337 timebuf[0].tv_nsec = UTIME_NOW;
338 timebuf[1].tv_sec = statbuf->st_mtime;
339 res += utimensat(AT_FDCWD,
filename, timebuf, 0);
357 #if defined(_MSC_VER)
358 r = _stat64(infilename, statbuf);
359 if (
r || !(statbuf->st_mode & S_IFREG))
return 0;
361 r =
stat(infilename, statbuf);
362 if (
r || !
S_ISREG(statbuf->st_mode))
return 0;
379 #if defined(_MSC_VER)
380 r = _stat64(infilename, &statbuf);
381 if (!
r && (statbuf.st_mode & _S_IFDIR))
return 1;
383 r =
stat(infilename, &statbuf);
384 if (!
r &&
S_ISDIR(statbuf.st_mode))
return 1;
403 return (
U64)statbuf.st_size;
414 return (
U64)statbuf.st_size;
422 for (
n=0;
n<nbFiles;
n++)
435 if (newptr)
return newptr;
442 # define UTIL_HAS_CREATEFILELIST
447 size_t dirLength, nbFiles = 0;
448 WIN32_FIND_DATAA cFile;
451 dirLength = strlen(dirName);
456 path[dirLength] =
'\\';
457 path[dirLength+1] =
'*';
458 path[dirLength+2] = 0;
460 hFile=FindFirstFileA(
path, &cFile);
462 fprintf(stderr,
"Cannot open directory '%s'\n", dirName);
469 int const fnameLength = (
int)strlen(cFile.cFileName);
470 path = (
char*)
malloc(dirLength + fnameLength + 2);
471 if (!
path) { FindClose(hFile);
return 0; }
473 path[dirLength] =
'\\';
474 memcpy(
path+dirLength+1, cFile.cFileName, fnameLength);
475 pathLength = dirLength+1+fnameLength;
476 path[pathLength] = 0;
477 if (cFile.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) {
478 if (strcmp (cFile.cFileName,
"..") == 0 ||
479 strcmp (cFile.cFileName,
".") == 0)
continue;
482 if (*bufStart ==
NULL) {
free(
path); FindClose(hFile);
return 0; }
484 else if ((cFile.dwFileAttributes & FILE_ATTRIBUTE_NORMAL) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_ARCHIVE) || (cFile.dwFileAttributes & FILE_ATTRIBUTE_COMPRESSED)) {
485 if (*bufStart + *
pos + pathLength >= *bufEnd) {
487 *bufStart = (
char*)
UTIL_realloc(*bufStart, newListSize);
488 *bufEnd = *bufStart + newListSize;
489 if (*bufStart ==
NULL) {
free(
path); FindClose(hFile);
return 0; }
491 if (*bufStart + *
pos + pathLength < *bufEnd) {
492 strncpy(*bufStart + *
pos,
path, *bufEnd - (*bufStart + *
pos));
493 *
pos += pathLength + 1;
498 }
while (FindNextFileA(hFile, &cFile));
505 #elif defined(__linux__) || (PLATFORM_POSIX_VERSION >= 200112L)
506 # define UTIL_HAS_CREATEFILELIST
514 int dirLength, nbFiles = 0;
516 if (!(dir = opendir(dirName))) {
517 fprintf(stderr,
"Cannot open directory '%s': %s\n", dirName, strerror(errno));
521 dirLength = (
int)strlen(dirName);
525 int fnameLength, pathLength;
526 if (strcmp (
entry->d_name,
"..") == 0 ||
527 strcmp (
entry->d_name,
".") == 0)
continue;
528 fnameLength = (
int)strlen(
entry->d_name);
529 path = (
char*)
malloc(dirLength + fnameLength + 2);
530 if (!
path) { closedir(dir);
return 0; }
532 path[dirLength] =
'/';
534 pathLength = dirLength+1+fnameLength;
535 path[pathLength] = 0;
539 if (*bufStart ==
NULL) {
free(
path); closedir(dir);
return 0; }
541 if (*bufStart + *
pos + pathLength >= *bufEnd) {
543 *bufStart = (
char*)
UTIL_realloc(*bufStart, newListSize);
544 *bufEnd = *bufStart + newListSize;
545 if (*bufStart ==
NULL) {
free(
path); closedir(dir);
return 0; }
547 if (*bufStart + *
pos + pathLength < *bufEnd) {
548 strncpy(*bufStart + *
pos,
path, *bufEnd - (*bufStart + *
pos));
549 *
pos += pathLength + 1;
558 fprintf(stderr,
"readdir(%s) error: %s\n", dirName, strerror(errno));
570 (void)bufStart; (void)bufEnd; (void)
pos;
571 fprintf(stderr,
"Directory %s ignored (compiled without _WIN32 or _POSIX_C_SOURCE)\n", dirName);
585 char** allocatedBuffer,
unsigned* allocatedNamesNb)
591 const char** fileTable;
595 for (
i=0,
pos=0, nbFiles=0;
i<inputNamesNb;
i++) {
597 size_t const len = strlen(inputNames[
i]) + 1;
598 if (
pos +
len >= bufSize) {
608 char* bufend =
buf + bufSize;
617 fileTable = (
const char**)
malloc(((
size_t)nbFiles+1) *
sizeof(
const char*));
620 for (
i=0,
pos=0;
i<nbFiles;
i++) {
622 pos += strlen(fileTable[
i]) + 1;
627 free((
void*)fileTable);
631 *allocatedBuffer =
buf;
632 *allocatedNamesNb = nbFiles;
641 if (allocatedBuffer)
free(allocatedBuffer);
642 if (filenameTable)
free((
void*)filenameTable);
646 #if defined (__cplusplus)
static static fork const void static count static fd const char const char static newpath const char static path const char path
static static fork const void static count static fd const char const char static newpath const char static path chmod
RZ_API void Ht_() free(HtName_(Ht) *ht)
#define INVALID_HANDLE_VALUE
memcpy(mem, inblock.get(), min(CONTAINING_RECORD(inblock.get(), MEMBLOCK, data) ->size, size))
void * realloc(void *ptr, size_t size)
void * malloc(size_t size)
static static fork const void static count static fd const char const char static newpath char char char static envp time
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds utime
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid void void static data static pause const char static mode static sync const char const char static newpath const char static pathname unsigned long static filedes void static end_data_segment static handler static getegid char static len static pgid const char static path static newfd static getpgrp static euid const sigset_t static mask const char static len const gid_t static list const char const char static newpath const char static library readdir
static const char struct stat static buf struct stat static buf static vhangup int struct rusage static rusage struct sysinfo static info unsigned static __unused struct utsname static buf const char static size const char static name static pid unsigned static persona static fsgid const void static flags const struct iovec static count static fd const void static len static munlockall struct sched_param static p static sched_yield static policy const struct timespec struct timespec static rem uid_t uid_t uid_t static suid struct pollfd unsigned static timeout chown
static void struct sockaddr socklen_t static fromlen static backlog static fork char char char static envp int struct rusage static rusage struct utsname static buf struct sembuf unsigned
UTIL_STATIC U64 UTIL_getSpanTimeMicro(UTIL_time_t clockStart, UTIL_time_t clockEnd)
UTIL_STATIC int UTIL_isRegFile(const char *infilename)
UTIL_STATIC U64 UTIL_getOpenFileSize(FILE *file)
UTIL_STATIC int UTIL_prepareFileList(const char *dirName, char **bufStart, size_t *pos, char **bufEnd)
UTIL_STATIC U32 UTIL_isDirectory(const char *infilename)
#define UTIL_STAT_MODE_ISREG(st_mode)
UTIL_STATIC void UTIL_freeFileList(const char **filenameTable, char *allocatedBuffer)
UTIL_STATIC UTIL_time_t UTIL_getTime(void)
UTIL_STATIC U64 UTIL_getTotalFileSize(const char **fileNamesTable, unsigned nbFiles)
UTIL_STATIC void * UTIL_realloc(void *ptr, size_t size)
UTIL_STATIC int UTIL_setFileStat(const char *filename, stat_t *statbuf)
UTIL_STATIC U64 UTIL_clockSpanMicro(UTIL_time_t clockStart)
UTIL_STATIC int UTIL_getFileStat(const char *infilename, stat_t *statbuf)
#define LIST_SIZE_INCREASE
UTIL_STATIC U64 UTIL_getSpanTimeNano(UTIL_time_t clockStart, UTIL_time_t clockEnd)
UTIL_STATIC const char ** UTIL_createFileList(const char **inputNames, unsigned inputNamesNb, char **allocatedBuffer, unsigned *allocatedNamesNb)
UTIL_STATIC U64 UTIL_getFileSize(const char *infilename)
UTIL_STATIC void UTIL_waitForNextTick(void)
UTIL_STATIC U64 UTIL_clockSpanNano(UTIL_time_t clockStart)
assert(limit<=UINT32_MAX/2)
static const z80_opcode fd[]