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)
28#if defined(__cplusplus) || defined(c_plusplus)
32extern MagickPrivate
char
33 **GetPathComponents(
const char *,
size_t *),
34 **ListFiles(
const char *,
const char *,
size_t *);
36extern MagickPrivate MagickBooleanType
37 GetExecutionPath(
char *,
const size_t),
38 ShredFile(
const char *);
40extern MagickPrivate ssize_t
41 GetMagickPageSize(
void);
43extern MagickPrivate
void
44 ChopPathComponents(
char *,
const size_t),
45 ExpandFilename(
char *);
47static inline int MagickReadDirectory(DIR *directory,
struct dirent *entry,
52 *result=readdir(directory);
60#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__)
61static inline wchar_t *create_wchar_path(
const char *utf8)
69 count=MultiByteToWideChar(CP_UTF8,0,utf8,-1,NULL,0);
70 if ((count > MAX_PATH) && (strncmp(utf8,
"\\\\?\\",4) != 0) &&
71 (NTLongPathsEnabled() == MagickFalse))
74 buffer[MagickPathExtent];
83 (void) FormatLocaleString(buffer,MagickPathExtent,
"\\\\?\\%s",utf8);
85 longPath=(
wchar_t *) NTAcquireQuantumMemory((
size_t) count,
87 if (longPath == (
wchar_t *) NULL)
88 return((
wchar_t *) NULL);
89 count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,longPath,count);
91 count=(int) GetShortPathNameW(longPath,shortPath,MAX_PATH);
92 longPath=(
wchar_t *) RelinquishMagickMemory(longPath);
93 if ((count < 5) || (count >= MAX_PATH))
94 return((
wchar_t *) NULL);
95 length=(size_t) count-3;
96 wide=(
wchar_t *) NTAcquireQuantumMemory(length,
sizeof(*wide));
97 wcscpy_s(wide,length,shortPath+4);
100 wide=(
wchar_t *) NTAcquireQuantumMemory((
size_t) count,
sizeof(*wide));
101 if ((wide != (
wchar_t *) NULL) &&
102 (MultiByteToWideChar(CP_UTF8,0,utf8,-1,wide,count) == 0))
103 wide=(
wchar_t *) RelinquishMagickMemory(wide);
107static inline wchar_t *create_wchar_mode(
const char *mode)
115 count=MultiByteToWideChar(CP_UTF8,0,mode,-1,NULL,0);
116 wide=(
wchar_t *) AcquireQuantumMemory((
size_t) count+1,
118 if (wide == (
wchar_t *) NULL)
119 return((
wchar_t *) NULL);
120 if (MultiByteToWideChar(CP_UTF8,0,mode,-1,wide,count) == 0)
122 wide=(
wchar_t *) RelinquishMagickMemory(wide);
123 return((
wchar_t *) NULL);
127 wide[count-1] = L
'N';
132static inline int access_utf8(
const char *path,
int mode)
134 if (path == (
const char *) NULL)
136#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
137 return(access(path,mode));
145 path_wide=create_wchar_path(path);
146 if (path_wide == (
wchar_t *) NULL)
148 status=_waccess(path_wide,mode);
149 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
154#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
155#define close_utf8 _close
157#define close_utf8 close
160static inline FILE *fopen_utf8(
const char *path,
const char *mode)
162#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
163 return(fopen(path,mode));
172 path_wide=create_wchar_path(path);
173 if (path_wide == (
wchar_t *) NULL)
174 return((FILE *) NULL);
175 mode_wide=create_wchar_mode(mode);
176 if (mode_wide == (
wchar_t *) NULL)
178 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
179 return((FILE *) NULL);
181 if (_wfopen_s(&file,path_wide,mode_wide) != 0)
183 mode_wide=(
wchar_t *) RelinquishMagickMemory(mode_wide);
184 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
189static inline void getcwd_utf8(
char *path,
size_t extent)
191#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
195 directory=getcwd(path,extent);
199 wide_path[MagickPathExtent];
201 (void) _wgetcwd(wide_path,MagickPathExtent-1);
202 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(
int) extent,NULL,NULL);
206#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
211static inline int open_utf8(
const char *path,
int flags,mode_t mode)
213#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
214 return(open(path,flags,mode));
223 path_wide=create_wchar_path(path);
224 if (path_wide == (
wchar_t *) NULL)
227 status=_wsopen_s(&file_handle,path_wide,flags | O_NOINHERIT,_SH_DENYNO,mode);
228 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
229 return(status == 0 ? file_handle : -1);
233static inline FILE *popen_utf8(
const char *command,
const char *type)
235#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
236 return(popen(command,type));
249 length=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,5);
252 length=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
255 command_wide=(
wchar_t *) AcquireQuantumMemory((
size_t) length,
256 sizeof(*command_wide));
257 if (command_wide == (
wchar_t *) NULL)
259 length=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,length);
261 file=_wpopen(command_wide,type_wide);
262 command_wide=(
wchar_t *) RelinquishMagickMemory(command_wide);
267static inline int remove_utf8(
const char *path)
269#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
270 return(unlink(path));
278 path_wide=create_wchar_path(path);
279 if (path_wide == (
wchar_t *) NULL)
281 status=_wremove(path_wide);
282 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
287static inline int rename_utf8(
const char *source,
const char *destination)
289#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
290 return(rename(source,destination));
299 source_wide=create_wchar_path(source);
300 if (source_wide == (
wchar_t *) NULL)
302 destination_wide=create_wchar_path(destination);
303 if (destination_wide == (
wchar_t *) NULL)
305 source_wide=(
wchar_t *) RelinquishMagickMemory(source_wide);
308 status=_wrename(source_wide,destination_wide);
309 destination_wide=(
wchar_t *) RelinquishMagickMemory(destination_wide);
310 source_wide=(
wchar_t *) RelinquishMagickMemory(source_wide);
315static inline int set_file_timestamp(
const char *path,
struct stat *attributes)
320#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
321#if defined(MAGICKCORE_HAVE_UTIMENSAT)
322#if defined(__APPLE__) || defined(__NetBSD__)
323#define st_atim st_atimespec
324#define st_ctim st_ctimespec
325#define st_mtim st_mtimespec
331 timestamp[0].tv_sec=attributes->st_atim.tv_sec;
332 timestamp[0].tv_nsec=attributes->st_atim.tv_nsec;
333 timestamp[1].tv_sec=attributes->st_mtim.tv_sec;
334 timestamp[1].tv_nsec=attributes->st_mtim.tv_nsec;
335 status=utimensat(AT_FDCWD,path,timestamp,0);
340 timestamp.actime=attributes->st_atime;
341 timestamp.modtime=attributes->st_mtime;
342 status=utime(path,×tamp);
352 path_wide=create_wchar_path(path);
353 if (path_wide == (WCHAR *) NULL)
355 handle=CreateFileW(path_wide,FILE_WRITE_ATTRIBUTES,FILE_SHARE_WRITE |
356 FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
357 if (handle != (HANDLE) NULL)
367 date_time.QuadPart=(ULONGLONG) (attributes->st_ctime*10000000LL)+
368 116444736000000000LL;
369 creation_time.dwLowDateTime=date_time.LowPart;
370 creation_time.dwHighDateTime=date_time.HighPart;
371 date_time.QuadPart=(ULONGLONG) (attributes->st_atime*10000000LL)+
372 116444736000000000LL;
373 last_access_time.dwLowDateTime=date_time.LowPart;
374 last_access_time.dwHighDateTime=date_time.HighPart;
375 date_time.QuadPart=(ULONGLONG) (attributes->st_mtime*10000000LL)+
376 116444736000000000LL;
377 last_write_time.dwLowDateTime=date_time.LowPart;
378 last_write_time.dwHighDateTime=date_time.HighPart;
379 status=SetFileTime(handle,&creation_time,&last_access_time,&last_write_time);
383 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
388static inline int stat_utf8(
const char *path,
struct stat *attributes)
390#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
391 return(stat(path,attributes));
399 path_wide=create_wchar_path(path);
400 if (path_wide == (WCHAR *) NULL)
402 status=_wstati64(path_wide,attributes);
403 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
408#if defined(__cplusplus) || defined(c_plusplus)