18#ifndef MAGICKCORE_UTILITY_PRIVATE_H
19#define MAGICKCORE_UTILITY_PRIVATE_H
21#include "magick/memory_.h"
22#include "magick/nt-base.h"
23#include "magick/nt-base-private.h"
24#if defined(MAGICKCORE_HAVE_UTIME_H)
28#if defined(__cplusplus) || defined(c_plusplus)
32extern MagickPrivate MagickBooleanType
33 ShredFile(
const char *);
35static inline int MagickReadDirectory(
DIR *directory,
struct dirent *entry,
40 *result=readdir(directory);
48#if defined(MAGICKCORE_WINDOWS_SUPPORT)
49static inline wchar_t *create_wchar_path(
const char *utf8)
57 count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,NULL,0);
58 if ((count > MAX_PATH) && (NTLongPathsEnabled() == MagickFalse))
61 buffer[MaxTextExtent];
67 (void) FormatLocaleString(buffer,MaxTextExtent,
"\\\\?\\%s",utf8);
69 longPath=(
wchar_t *) NTAcquireQuantumMemory((
size_t) count,
71 if (longPath == (
wchar_t *) NULL)
72 return((
wchar_t *) NULL);
73 count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,longPath,count);
75 count=(int) GetShortPathNameW(longPath,shortPath,MAX_PATH);
76 longPath=(
wchar_t *) RelinquishMagickMemory(longPath);
77 if ((count < 5) || (count >= MAX_PATH))
78 return((
wchar_t *) NULL);
79 wideChar=(
wchar_t *) NTAcquireQuantumMemory((
size_t) count-3,
81 wcscpy(wideChar,shortPath+4);
84 wideChar=(
wchar_t *) NTAcquireQuantumMemory(count,
sizeof(*wideChar));
85 if (wideChar == (
wchar_t *) NULL)
86 return((
wchar_t *) NULL);
87 count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,wideChar,count);
90 wideChar=(
wchar_t *) RelinquishMagickMemory(wideChar);
91 return((
wchar_t *) NULL);
97static inline int access_utf8(
const char *path,
int mode)
99 if (path == (
const char *) NULL)
101#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
102 return(access(path,mode));
110 path_wide=create_wchar_path(path);
111 if (path_wide == (
wchar_t *) NULL)
113 status=_waccess(path_wide,mode);
114 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
119static inline FILE *fopen_utf8(
const char *path,
const char *mode)
121#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
122 return(fopen(path,mode));
131 path_wide=create_wchar_path(path);
132 if (path_wide == (
wchar_t *) NULL)
133 return((FILE *) NULL);
134 mode_wide=create_wchar_path(mode);
135 if (mode_wide == (
wchar_t *) NULL)
137 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
138 return((FILE *) NULL);
140 file=_wfopen(path_wide,mode_wide);
141 mode_wide=(
wchar_t *) RelinquishMagickMemory(mode_wide);
142 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
147static inline void getcwd_utf8(
char *path,
size_t extent)
149#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
153 directory=getcwd(path,extent);
157 wide_path[MaxTextExtent];
159 (void) _wgetcwd(wide_path,MaxTextExtent-1);
160 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(
int) extent,NULL,NULL);
164#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
169static inline int open_utf8(
const char *path,
int flags,mode_t mode)
171#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
172 return(open(path,flags,mode));
180 path_wide=create_wchar_path(path);
181 if (path_wide == (
wchar_t *) NULL)
183 status=_wopen(path_wide,flags,mode);
184 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
189static inline FILE *popen_utf8(
const char *command,
const char *type)
191#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
192 return(popen(command,type));
205 length=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,5);
208 length=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
211 command_wide=(
wchar_t *) AcquireQuantumMemory((
size_t) length,
212 sizeof(*command_wide));
213 if (command_wide == (
wchar_t *) NULL)
215 length=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,length);
217 file=_wpopen(command_wide,type_wide);
218 command_wide=(
wchar_t *) RelinquishMagickMemory(command_wide);
223static inline int remove_utf8(
const char *path)
225#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
226 return(unlink(path));
234 path_wide=create_wchar_path(path);
235 if (path_wide == (
wchar_t *) NULL)
237 status=_wremove(path_wide);
238 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
243static inline int rename_utf8(
const char *source,
const char *destination)
245#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
246 return(rename(source,destination));
255 source_wide=create_wchar_path(source);
256 if (source_wide == (
wchar_t *) NULL)
258 destination_wide=create_wchar_path(destination);
259 if (destination_wide == (
wchar_t *) NULL)
261 source_wide=(
wchar_t *) RelinquishMagickMemory(source_wide);
264 status=_wrename(source_wide,destination_wide);
265 destination_wide=(
wchar_t *) RelinquishMagickMemory(destination_wide);
266 source_wide=(
wchar_t *) RelinquishMagickMemory(source_wide);
271static inline int set_file_timestamp(
const char *path,
struct stat *attributes)
276#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
277#if defined(MAGICKCORE_HAVE_UTIMENSAT)
278#if defined(__APPLE__) || defined(__NetBSD__)
279#define st_atim st_atimespec
280#define st_ctim st_ctimespec
281#define st_mtim st_mtimespec
287 timestamp[0].tv_sec=attributes->st_atim.tv_sec;
288 timestamp[0].tv_nsec=attributes->st_atim.tv_nsec;
289 timestamp[1].tv_sec=attributes->st_mtim.tv_sec;
290 timestamp[1].tv_nsec=attributes->st_mtim.tv_nsec;
291 status=utimensat(AT_FDCWD,path,timestamp,0);
296 timestamp.actime=attributes->st_atime;
297 timestamp.modtime=attributes->st_mtime;
298 status=utime(path,×tamp);
308 path_wide=create_wchar_path(path);
309 if (path_wide == (WCHAR *) NULL)
311 handle=CreateFileW(path_wide,FILE_WRITE_ATTRIBUTES,FILE_SHARE_WRITE |
312 FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
313 if (handle != (HANDLE) NULL)
323 date_time.QuadPart=(ULONGLONG) (attributes->st_ctime*10000000LL)+
324 116444736000000000LL;
325 creation_time.dwLowDateTime=date_time.LowPart;
326 creation_time.dwHighDateTime=date_time.HighPart;
327 date_time.QuadPart=(ULONGLONG) (attributes->st_atime*10000000LL)+
328 116444736000000000LL;
329 last_access_time.dwLowDateTime=date_time.LowPart;
330 last_access_time.dwHighDateTime=date_time.HighPart;
331 date_time.QuadPart=(ULONGLONG) (attributes->st_mtime*10000000LL)+
332 116444736000000000LL;
333 last_write_time.dwLowDateTime=date_time.LowPart;
334 last_write_time.dwHighDateTime=date_time.HighPart;
335 status=SetFileTime(handle,&creation_time,&last_access_time,
340 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
345static inline int stat_utf8(
const char *path,
struct stat *attributes)
347#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
348 return(stat(path,attributes));
356 path_wide=create_wchar_path(path);
357 if (path_wide == (WCHAR *) NULL)
359 status=_wstati64(path_wide,attributes);
360 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
365#if defined(__cplusplus) || defined(c_plusplus)