diff options
Diffstat (limited to 'programs/util.h')
-rw-r--r-- | programs/util.h | 96 |
1 files changed, 74 insertions, 22 deletions
diff --git a/programs/util.h b/programs/util.h index 1dd515c..733c1ca 100644 --- a/programs/util.h +++ b/programs/util.h @@ -33,16 +33,21 @@ extern "C" { #include <stddef.h> /* size_t, ptrdiff_t */ #include <stdlib.h> /* malloc */ #include <string.h> /* strlen, strncpy */ -#include <stdio.h> /* fprintf */ +#include <stdio.h> /* fprintf, fileno */ #include <assert.h> #include <sys/types.h> /* stat, utime */ #include <sys/stat.h> /* stat */ -#if defined(_MSC_VER) +#if defined(_WIN32) # include <sys/utime.h> /* utime */ # include <io.h> /* _chmod */ #else # include <unistd.h> /* chown, stat */ +# if PLATFORM_POSIX_VERSION < 200809L # include <utime.h> /* utime */ +# else +# include <fcntl.h> /* AT_FDCWD */ +# include <sys/stat.h> /* for utimensat */ +# endif #endif #include <time.h> /* time */ #include <limits.h> /* INT_MAX */ @@ -117,6 +122,36 @@ extern "C" { #endif +/*-**************************************** +* stat() functions +******************************************/ +#if defined(_MSC_VER) +# define UTIL_TYPE_stat __stat64 +# define UTIL_stat _stat64 +# define UTIL_fstat _fstat64 +# define UTIL_STAT_MODE_ISREG(st_mode) ((st_mode) & S_IFREG) +#elif defined(__MINGW32__) && defined (__MSVCRT__) +# define UTIL_TYPE_stat _stati64 +# define UTIL_stat _stati64 +# define UTIL_fstat _fstati64 +# define UTIL_STAT_MODE_ISREG(st_mode) ((st_mode) & S_IFREG) +#else +# define UTIL_TYPE_stat stat +# define UTIL_stat stat +# define UTIL_fstat fstat +# define UTIL_STAT_MODE_ISREG(st_mode) (S_ISREG(st_mode)) +#endif + + +/*-**************************************** +* fileno() function +******************************************/ +#if defined(_MSC_VER) +# define UTIL_fileno _fileno +#else +# define UTIL_fileno fileno +#endif + /* ************************************* * Constants ***************************************/ @@ -287,14 +322,23 @@ UTIL_STATIC int UTIL_isRegFile(const char* infilename); UTIL_STATIC int UTIL_setFileStat(const char *filename, stat_t *statbuf) { int res = 0; - struct utimbuf timebuf; if (!UTIL_isRegFile(filename)) return -1; - timebuf.actime = time(NULL); - timebuf.modtime = statbuf->st_mtime; - res += utime(filename, &timebuf); /* set access and modification times */ + { +#if defined(_WIN32) || (PLATFORM_POSIX_VERSION < 200809L) + struct utimbuf timebuf; + timebuf.actime = time(NULL); + timebuf.modtime = statbuf->st_mtime; + res += utime(filename, &timebuf); /* set access and modification times */ +#else + struct timespec timebuf[2] = {}; + timebuf[0].tv_nsec = UTIME_NOW; + timebuf[1].tv_sec = statbuf->st_mtime; + res += utimensat(AT_FDCWD, filename, timebuf, 0); /* set access and modification times */ +#endif + } #if !defined(_WIN32) res += chown(filename, statbuf->st_uid, statbuf->st_gid); /* Copy ownership */ @@ -343,22 +387,30 @@ UTIL_STATIC U32 UTIL_isDirectory(const char* infilename) } +UTIL_STATIC U64 UTIL_getOpenFileSize(FILE* file) +{ + int r; + int fd; + struct UTIL_TYPE_stat statbuf; + + fd = UTIL_fileno(file); + if (fd < 0) { + perror("fileno"); + exit(1); + } + r = UTIL_fstat(fd, &statbuf); + if (r || !UTIL_STAT_MODE_ISREG(statbuf.st_mode)) return 0; /* No good... */ + return (U64)statbuf.st_size; +} + + UTIL_STATIC U64 UTIL_getFileSize(const char* infilename) { int r; -#if defined(_MSC_VER) - struct __stat64 statbuf; - r = _stat64(infilename, &statbuf); - if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */ -#elif defined(__MINGW32__) && defined (__MSVCRT__) - struct _stati64 statbuf; - r = _stati64(infilename, &statbuf); - if (r || !(statbuf.st_mode & S_IFREG)) return 0; /* No good... */ -#else - struct stat statbuf; - r = stat(infilename, &statbuf); - if (r || !S_ISREG(statbuf.st_mode)) return 0; /* No good... */ -#endif + struct UTIL_TYPE_stat statbuf; + + r = UTIL_stat(infilename, &statbuf); + if (r || !UTIL_STAT_MODE_ISREG(statbuf.st_mode)) return 0; /* No good... */ return (U64)statbuf.st_size; } @@ -542,15 +594,15 @@ UTIL_createFileList(const char** inputNames, unsigned inputNamesNb, for (i=0, pos=0, nbFiles=0; i<inputNamesNb; i++) { if (!UTIL_isDirectory(inputNames[i])) { - size_t const len = strlen(inputNames[i]); + size_t const len = strlen(inputNames[i]) + 1; /* include nul char */ if (pos + len >= bufSize) { while (pos + len >= bufSize) bufSize += LIST_SIZE_INCREASE; buf = (char*)UTIL_realloc(buf, bufSize); if (!buf) return NULL; } assert(pos + len < bufSize); - strncpy(buf + pos, inputNames[i], bufSize - pos); - pos += len + 1; + memcpy(buf + pos, inputNames[i], len); + pos += len; nbFiles++; } else { char* bufend = buf + bufSize; |