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) && !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 char *realpath_utf8(
const char *path)
269#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
270#if defined(MAGICKCORE_HAVE_REALPATH)
271 return(realpath(path,(
char *) NULL));
273 return(AcquireString(path));
298 if (path == (
const char *) NULL)
299 return((
char *) NULL);
300 length=MultiByteToWideChar(CP_UTF8,0,path,-1,NULL,0);
302 return((
char *) NULL);
303 wide_path=(
wchar_t *) AcquireQuantumMemory(length,
sizeof(
wchar_t));
304 if (wide_path == (
wchar_t *) NULL)
305 return((
char *) NULL);
306 MultiByteToWideChar(CP_UTF8,0,path,-1,wide_path,length);
310 full_path_length=GetFullPathNameW(wide_path,0,NULL,NULL);
311 if (full_path_length == 0)
313 wide_path=(
wchar_t *) RelinquishMagickMemory(wide_path);
314 return((
char *) NULL);
316 full_path=(
wchar_t *) AcquireQuantumMemory(full_path_length,
sizeof(
wchar_t));
317 if (full_path == (
wchar_t *) NULL)
319 wide_path=(
wchar_t *) RelinquishMagickMemory(wide_path);
320 return((
char *) NULL);
322 GetFullPathNameW(wide_path,full_path_length,full_path,NULL);
323 wide_path=(
wchar_t *) RelinquishMagickMemory(wide_path);
327 file_handle=CreateFileW(full_path,GENERIC_READ,FILE_SHARE_READ |
328 FILE_SHARE_WRITE | FILE_SHARE_DELETE,NULL,OPEN_EXISTING,
329 FILE_FLAG_BACKUP_SEMANTICS,NULL);
330 if (file_handle != INVALID_HANDLE_VALUE)
335 final_path_length=GetFinalPathNameByHandleW(file_handle,NULL,0,
336 FILE_NAME_NORMALIZED);
337 if (final_path_length == 0)
339 CloseHandle(file_handle);
340 full_path=(
wchar_t *) RelinquishMagickMemory(full_path);
341 return((
char *) NULL);
343 full_path=(
wchar_t *) RelinquishMagickMemory(full_path);
344 full_path=(
wchar_t *) AcquireQuantumMemory(final_path_length,
346 if (full_path == (
wchar_t *) NULL)
348 CloseHandle(file_handle);
349 return((
char *) NULL);
351 GetFinalPathNameByHandleW(file_handle,full_path,final_path_length,
352 FILE_NAME_NORMALIZED);
353 CloseHandle(file_handle);
358 clean_path=full_path;
359 if (wcsncmp(full_path,L
"\\\\?\\",4) == 0)
360 clean_path=full_path+4;
364 utf8_length=WideCharToMultiByte(CP_UTF8,0,clean_path,-1,NULL,0,NULL,NULL);
365 if (utf8_length <= 0)
367 full_path=(
wchar_t *) RelinquishMagickMemory(full_path);
370 real_path=(
char *) AcquireQuantumMemory(utf8_length,
sizeof(
char));
371 if (real_path == (
char *) NULL)
373 full_path=(
wchar_t *) RelinquishMagickMemory(full_path);
376 WideCharToMultiByte(CP_UTF8,0,clean_path,-1,real_path,utf8_length,NULL,NULL);
377 full_path=(
wchar_t *) RelinquishMagickMemory(full_path);
382static inline int remove_utf8(
const char *path)
384#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
385 return(unlink(path));
393 path_wide=create_wchar_path(path);
394 if (path_wide == (
wchar_t *) NULL)
396 status=_wremove(path_wide);
397 path_wide=(
wchar_t *) RelinquishMagickMemory(path_wide);
402static inline int rename_utf8(
const char *source,
const char *destination)
404#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
405 return(rename(source,destination));
414 source_wide=create_wchar_path(source);
415 if (source_wide == (
wchar_t *) NULL)
417 destination_wide=create_wchar_path(destination);
418 if (destination_wide == (
wchar_t *) NULL)
420 source_wide=(
wchar_t *) RelinquishMagickMemory(source_wide);
423 status=_wrename(source_wide,destination_wide);
424 destination_wide=(
wchar_t *) RelinquishMagickMemory(destination_wide);
425 source_wide=(
wchar_t *) RelinquishMagickMemory(source_wide);
430static inline int set_file_timestamp(
const char *path,
struct stat *attributes)
435#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
436#if defined(MAGICKCORE_HAVE_UTIMENSAT)
437#if defined(__APPLE__) || defined(__NetBSD__)
438#define st_atim st_atimespec
439#define st_ctim st_ctimespec
440#define st_mtim st_mtimespec
446 timestamp[0].tv_sec=attributes->st_atim.tv_sec;
447 timestamp[0].tv_nsec=attributes->st_atim.tv_nsec;
448 timestamp[1].tv_sec=attributes->st_mtim.tv_sec;
449 timestamp[1].tv_nsec=attributes->st_mtim.tv_nsec;
450 status=utimensat(AT_FDCWD,path,timestamp,0);
455 timestamp.actime=attributes->st_atime;
456 timestamp.modtime=attributes->st_mtime;
457 status=utime(path,×tamp);
467 path_wide=create_wchar_path(path);
468 if (path_wide == (WCHAR *) NULL)
470 handle=CreateFileW(path_wide,FILE_WRITE_ATTRIBUTES,FILE_SHARE_WRITE |
471 FILE_SHARE_READ,NULL,OPEN_EXISTING,0,NULL);
472 if (handle != (HANDLE) NULL)
482 date_time.QuadPart=(ULONGLONG) (attributes->st_ctime*10000000LL)+
483 116444736000000000LL;
484 creation_time.dwLowDateTime=date_time.LowPart;
485 creation_time.dwHighDateTime=date_time.HighPart;
486 date_time.QuadPart=(ULONGLONG) (attributes->st_atime*10000000LL)+
487 116444736000000000LL;
488 last_access_time.dwLowDateTime=date_time.LowPart;
489 last_access_time.dwHighDateTime=date_time.HighPart;
490 date_time.QuadPart=(ULONGLONG) (attributes->st_mtime*10000000LL)+
491 116444736000000000LL;
492 last_write_time.dwLowDateTime=date_time.LowPart;
493 last_write_time.dwHighDateTime=date_time.HighPart;
494 status=SetFileTime(handle,&creation_time,&last_access_time,&last_write_time);
498 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
503static inline int stat_utf8(
const char *path,
struct stat *attributes)
505#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__) || defined(__MINGW32__)
506 return(stat(path,attributes));
514 path_wide=create_wchar_path(path);
515 if (path_wide == (WCHAR *) NULL)
517 status=_wstati64(path_wide,attributes);
518 path_wide=(WCHAR *) RelinquishMagickMemory(path_wide);
523#if defined(__cplusplus) || defined(c_plusplus)