time.c 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  1. // Copyright (C) 2004-2025 Artifex Software, Inc.
  2. //
  3. // This file is part of MuPDF.
  4. //
  5. // MuPDF is free software: you can redistribute it and/or modify it under the
  6. // terms of the GNU Affero General Public License as published by the Free
  7. // Software Foundation, either version 3 of the License, or (at your option)
  8. // any later version.
  9. //
  10. // MuPDF is distributed in the hope that it will be useful, but WITHOUT ANY
  11. // WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
  12. // FOR A PARTICULAR PURPOSE. See the GNU Affero General Public License for more
  13. // details.
  14. //
  15. // You should have received a copy of the GNU Affero General Public License
  16. // along with MuPDF. If not, see <https://www.gnu.org/licenses/agpl-3.0.en.html>
  17. //
  18. // Alternative licensing terms are available from the licensor.
  19. // For commercial licensing, see <https://www.artifex.com/> or contact
  20. // Artifex Software, Inc., 39 Mesa Street, Suite 108A, San Francisco,
  21. // CA 94129, USA, for further information.
  22. #include "mupdf/fitz.h"
  23. #include <sys/stat.h>
  24. #ifdef _WIN32
  25. #include <stdio.h>
  26. #include <errno.h>
  27. #include <time.h>
  28. #include <windows.h>
  29. #include <direct.h> /* for mkdir */
  30. #ifndef _WINRT
  31. #define DELTA_EPOCH_IN_MICROSECS 11644473600000000ULL
  32. int gettimeofday(struct timeval *tv, struct timezone *tz)
  33. {
  34. FILETIME ft;
  35. unsigned __int64 tmpres = 0;
  36. if (tv)
  37. {
  38. GetSystemTimeAsFileTime(&ft);
  39. tmpres |= ft.dwHighDateTime;
  40. tmpres <<= 32;
  41. tmpres |= ft.dwLowDateTime;
  42. tmpres /= 10; /*convert into microseconds*/
  43. /*converting file time to unix epoch*/
  44. tmpres -= DELTA_EPOCH_IN_MICROSECS;
  45. tv->tv_sec = (long)(tmpres / 1000000UL);
  46. tv->tv_usec = (long)(tmpres % 1000000UL);
  47. }
  48. return 0;
  49. }
  50. #endif /* !_WINRT */
  51. static char *
  52. utf8_from_wchar(const wchar_t *s)
  53. {
  54. const wchar_t *src = s;
  55. char *d;
  56. char *dst;
  57. int len = 1;
  58. while (*src)
  59. {
  60. len += fz_runelen(*src++);
  61. }
  62. d = Memento_label(malloc(len), "utf8_from_wchar");
  63. if (d != NULL)
  64. {
  65. dst = d;
  66. src = s;
  67. while (*src)
  68. {
  69. dst += fz_runetochar(dst, *src++);
  70. }
  71. *dst = 0;
  72. }
  73. return d;
  74. }
  75. static wchar_t *
  76. wchar_from_utf8(const char *s)
  77. {
  78. wchar_t *d, *r;
  79. int c;
  80. /* This allocation is larger than we need, but it's guaranteed
  81. * to be safe. */
  82. r = d = malloc((strlen(s) + 1) * sizeof(wchar_t));
  83. if (!r)
  84. return NULL;
  85. while (*s) {
  86. s += fz_chartorune(&c, s);
  87. /* Truncating c to a wchar_t can be problematic if c
  88. * is 0x10000. */
  89. if (c >= 0x10000)
  90. {
  91. c -= 0x10000;
  92. *d++ = 0xd800 + (c>>10);
  93. c = 0xdc00 + (c&1023);
  94. }
  95. *d++ = c;
  96. }
  97. *d = 0;
  98. return r;
  99. }
  100. void *
  101. fz_fopen_utf8(const char *name, const char *mode)
  102. {
  103. wchar_t *wname, *wmode;
  104. FILE *file;
  105. wname = wchar_from_utf8(name);
  106. if (wname == NULL)
  107. {
  108. return NULL;
  109. }
  110. wmode = wchar_from_utf8(mode);
  111. if (wmode == NULL)
  112. {
  113. free(wname);
  114. return NULL;
  115. }
  116. file = _wfopen(wname, wmode);
  117. free(wname);
  118. free(wmode);
  119. return file;
  120. }
  121. int
  122. fz_remove_utf8(const char *name)
  123. {
  124. wchar_t *wname;
  125. int n;
  126. wname = wchar_from_utf8(name);
  127. if (wname == NULL)
  128. {
  129. errno = ENOMEM;
  130. return -1;
  131. }
  132. n = _wremove(wname);
  133. free(wname);
  134. return n;
  135. }
  136. char **
  137. fz_argv_from_wargv(int argc, wchar_t **wargv)
  138. {
  139. char **argv;
  140. int i;
  141. argv = Memento_label(calloc(argc, sizeof(char *)), "fz_argv");
  142. if (argv == NULL)
  143. {
  144. fprintf(stderr, "Out of memory while processing command line args!\n");
  145. exit(1);
  146. }
  147. for (i = 0; i < argc; i++)
  148. {
  149. argv[i] = Memento_label(utf8_from_wchar(wargv[i]), "fz_arg");
  150. if (argv[i] == NULL)
  151. {
  152. fprintf(stderr, "Out of memory while processing command line args!\n");
  153. exit(1);
  154. }
  155. }
  156. return argv;
  157. }
  158. void
  159. fz_free_argv(int argc, char **argv)
  160. {
  161. int i;
  162. for (i = 0; i < argc; i++)
  163. free(argv[i]);
  164. free(argv);
  165. }
  166. int64_t
  167. fz_stat_ctime(const char *path)
  168. {
  169. struct _stat info;
  170. wchar_t *wpath;
  171. wpath = wchar_from_utf8(path);
  172. if (wpath == NULL)
  173. return 0;
  174. if (_wstat(wpath, &info) < 0) {
  175. free(wpath);
  176. return 0;
  177. }
  178. free(wpath);
  179. return info.st_ctime;
  180. }
  181. int64_t
  182. fz_stat_mtime(const char *path)
  183. {
  184. struct _stat info;
  185. wchar_t *wpath;
  186. wpath = wchar_from_utf8(path);
  187. if (wpath == NULL)
  188. return 0;
  189. if (_wstat(wpath, &info) < 0) {
  190. free(wpath);
  191. return 0;
  192. }
  193. free(wpath);
  194. return info.st_mtime;
  195. }
  196. int
  197. fz_mkdir(char *path)
  198. {
  199. int ret;
  200. wchar_t *wpath = wchar_from_utf8(path);
  201. if (wpath == NULL)
  202. return -1;
  203. ret = _wmkdir(wpath);
  204. free(wpath);
  205. return ret;
  206. }
  207. #else
  208. int64_t
  209. fz_stat_ctime(const char *path)
  210. {
  211. struct stat info;
  212. if (stat(path, &info) < 0)
  213. return 0;
  214. return info.st_ctime;
  215. }
  216. int64_t
  217. fz_stat_mtime(const char *path)
  218. {
  219. struct stat info;
  220. if (stat(path, &info) < 0)
  221. return 0;
  222. return info.st_mtime;
  223. }
  224. int
  225. fz_mkdir(char *path)
  226. {
  227. return mkdir(path, S_IRWXU | S_IRWXG | S_IRWXO);
  228. }
  229. #endif /* _WIN32 */