18#ifndef MAGICKCORE_UTILITY_PRIVATE_H
19#define MAGICKCORE_UTILITY_PRIVATE_H
21#include "MagickCore/memory_.h"
22#include "MagickCore/nt-base.h"
23#include "MagickCore/nt-base-private.h"
24#if defined(MAGICKCORE_HAVE_UTIME_H)
27#if defined(__MINGW32__)
31#if defined(__cplusplus) || defined(c_plusplus)
35extern MagickPrivate
char
36 **GetPathComponents(
const char *,
size_t *),
37 **ListFiles(
const char *,
const char *,
size_t *);
39extern MagickPrivate MagickBooleanType
40 GetExecutionPath(
char *,
const size_t),
41 ShredFile(
const char *);
43extern MagickPrivate ssize_t
44 GetMagickPageSize(
void);
46extern MagickPrivate
void
47 ChopPathComponents(
char *,
const size_t),
48 ExpandFilename(
char *);
50static inline int MagickReadDirectory(DIR *directory,
struct dirent *entry,
55 *result=readdir(directory);
63#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__)
64static inline wchar_t *create_wchar_path(
const char *utf8)
72 count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,NULL,0);
73 if ((count > MAX_PATH) && (strncmp(utf8,
"\\\\?\\",4) != 0) &&
74 (NTLongPathsEnabled() == MagickFalse))
77 buffer[MagickPathExtent];
86 (void) FormatLocaleString(buffer,MagickPathExtent,
"\\\\?\\%s",utf8);
88 longPath=(
wchar_t *) NTAcquireQuantumMemory((
size_t) count,
90 if (longPath == (
wchar_t *) NULL)
91 return((
wchar_t *) NULL);
92 count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,longPath,count);
94 count=(int) GetShortPathNameW(longPath,shortPath,MAX_PATH);
95 longPath=(
wchar_t *) RelinquishMagickMemory(longPath);
96 if ((count < 5) || (count >= MAX_PATH))
97 return((
wchar_t *) NULL);
98 length=(size_t) count-3;
99 wide=(
wchar_t *) NTAcquireQuantumMemory(length,
sizeof(*wide));
100 wcscpy_s(wide,length,shortPath+4);
103 wide=(
wchar_t *) NTAcquireQuantumMemory((
size_t) count,
sizeof(*wide));
104 if ((wide != (
wchar_t *) NULL) &&
105 (MultiByteToWideChar(CP_UTF8,0,utf8,-1,wide,count) == 0))
106 wide=(
wchar_t *) RelinquishMagickMemory(wide);
110static inline wchar_t *create_wchar_mode(
const char *mode)
118 count=MultiByteToWideChar(CP_UTF8,0,mode,-1,NULL,0);
119 wide=(
wchar_t *) AcquireQuantumMemory((
size_t) count+1,
121 if (wide == (
wchar_t *) NULL)
122 return((
wchar_t *) NULL);
123 if (MultiByteToWideChar(CP_UTF8,0,mode,-1,wide,count) == 0)
125 wide=(
wchar_t *) RelinquishMagickMemory(wide);
126 return((
wchar_t *) NULL);
130 wide[count-1] = L
'N';
135static inline int access_utf8(
const char *path,
int mode)
137 if (path == (
const char *) NULL)
139#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
140 return(access(path,mode));
148 path_wide=create_wchar_path(path);
149 if (path_wide == (
wchar_t *) NULL)
151 status=_waccess(path_wide,mode);
152 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
157#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__)
158#define close_utf8 _close
160#define close_utf8 close
163static inline FILE *fopen_utf8(
const char *path,
const char *mode)
165#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
166 return(fopen(path,mode));
175 path_wide=create_wchar_path(path);
176 if (path_wide == (
wchar_t *) NULL)
177 return((FILE *) NULL);
178 mode_wide=create_wchar_mode(mode);
179 if (mode_wide == (
wchar_t *) NULL)
181 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
182 return((FILE *) NULL);
184 if (_wfopen_s(&file,path_wide,mode_wide) != 0)
186 mode_wide=(
wchar_t *) RelinquishMagickMemory(mode_wide);
187 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
192static inline void getcwd_utf8(
char *path,
size_t extent)
194#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
198 directory=getcwd(path,extent);
202 wide_path[MagickPathExtent];
204 (void) _wgetcwd(wide_path,MagickPathExtent-1);
205 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(
int) extent,NULL,NULL);
209#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
214static inline int open_utf8(
const char *path,
int flags,mode_t mode)
216#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
217 return(open(path,flags,mode));
226 path_wide=create_wchar_path(path);
227 if (path_wide == (
wchar_t *) NULL)
230 status=_wsopen_s(&file_handle,path_wide,flags | O_NOINHERIT,_SH_DENYNO,mode);
231 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
232 return(status == 0 ? file_handle : -1);
236static inline FILE *popen_utf8(
const char *command,
const char *type)
238#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
239 return(popen(command,type));
252 length=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,5);
255 length=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
258 command_wide=(
wchar_t *) AcquireQuantumMemory((
size_t) length,
259 sizeof(*command_wide));
260 if (command_wide == (
wchar_t *) NULL)
262 length=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,length);
264 file=_wpopen(command_wide,type_wide);
265 command_wide=(
wchar_t *) RelinquishMagickMemory(command_wide);
270static inline int remove_utf8(
const char *path)
272#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
273 return(unlink(path));
281 path_wide=create_wchar_path(path);
282 if (path_wide == (
wchar_t *) NULL)
284 status=_wremove(path_wide);
285 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
290static inline int rename_utf8(
const char *source,
const char *destination)
292#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
293 return(rename(source,destination));
302 source_wide=create_wchar_path(source);
303 if (source_wide == (
wchar_t *) NULL)
305 destination_wide=create_wchar_path(destination);
306 if (destination_wide == (
wchar_t *) NULL)
308 source_wide=(
wchar_t *) RelinquishMagickMemory(source_wide);
311 status=_wrename(source_wide,destination_wide);
312 destination_wide=(
wchar_t *) RelinquishMagickMemory(destination_wide);
313 source_wide=(
wchar_t *) RelinquishMagickMemory(source_wide);
318static inline int set_file_timestamp(
const char *path,
struct stat *attributes)
323#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
324#if defined(MAGICKCORE_HAVE_UTIMENSAT)
325#if defined(__APPLE__) || defined(__NetBSD__)
326#define st_atim st_atimespec
327#define st_ctim st_ctimespec
328#define st_mtim st_mtimespec
334 timestamp[0].tv_sec=attributes->st_atim.tv_sec;
335 timestamp[0].tv_nsec=attributes->st_atim.tv_nsec;
336 timestamp[1].tv_sec=attributes->st_mtim.tv_sec;
337 timestamp[1].tv_nsec=attributes->st_mtim.tv_nsec;
338 status=utimensat(AT_FDCWD,path,timestamp,0);
343 timestamp.actime=attributes->st_atime;
344 timestamp.modtime=attributes->st_mtime;
345 status=utime(path,×tamp);
355 path_wide=create_wchar_path(path);
356 if (path_wide == (WCHAR *) NULL)
358 handle=CreateFileW(path_wide,FILE_WRITE_ATTRIBUTES,FILE_SHARE_WRITE |
359 FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
360 if (handle != (HANDLE) NULL)
370 date_time.QuadPart=(ULONGLONG) (attributes->st_ctime*10000000LL)+
371 116444736000000000LL;
372 creation_time.dwLowDateTime=date_time.LowPart;
373 creation_time.dwHighDateTime=date_time.HighPart;
374 date_time.QuadPart=(ULONGLONG) (attributes->st_atime*10000000LL)+
375 116444736000000000LL;
376 last_access_time.dwLowDateTime=date_time.LowPart;
377 last_access_time.dwHighDateTime=date_time.HighPart;
378 date_time.QuadPart=(ULONGLONG) (attributes->st_mtime*10000000LL)+
379 116444736000000000LL;
380 last_write_time.dwLowDateTime=date_time.LowPart;
381 last_write_time.dwHighDateTime=date_time.HighPart;
382 status=SetFileTime(handle,&creation_time,&last_access_time,&last_write_time);
386 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
391static inline int stat_utf8(
const char *path,
struct stat *attributes)
393#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
394 return(stat(path,attributes));
402 path_wide=create_wchar_path(path);
403 if (path_wide == (WCHAR *) NULL)
405 status=wstat(path_wide,attributes);
406 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
411#if defined(__cplusplus) || defined(c_plusplus)