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,
48 struct dirent **result)
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);
150#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
151#define close_utf8 _close
153#define close_utf8 close
156static inline FILE *fopen_utf8(
const char *path,
const char *mode)
158#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
159 return(fopen(path,mode));
168 path_wide=create_wchar_path(path);
169 if (path_wide == (
wchar_t *) NULL)
170 return((FILE *) NULL);
171 mode_wide=create_wchar_mode(mode);
172 if (mode_wide == (
wchar_t *) NULL)
174 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
175 return((FILE *) NULL);
177 file=_wfopen(path_wide,mode_wide);
178 mode_wide=(
wchar_t *) RelinquishMagickMemory(mode_wide);
179 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
184static inline void getcwd_utf8(
char *path,
size_t extent)
186#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
190 directory=getcwd(path,extent);
194 wide_path[MagickPathExtent];
196 (void) _wgetcwd(wide_path,MagickPathExtent-1);
197 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(
int) extent,NULL,NULL);
201#if defined(MAGICKCORE_WINDOWS_SUPPORT) && !defined(__CYGWIN__) && !defined(__MINGW32__)
206static inline int open_utf8(
const char *path,
int flags,mode_t mode)
208#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
209 return(open(path,flags,mode));
217 path_wide=create_wchar_path(path);
218 if (path_wide == (
wchar_t *) NULL)
221 status=_wopen(path_wide,flags | O_NOINHERIT,mode);
222 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
227static inline FILE *popen_utf8(
const char *command,
const char *type)
229#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
230 return(popen(command,type));
243 length=MultiByteToWideChar(CP_UTF8,0,type,-1,type_wide,5);
246 length=MultiByteToWideChar(CP_UTF8,0,command,-1,NULL,0);
249 command_wide=(
wchar_t *) AcquireQuantumMemory((
size_t) length,
250 sizeof(*command_wide));
251 if (command_wide == (
wchar_t *) NULL)
253 length=MultiByteToWideChar(CP_UTF8,0,command,-1,command_wide,length);
255 file=_wpopen(command_wide,type_wide);
256 command_wide=(
wchar_t *) RelinquishMagickMemory(command_wide);
261static inline char *realpath_utf8(
const char *path)
263#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
264#if defined(MAGICKCORE_HAVE_REALPATH)
265 return(realpath(path,(
char *) NULL));
267 return(AcquireString(path));
292 if (path == (
const char *) NULL)
293 return((
char *) NULL);
294 length=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
296 return((
char *) NULL);
297 wide_path=(
wchar_t *) AcquireQuantumMemory(length,
sizeof(
wchar_t));
298 if (wide_path == (
wchar_t *) NULL)
299 return((
char *) NULL);
300 MultiByteToWideChar(CP_UTF8,0,path,-1,wide_path,length);
304 full_path_length=GetFullPathNameW(wide_path,0,NULL,NULL);
305 if (full_path_length == 0)
307 wide_path=(
wchar_t *) RelinquishMagickMemory(wide_path);
308 return((
char *) NULL);
310 full_path=(
wchar_t *) AcquireQuantumMemory(full_path_length,
sizeof(
wchar_t));
311 if (full_path == (
wchar_t *) NULL)
313 wide_path=(
wchar_t *) RelinquishMagickMemory(wide_path);
314 return((
char *) NULL);
316 GetFullPathNameW(wide_path,full_path_length,full_path,NULL);
317 wide_path=(
wchar_t *) RelinquishMagickMemory(wide_path);
321 file_handle=CreateFileW(full_path,GENERIC_READ,FILE_SHARE_READ |
322 FILE_SHARE_WRITE | FILE_SHARE_DELETE,NULL,OPEN_EXISTING,
323 FILE_FLAG_BACKUP_SEMANTICS,NULL);
324 if (file_handle != INVALID_HANDLE_VALUE)
329 final_path_length=GetFinalPathNameByHandleW(file_handle,NULL,0,
330 FILE_NAME_NORMALIZED);
331 if (final_path_length == 0)
333 CloseHandle(file_handle);
334 full_path=(
wchar_t *) RelinquishMagickMemory(full_path);
335 return((
char *) NULL);
337 full_path=(
wchar_t *) RelinquishMagickMemory(full_path);
338 full_path=(
wchar_t *) AcquireQuantumMemory(final_path_length,
340 if (full_path == (
wchar_t *) NULL)
342 CloseHandle(file_handle);
343 return((
char *) NULL);
345 GetFinalPathNameByHandleW(file_handle,full_path,final_path_length,
346 FILE_NAME_NORMALIZED);
347 CloseHandle(file_handle);
352 clean_path=full_path;
353 if (wcsncmp(full_path,L
"\\\\?\\",4) == 0)
354 clean_path=full_path+4;
358 utf8_length=WideCharToMultiByte(CP_UTF8,0,clean_path,-1,NULL,0,NULL,NULL);
359 if (utf8_length <= 0)
361 full_path=(
wchar_t *) RelinquishMagickMemory(full_path);
364 real_path=(
char *) AcquireQuantumMemory(utf8_length,
sizeof(
char));
365 if (real_path == (
char *) NULL)
367 full_path=(
wchar_t *) RelinquishMagickMemory(full_path);
370 WideCharToMultiByte(CP_UTF8,0,clean_path,-1,real_path,utf8_length,NULL,NULL);
371 full_path=(
wchar_t *) RelinquishMagickMemory(full_path);
376static inline int remove_utf8(
const char *path)
378#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
379 return(unlink(path));
387 path_wide=create_wchar_path(path);
388 if (path_wide == (
wchar_t *) NULL)
390 status=_wremove(path_wide);
391 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
396static inline int rename_utf8(
const char *source,
const char *destination)
398#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
399 return(rename(source,destination));
408 source_wide=create_wchar_path(source);
409 if (source_wide == (
wchar_t *) NULL)
411 destination_wide=create_wchar_path(destination);
412 if (destination_wide == (
wchar_t *) NULL)
414 source_wide=(
wchar_t *) RelinquishMagickMemory(source_wide);
417 status=_wrename(source_wide,destination_wide);
418 destination_wide=(
wchar_t *) RelinquishMagickMemory(destination_wide);
419 source_wide=(
wchar_t *) RelinquishMagickMemory(source_wide);
424static inline int set_file_timestamp(
const char *path,
struct stat *attributes)
429#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
430#if defined(MAGICKCORE_HAVE_UTIMENSAT)
431#if defined(__APPLE__) || defined(__NetBSD__)
432#define st_atim st_atimespec
433#define st_ctim st_ctimespec
434#define st_mtim st_mtimespec
440 timestamp[0].tv_sec=attributes->st_atim.tv_sec;
441 timestamp[0].tv_nsec=attributes->st_atim.tv_nsec;
442 timestamp[1].tv_sec=attributes->st_mtim.tv_sec;
443 timestamp[1].tv_nsec=attributes->st_mtim.tv_nsec;
444 status=utimensat(AT_FDCWD,path,timestamp,0);
449 timestamp.actime=attributes->st_atime;
450 timestamp.modtime=attributes->st_mtime;
451 status=utime(path,×tamp);
461 path_wide=create_wchar_path(path);
462 if (path_wide == (WCHAR *) NULL)
464 handle=CreateFileW(path_wide,FILE_WRITE_ATTRIBUTES,FILE_SHARE_WRITE |
465 FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
466 if (handle != (HANDLE) NULL)
476 date_time.QuadPart=(ULONGLONG) (attributes->st_ctime*10000000LL)+
477 116444736000000000LL;
478 creation_time.dwLowDateTime=date_time.LowPart;
479 creation_time.dwHighDateTime=date_time.HighPart;
480 date_time.QuadPart=(ULONGLONG) (attributes->st_atime*10000000LL)+
481 116444736000000000LL;
482 last_access_time.dwLowDateTime=date_time.LowPart;
483 last_access_time.dwHighDateTime=date_time.HighPart;
484 date_time.QuadPart=(ULONGLONG) (attributes->st_mtime*10000000LL)+
485 116444736000000000LL;
486 last_write_time.dwLowDateTime=date_time.LowPart;
487 last_write_time.dwHighDateTime=date_time.HighPart;
488 status=SetFileTime(handle,&creation_time,&last_access_time,&last_write_time);
492 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
497static inline int stat_utf8(
const char *path,
struct stat *attributes)
499#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
500 return(stat(path,attributes));
508 path_wide=create_wchar_path(path);
509 if (path_wide == (WCHAR *) NULL)
511 status=_wstati64(path_wide,attributes);
512 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
517#if defined(__cplusplus) || defined(c_plusplus)