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)
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];
80 (void) FormatLocaleString(buffer,MagickPathExtent,
"\\\\?\\%s",utf8);
82 longPath=(
wchar_t *) NTAcquireQuantumMemory((
size_t) count,
84 if (longPath == (
wchar_t *) NULL)
85 return((
wchar_t *) NULL);
86 count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,longPath,count);
88 count=(int) GetShortPathNameW(longPath,shortPath,MAX_PATH);
89 longPath=(
wchar_t *) RelinquishMagickMemory(longPath);
90 if ((count < 5) || (count >= MAX_PATH))
91 return((
wchar_t *) NULL);
92 wide=(
wchar_t *) NTAcquireQuantumMemory((
size_t) count-3,
sizeof(*wide));
93 wcscpy(wide,shortPath+4);
96 wide=(
wchar_t *) NTAcquireQuantumMemory((
size_t) count,
sizeof(*wide));
97 if ((wide != (
wchar_t *) NULL) &&
98 (MultiByteToWideChar(CP_UTF8,0,utf8,-1,wide,count) == 0))
99 wide=(
wchar_t *) RelinquishMagickMemory(wide);
103static inline wchar_t *create_wchar_mode(
const char *mode)
111 count=MultiByteToWideChar(CP_UTF8,0,mode,-1,NULL,0);
112 wide=(
wchar_t *) AcquireQuantumMemory((
size_t) count+1,
114 if (wide == (
wchar_t *) NULL)
115 return((
wchar_t *) NULL);
116 if (MultiByteToWideChar(CP_UTF8,0,mode,-1,wide,count) == 0)
118 wide=(
wchar_t *) RelinquishMagickMemory(wide);
119 return((
wchar_t *) NULL);
123 wide[count-1] = L
'N';
128static inline int access_utf8(
const char *path,
int mode)
130 if (path == (
const char *) NULL)
132#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
133 return(access(path,mode));
141 path_wide=create_wchar_path(path);
142 if (path_wide == (
wchar_t *) NULL)
144 status=_waccess(path_wide,mode);
145 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
150static inline FILE *fopen_utf8(
const char *path,
const char *mode)
152#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
153 return(fopen(path,mode));
162 path_wide=create_wchar_path(path);
163 if (path_wide == (
wchar_t *) NULL)
164 return((FILE *) NULL);
165 mode_wide=create_wchar_mode(mode);
166 if (mode_wide == (
wchar_t *) NULL)
168 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
169 return((FILE *) NULL);
171 file=_wfopen(path_wide,mode_wide);
172 mode_wide=(
wchar_t *) RelinquishMagickMemory(mode_wide);
173 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
178static inline void getcwd_utf8(
char *path,
size_t extent)
180#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
184 directory=getcwd(path,extent);
188 wide_path[MagickPathExtent];
190 (void) _wgetcwd(wide_path,MagickPathExtent-1);
191 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(
int) extent,NULL,NULL);
195#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
200static inline int open_utf8(
const char *path,
int flags,mode_t mode)
202#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
203 return(open(path,flags,mode));
211 path_wide=create_wchar_path(path);
212 if (path_wide == (
wchar_t *) NULL)
215 status=_wopen(path_wide,flags | O_NOINHERIT,mode);
216 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
221static inline FILE *popen_utf8(
const char *command,
const char *type)
223#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
224 return(popen(command,type));
237 length=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,5);
240 length=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
243 command_wide=(
wchar_t *) AcquireQuantumMemory((
size_t) length,
244 sizeof(*command_wide));
245 if (command_wide == (
wchar_t *) NULL)
247 length=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,length);
249 file=_wpopen(command_wide,type_wide);
250 command_wide=(
wchar_t *) RelinquishMagickMemory(command_wide);
255static inline int remove_utf8(
const char *path)
257#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
258 return(unlink(path));
266 path_wide=create_wchar_path(path);
267 if (path_wide == (
wchar_t *) NULL)
269 status=_wremove(path_wide);
270 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
275static inline int rename_utf8(
const char *source,
const char *destination)
277#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
278 return(rename(source,destination));
287 source_wide=create_wchar_path(source);
288 if (source_wide == (
wchar_t *) NULL)
290 destination_wide=create_wchar_path(destination);
291 if (destination_wide == (
wchar_t *) NULL)
293 source_wide=(
wchar_t *) RelinquishMagickMemory(source_wide);
296 status=_wrename(source_wide,destination_wide);
297 destination_wide=(
wchar_t *) RelinquishMagickMemory(destination_wide);
298 source_wide=(
wchar_t *) RelinquishMagickMemory(source_wide);
303static inline int set_file_timestamp(
const char *path,
struct stat *attributes)
308#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
309#if defined(MAGICKCORE_HAVE_UTIMENSAT)
310#if defined(__APPLE__) || defined(__NetBSD__)
311#define st_atim st_atimespec
312#define st_ctim st_ctimespec
313#define st_mtim st_mtimespec
319 timestamp[0].tv_sec=attributes->st_atim.tv_sec;
320 timestamp[0].tv_nsec=attributes->st_atim.tv_nsec;
321 timestamp[1].tv_sec=attributes->st_mtim.tv_sec;
322 timestamp[1].tv_nsec=attributes->st_mtim.tv_nsec;
323 status=utimensat(AT_FDCWD,path,timestamp,0);
328 timestamp.actime=attributes->st_atime;
329 timestamp.modtime=attributes->st_mtime;
330 status=utime(path,×tamp);
340 path_wide=create_wchar_path(path);
341 if (path_wide == (WCHAR *) NULL)
343 handle=CreateFileW(path_wide,FILE_WRITE_ATTRIBUTES,FILE_SHARE_WRITE |
344 FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
345 if (handle != (HANDLE) NULL)
355 date_time.QuadPart=(ULONGLONG) (attributes->st_ctime*10000000LL)+
356 116444736000000000LL;
357 creation_time.dwLowDateTime=date_time.LowPart;
358 creation_time.dwHighDateTime=date_time.HighPart;
359 date_time.QuadPart=(ULONGLONG) (attributes->st_atime*10000000LL)+
360 116444736000000000LL;
361 last_access_time.dwLowDateTime=date_time.LowPart;
362 last_access_time.dwHighDateTime=date_time.HighPart;
363 date_time.QuadPart=(ULONGLONG) (attributes->st_mtime*10000000LL)+
364 116444736000000000LL;
365 last_write_time.dwLowDateTime=date_time.LowPart;
366 last_write_time.dwHighDateTime=date_time.HighPart;
367 status=SetFileTime(handle,&creation_time,&last_access_time,&last_write_time);
371 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
376static inline int stat_utf8(
const char *path,
struct stat *attributes)
378#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
379 return(stat(path,attributes));
387 path_wide=create_wchar_path(path);
388 if (path_wide == (WCHAR *) NULL)
390 status=_wstati64(path_wide,attributes);
391 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
396#if defined(__cplusplus) || defined(c_plusplus)