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(count,
sizeof(*longPath));
83 if (longPath == (
wchar_t *) NULL)
84 return((
wchar_t *) NULL);
85 count=MultiByteToWideChar(CP_UTF8,0,buffer,-1,longPath,count);
87 count=GetShortPathNameW(longPath,shortPath,MAX_PATH);
88 longPath=(
wchar_t *) RelinquishMagickMemory(longPath);
89 if ((count < 5) || (count >= MAX_PATH))
90 return((
wchar_t *) NULL);
91 wide=(
wchar_t *) NTAcquireQuantumMemory((
size_t) count-3,
sizeof(*wide));
92 wcscpy(wide,shortPath+4);
95 wide=(
wchar_t *) NTAcquireQuantumMemory(count,
sizeof(*wide));
96 if ((wide != (
wchar_t *) NULL) &&
97 (MultiByteToWideChar(CP_UTF8,0,utf8,-1,wide,count) == 0))
98 wide=(
wchar_t *) RelinquishMagickMemory(wide);
102static inline wchar_t *create_wchar_mode(
const char *mode)
110 count=MultiByteToWideChar(CP_UTF8,0,mode,-1,NULL,0);
111 wide=(
wchar_t *) AcquireQuantumMemory((
size_t) count+1,
113 if (wide == (
wchar_t *) NULL)
114 return((
wchar_t *) NULL);
115 if (MultiByteToWideChar(CP_UTF8,0,mode,-1,wide,count) == 0)
117 wide=(
wchar_t *) RelinquishMagickMemory(wide);
118 return((
wchar_t *) NULL);
122 wide[count-1] = L
'N';
127static inline int access_utf8(
const char *path,
int mode)
129 if (path == (
const char *) NULL)
131#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
132 return(access(path,mode));
140 path_wide=create_wchar_path(path);
141 if (path_wide == (
wchar_t *) NULL)
143 status=_waccess(path_wide,mode);
144 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
149static inline FILE *fopen_utf8(
const char *path,
const char *mode)
151#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
152 return(fopen(path,mode));
161 path_wide=create_wchar_path(path);
162 if (path_wide == (
wchar_t *) NULL)
163 return((FILE *) NULL);
164 mode_wide=create_wchar_mode(mode);
165 if (mode_wide == (
wchar_t *) NULL)
167 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
168 return((FILE *) NULL);
170 file=_wfopen(path_wide,mode_wide);
171 mode_wide=(
wchar_t *) RelinquishMagickMemory(mode_wide);
172 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
177static inline void getcwd_utf8(
char *path,
size_t extent)
179#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
183 directory=getcwd(path,extent);
187 wide_path[MagickPathExtent];
189 (void) _wgetcwd(wide_path,MagickPathExtent-1);
190 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(
int) extent,NULL,NULL);
194#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
199static inline int open_utf8(
const char *path,
int flags,mode_t mode)
201#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
202 return(open(path,flags,mode));
210 path_wide=create_wchar_path(path);
211 if (path_wide == (
wchar_t *) NULL)
214 status=_wopen(path_wide,flags | O_NOINHERIT,mode);
215 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
220static inline FILE *popen_utf8(
const char *command,
const char *type)
222#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
223 return(popen(command,type));
236 length=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,5);
239 length=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
242 command_wide=(
wchar_t *) AcquireQuantumMemory(length,
sizeof(*command_wide));
243 if (command_wide == (
wchar_t *) NULL)
245 length=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,length);
247 file=_wpopen(command_wide,type_wide);
248 command_wide=(
wchar_t *) RelinquishMagickMemory(command_wide);
253static inline int remove_utf8(
const char *path)
255#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
256 return(unlink(path));
264 path_wide=create_wchar_path(path);
265 if (path_wide == (
wchar_t *) NULL)
267 status=_wremove(path_wide);
268 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
273static inline int rename_utf8(
const char *source,
const char *destination)
275#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
276 return(rename(source,destination));
285 source_wide=create_wchar_path(source);
286 if (source_wide == (
wchar_t *) NULL)
288 destination_wide=create_wchar_path(destination);
289 if (destination_wide == (
wchar_t *) NULL)
291 source_wide=(
wchar_t *) RelinquishMagickMemory(source_wide);
294 status=_wrename(source_wide,destination_wide);
295 destination_wide=(
wchar_t *) RelinquishMagickMemory(destination_wide);
296 source_wide=(
wchar_t *) RelinquishMagickMemory(source_wide);
301static inline int set_file_timestamp(
const char *path,
struct stat *attributes)
306#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
307#if defined(MAGICKCORE_HAVE_UTIMENSAT)
308#if defined(__APPLE__) || defined(__NetBSD__)
309#define st_atim st_atimespec
310#define st_ctim st_ctimespec
311#define st_mtim st_mtimespec
317 timestamp[0].tv_sec=attributes->st_atim.tv_sec;
318 timestamp[0].tv_nsec=attributes->st_atim.tv_nsec;
319 timestamp[1].tv_sec=attributes->st_mtim.tv_sec;
320 timestamp[1].tv_nsec=attributes->st_mtim.tv_nsec;
321 status=utimensat(AT_FDCWD,path,timestamp,0);
326 timestamp.actime=attributes->st_atime;
327 timestamp.modtime=attributes->st_mtime;
328 status=utime(path,×tamp);
338 path_wide=create_wchar_path(path);
339 if (path_wide == (WCHAR *) NULL)
341 handle=CreateFileW(path_wide,FILE_WRITE_ATTRIBUTES,FILE_SHARE_WRITE |
342 FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
343 if (handle != (HANDLE) NULL)
353 date_time.QuadPart=(attributes->st_ctime*10000000LL)+116444736000000000LL;
354 creation_time.dwLowDateTime=date_time.LowPart;
355 creation_time.dwHighDateTime=date_time.HighPart;
356 date_time.QuadPart=(attributes->st_atime*10000000LL)+116444736000000000LL;
357 last_access_time.dwLowDateTime=date_time.LowPart;
358 last_access_time.dwHighDateTime=date_time.HighPart;
359 date_time.QuadPart=(attributes->st_mtime*10000000LL)+116444736000000000LL;
360 last_write_time.dwLowDateTime=date_time.LowPart;
361 last_write_time.dwHighDateTime=date_time.HighPart;
362 status=SetFileTime(handle,&creation_time,&last_access_time,&last_write_time);
366 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
371static inline int stat_utf8(
const char *path,
struct stat *attributes)
373#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
374 return(stat(path,attributes));
382 path_wide=create_wchar_path(path);
383 if (path_wide == (WCHAR *) NULL)
385 status=wstat(path_wide,attributes);
386 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
391#if defined(__cplusplus) || defined(c_plusplus)