42#include "MagickCore/studio.h"
43#include "MagickCore/property.h"
44#include "MagickCore/blob.h"
45#include "MagickCore/color.h"
46#include "MagickCore/exception.h"
47#include "MagickCore/exception-private.h"
48#include "MagickCore/geometry.h"
49#include "MagickCore/image-private.h"
50#include "MagickCore/list.h"
51#include "MagickCore/log.h"
52#include "MagickCore/magick-private.h"
53#include "MagickCore/memory_.h"
54#include "MagickCore/nt-base-private.h"
55#include "MagickCore/option.h"
56#include "MagickCore/policy.h"
57#include "MagickCore/random_.h"
58#include "MagickCore/registry.h"
59#include "MagickCore/resource_.h"
60#include "MagickCore/semaphore.h"
61#include "MagickCore/signature-private.h"
62#include "MagickCore/statistic.h"
63#include "MagickCore/string_.h"
64#include "MagickCore/string-private.h"
65#include "MagickCore/token.h"
66#include "MagickCore/token-private.h"
67#include "MagickCore/utility.h"
68#include "MagickCore/utility-private.h"
69#if defined(MAGICKCORE_HAVE_PROCESS_H)
72#if defined(MAGICKCORE_HAVE_MACH_O_DYLD_H)
73#include <mach-o/dyld.h>
80 Base64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
86 IsPathDirectory(
const char *);
111MagickExport MagickBooleanType AcquireUniqueFilename(
char *path)
116 file=AcquireUniqueFileResource(path);
119 file=close_utf8(file)-1;
153MagickExport MagickBooleanType AcquireUniqueSymbolicLink(
const char *source,
176 assert(source != (
const char *) NULL);
177 assert(destination != (
char *) NULL);
178#if defined(MAGICKCORE_HAVE_SYMLINK)
186 status=IsRightsAuthorized(SystemPolicyDomain,(PolicyRights)
187 (ReadPolicyRights | WritePolicyRights),
"follow");
188 passes=GetPolicyValue(
"system:shred");
189 if ((passes != (
char *) NULL) || (status == MagickFalse))
190 passes=DestroyString(passes);
193 (void) AcquireUniqueFilename(destination);
194 (void) RelinquishUniqueFileResource(destination);
195 if (*source == *DirectorySeparator)
197 if (symlink(source,destination) == 0)
203 path[MagickPathExtent];
206 if (getcwd(path,MagickPathExtent) == (
char *) NULL)
208 (void) ConcatenateMagickString(path,DirectorySeparator,
210 (void) ConcatenateMagickString(path,source,MagickPathExtent);
211 if (symlink(path,destination) == 0)
220 destination_file=AcquireUniqueFileResource(destination);
221 if (destination_file == -1)
223 source_file=open_utf8(source,O_RDONLY | O_BINARY,0);
224 if (source_file == -1)
226 (void) close_utf8(destination_file);
227 (void) RelinquishUniqueFileResource(destination);
230 quantum=(size_t) MagickMaxBufferExtent;
231 if ((fstat(source_file,&attributes) == 0) && (attributes.st_size > 0))
232 quantum=(size_t) MagickMin(attributes.st_size,MagickMaxBufferExtent);
233 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
234 if (buffer == (
unsigned char *) NULL)
236 (void) close_utf8(source_file);
237 (void) close_utf8(destination_file);
238 (void) RelinquishUniqueFileResource(destination);
244 count=(ssize_t) read(source_file,buffer,quantum);
247 length=(size_t) count;
248 count=(ssize_t) write(destination_file,buffer,length);
249 if ((
size_t) count != length)
251 (void) RelinquishUniqueFileResource(destination);
256 (void) close_utf8(destination_file);
257 (void) close_utf8(source_file);
258 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
289MagickExport
void AppendImageFormat(
const char *format,
char *filename)
292 extension[MagickPathExtent],
293 root[MagickPathExtent];
295 assert(format != (
char *) NULL);
296 assert(filename != (
char *) NULL);
297 if (IsEventLogging() != MagickFalse)
298 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
299 if ((*format ==
'\0') || (*filename ==
'\0'))
301 if (LocaleCompare(filename,
"-") == 0)
304 message[MagickPathExtent];
306 (void) FormatLocaleString(message,MagickPathExtent,
"%s:%s",format,
308 (void) CopyMagickString(filename,message,MagickPathExtent);
311 GetPathComponent(filename,ExtensionPath,extension);
312 if ((LocaleCompare(extension,
"Z") == 0) ||
313 (LocaleCompare(extension,
"bz2") == 0) ||
314 (LocaleCompare(extension,
"gz") == 0) ||
315 (LocaleCompare(extension,
"wmz") == 0) ||
316 (LocaleCompare(extension,
"svgz") == 0))
318 GetPathComponent(filename,RootPath,root);
319 (void) CopyMagickString(filename,root,MagickPathExtent);
320 GetPathComponent(filename,RootPath,root);
321 (void) FormatLocaleString(filename,MagickPathExtent,
"%s.%s.%s",root,
325 GetPathComponent(filename,RootPath,root);
326 (void) FormatLocaleString(filename,MagickPathExtent,
"%s.%s",root,format);
355MagickExport
unsigned char *Base64Decode(
const char *source,
size_t *length)
370 assert(source != (
char *) NULL);
371 assert(length != (
size_t *) NULL);
372 if (IsEventLogging() != MagickFalse)
373 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
375 decode=(
unsigned char *) AcquireQuantumMemory((strlen(source)+3)/4,
377 if (decode == (
unsigned char *) NULL)
378 return((
unsigned char *) NULL);
381 for (p=source; *p !=
'\0'; p++)
383 if (isspace((
int) ((
unsigned char) *p)) != 0)
388 if (q == (
char *) NULL)
390 decode=(
unsigned char *) RelinquishMagickMemory(decode);
391 return((
unsigned char *) NULL);
397 decode[i]=(
unsigned char)((q-Base64) << 2);
403 decode[i++]|=(
unsigned char)((q-Base64) >> 4);
404 decode[i]=(
unsigned char)(((q-Base64) & 0x0f) << 4);
410 decode[i++]|=(
unsigned char)((q-Base64) >> 2);
411 decode[i]=(
unsigned char)(((q-Base64) & 0x03) << 6);
417 decode[i++]|=(
unsigned char)(q-Base64);
430 decode=(
unsigned char *) RelinquishMagickMemory(decode);
431 return((
unsigned char *) NULL);
445 decode=(
unsigned char *) RelinquishMagickMemory(decode);
446 return((
unsigned char *) NULL);
450 for ( ; *p !=
'\0'; p++)
451 if (isspace((
int) ((
unsigned char) *p)) == 0)
455 decode=(
unsigned char *) RelinquishMagickMemory(decode);
456 return((
unsigned char *) NULL);
462 for ( ; *p !=
'\0'; p++)
463 if (isspace((
int) ((
unsigned char) *p)) == 0)
465 decode=(
unsigned char *) RelinquishMagickMemory(decode);
466 return((
unsigned char *) NULL);
468 if ((
int) decode[i] != 0)
470 decode=(
unsigned char *) RelinquishMagickMemory(decode);
471 return((
unsigned char *) NULL);
511MagickExport
char *Base64Encode(
const unsigned char *blob,
512 const size_t blob_length,
size_t *encode_length)
526 assert(blob != (
const unsigned char *) NULL);
527 assert(blob_length != 0);
528 assert(encode_length != (
size_t *) NULL);
529 if (IsEventLogging() != MagickFalse)
530 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
532 encode=(
char *) AcquireQuantumMemory(blob_length/3+4,4*
sizeof(*encode));
533 if (encode == (
char *) NULL)
534 return((
char *) NULL);
536 for (p=blob; p < (blob+blob_length-2); p+=(ptrdiff_t) 3)
538 encode[i++]=Base64[(int) (*p >> 2)];
539 encode[i++]=Base64[(int) (((*p & 0x03) << 4)+(*(p+1) >> 4))];
540 encode[i++]=Base64[(int) (((*(p+1) & 0x0f) << 2)+(*(p+2) >> 6))];
541 encode[i++]=Base64[(int) (*(p+2) & 0x3f)];
543 remainder=blob_length % 3;
555 for (j=0; j < (ssize_t) remainder; j++)
557 encode[i++]=Base64[(int) (code[0] >> 2)];
558 encode[i++]=Base64[(int) (((code[0] & 0x03) << 4)+(code[1] >> 4))];
562 encode[i++]=Base64[(int) (((code[1] & 0x0f) << 2)+(code[2] >> 6))];
595MagickPrivate
void ChopPathComponents(
char *path,
const size_t components)
600 for (i=0; i < (ssize_t) components; i++)
601 GetPathComponent(path,HeadPath,path);
627MagickPrivate
void ExpandFilename(
char *path)
630 expand_path[MagickPathExtent];
632 if (path == (
char *) NULL)
636 (void) CopyMagickString(expand_path,path,MagickPathExtent);
637 if ((*(path+1) == *DirectorySeparator) || (*(path+1) ==
'\0'))
645 (void) CopyMagickString(expand_path,
".",MagickPathExtent);
646 (void) ConcatenateMagickString(expand_path,path+1,MagickPathExtent);
647 home=GetEnvironmentValue(
"HOME");
648 if (home == (
char *) NULL)
649 home=GetEnvironmentValue(
"USERPROFILE");
650 if (home != (
char *) NULL)
652 (void) CopyMagickString(expand_path,home,MagickPathExtent);
653 (void) ConcatenateMagickString(expand_path,path+1,MagickPathExtent);
654 home=DestroyString(home);
659#if defined(MAGICKCORE_POSIX_SUPPORT) && !defined(__OS2__)
661#if defined(MAGICKCORE_HAVE_GETPWNAM_R)
662 buffer[MagickPathExtent],
664 username[MagickPathExtent];
676 (void) CopyMagickString(username,path+1,MagickPathExtent);
677 p=strchr(username,
'/');
678 if (p != (
char *) NULL)
680#if !defined(MAGICKCORE_HAVE_GETPWNAM_R)
681 entry=getpwnam(username);
683 entry=(
struct passwd *) NULL;
684 if (getpwnam_r(username,&pwd,buffer,
sizeof(buffer),&entry) < 0)
687 if (entry == (
struct passwd *) NULL)
689 (void) CopyMagickString(expand_path,entry->pw_dir,MagickPathExtent);
690 if (p != (
char *) NULL)
692 (void) ConcatenateMagickString(expand_path,
"/",MagickPathExtent);
693 (void) ConcatenateMagickString(expand_path,p+1,MagickPathExtent);
697 (void) CopyMagickString(path,expand_path,MagickPathExtent);
756static inline void getcwd_utf8(
char *path,
size_t extent)
758#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
762 directory=getcwd(path,extent);
766 wide_path[MagickPathExtent];
768 (void) _wgetcwd(wide_path,MagickPathExtent-1);
769 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(
int) extent,NULL,NULL);
773MagickExport MagickBooleanType ExpandFilenames(
int *number_arguments,
777 home_directory[MagickPathExtent],
794 assert(number_arguments != (
int *) NULL);
795 assert(arguments != (
char ***) NULL);
796 if (IsEventLogging() != MagickFalse)
797 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
798 vector=(
char **) AcquireQuantumMemory((
size_t) (*number_arguments+1),
800 if (vector == (
char **) NULL)
801 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
805 *home_directory=
'\0';
807 for (i=0; i < (ssize_t) *number_arguments; i++)
811 filename[MagickPathExtent],
812 magick[MagickPathExtent],
814 path[MagickPathExtent],
815 subimage[MagickPathExtent];
820 option=(*arguments)[i];
826 vector[count++]=ConstantString(option);
828 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
834 for (j=0; j < parameters; j++)
837 if (i == (ssize_t) *number_arguments)
839 option=(*arguments)[i];
840 vector[count++]=ConstantString(option);
844 if ((*option ==
'"') || (*option ==
'\''))
846 GetPathComponent(option,TailPath,filename);
847 GetPathComponent(option,MagickPath,magick);
848 if ((LocaleCompare(magick,
"CAPTION") == 0) ||
849 (LocaleCompare(magick,
"LABEL") == 0) ||
850 (LocaleCompare(magick,
"PANGO") == 0) ||
851 (LocaleCompare(magick,
"VID") == 0))
853 if ((IsGlob(filename) == MagickFalse) && (*option !=
'@'))
855 if (IsPathAccessible(option) != MagickFalse)
862 GetPathComponent(option,HeadPath,path);
863 GetPathComponent(option,SubimagePath,subimage);
864 ExpandFilename(path);
865 if (*home_directory ==
'\0')
866 getcwd_utf8(home_directory,MagickPathExtent-1);
867 filelist=ListFiles(*path ==
'\0' ? home_directory : path,filename,
884 exception=AcquireExceptionInfo();
885 files=FileToString(option,~0UL,exception);
886 exception=DestroyExceptionInfo(exception);
887 if (files == (
char *) NULL)
889 filelist=StringToArgv(files,&length);
890 if (filelist == (
char **) NULL)
892 files=DestroyString(files);
893 filelist[0]=DestroyString(filelist[0]);
894 for (j=0; j < (ssize_t) (length-1); j++)
895 filelist[j]=filelist[j+1];
896 number_files=(size_t) length-1;
898 if (filelist == (
char **) NULL)
900 for (j=0; j < (ssize_t) number_files; j++)
901 if (IsPathDirectory(filelist[j]) <= 0)
903 if (j == (ssize_t) number_files)
905 for (j=0; j < (ssize_t) number_files; j++)
906 filelist[j]=DestroyString(filelist[j]);
907 filelist=(
char **) RelinquishMagickMemory(filelist);
913 vector=(
char **) ResizeQuantumMemory(vector,(
size_t) ((ssize_t)
914 *number_arguments+count+(ssize_t) number_files+1),
sizeof(*vector));
915 if (vector == (
char **) NULL)
917 for (j=0; j < (ssize_t) number_files; j++)
918 filelist[j]=DestroyString(filelist[j]);
919 filelist=(
char **) RelinquishMagickMemory(filelist);
922 for (j=0; j < (ssize_t) number_files; j++)
925 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
934 vector[count++]=ConstantString(option);
935 for (k=0; k < parameters; k++)
938 if (j == (ssize_t) number_files)
941 vector[count++]=ConstantString(option);
945 (void) CopyMagickString(filename,path,MagickPathExtent);
947 (void) ConcatenateMagickString(filename,DirectorySeparator,
949 if (filelist[j] != (
char *) NULL)
950 (void) ConcatenateMagickString(filename,filelist[j],MagickPathExtent);
951 filelist[j]=DestroyString(filelist[j]);
952 if (strlen(filename) >= (MagickPathExtent-1))
953 ThrowFatalException(OptionFatalError,
"FilenameTruncated");
954 if (IsPathDirectory(filename) <= 0)
957 file_path[MagickPathExtent];
962 (void) ConcatenateMagickString(file_path,magick,
964 (void) ConcatenateMagickString(file_path,
":",MagickPathExtent);
966 (void) ConcatenateMagickString(file_path,filename,MagickPathExtent);
967 if (*subimage !=
'\0')
969 (void) ConcatenateMagickString(file_path,
"[",MagickPathExtent);
970 (void) ConcatenateMagickString(file_path,subimage,
972 (void) ConcatenateMagickString(file_path,
"]",MagickPathExtent);
974 if (strlen(file_path) >= (MagickPathExtent-1))
975 ThrowFatalException(OptionFatalError,
"FilenameTruncated");
976 if (destroy != MagickFalse)
979 vector[count]=DestroyString(vector[count]);
982 vector[count++]=ConstantString(file_path);
985 filelist=(
char **) RelinquishMagickMemory(filelist);
987 vector[count]=(
char *) NULL;
988 if (IsEventLogging() != MagickFalse)
993 command_line=AcquireString(vector[0]);
994 for (i=1; i < count; i++)
996 (void) ConcatenateString(&command_line,
" {");
997 (void) ConcatenateString(&command_line,vector[i]);
998 (void) ConcatenateString(&command_line,
"}");
1000 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1001 "Command line: %s",command_line);
1002 command_line=DestroyString(command_line);
1004 *number_arguments=(int) count;
1034MagickPrivate MagickBooleanType GetExecutionPath(
char *path,
const size_t extent)
1040 directory=getcwd(path,(
unsigned long) extent);
1042#if defined(MAGICKCORE_HAVE_GETPID) && defined(MAGICKCORE_HAVE_READLINK) && defined(PATH_MAX)
1045 execution_path[PATH_MAX+1],
1046 link_path[MagickPathExtent];
1051 (void) FormatLocaleString(link_path,MagickPathExtent,
"/proc/%.20g/exe",
1053 count=readlink(link_path,execution_path,PATH_MAX);
1056 (void) FormatLocaleString(link_path,MagickPathExtent,
"/proc/%.20g/file",
1058 count=readlink(link_path,execution_path,PATH_MAX);
1060 if ((count > 0) && (count <= (ssize_t) PATH_MAX))
1062 execution_path[count]=
'\0';
1063 (void) CopyMagickString(path,execution_path,extent);
1067#if defined(MAGICKCORE_HAVE__NSGETEXECUTABLEPATH)
1070 executable_path[PATH_MAX << 1];
1075 length=
sizeof(executable_path);
1076 if (_NSGetExecutablePath(executable_path,&length) == 0)
1079 *real_path = realpath_utf8(executable_path);
1081 if (real_path != (
char *) NULL)
1083 (void) CopyMagickString(path,real_path,extent);
1084 real_path=DestroyString(real_path);
1089#if defined(MAGICKCORE_HAVE_GETEXECNAME)
1094 execution_path=(
const char *) getexecname();
1095 if (execution_path != (
const char *) NULL)
1097 if (*execution_path != *DirectorySeparator)
1098 (void) ConcatenateMagickString(path,DirectorySeparator,extent);
1099 (void) ConcatenateMagickString(path,execution_path,extent);
1103#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1104 NTGetExecutionPath(path,extent);
1115 program_name=program_invocation_name;
1116 if (*program_invocation_name !=
'/')
1121 extent=strlen(directory)+strlen(program_name)+2;
1122 program_name=AcquireQuantumMemory(extent,
sizeof(*program_name));
1123 if (program_name == (
char *) NULL)
1124 program_name=program_invocation_name;
1126 count=FormatLocaleString(program_name,extent,
"%s/%s",directory,
1127 program_invocation_name);
1132 *real_path = realpath_utf8(program_name);
1134 if (real_path != (
char *) NULL)
1136 (void) CopyMagickString(path,real_path,extent);
1137 real_path=DestroyString(real_path);
1140 if (program_name != program_invocation_name)
1141 program_name=(
char *) RelinquishMagickMemory(program_name);
1144#if defined(__OpenBSD__)
1149 (void) CopyMagickString(path,__progname,extent);
1152 return(IsPathAccessible(path));
1173MagickPrivate ssize_t GetMagickPageSize(
void)
1180#if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
1181 page_size=(ssize_t) sysconf(_SC_PAGE_SIZE);
1182#elif defined(MAGICKCORE_HAVE_GETPAGESIZE)
1183 page_size=(ssize_t) getpagesize();
1214MagickExport MagickBooleanType GetPathAttributes(
const char *path,
1220 if (path == (
const char *) NULL)
1223 return(MagickFalse);
1225 (void) memset(attributes,0,
sizeof(
struct stat));
1226 status=stat_utf8(path,(
struct stat *) attributes) == 0 ? MagickTrue :
1262MagickExport
void GetPathComponent(
const char *path,PathType type,
1276 assert(path != (
const char *) NULL);
1277 assert(component != (
char *) NULL);
1278 if (IsEventLogging() != MagickFalse)
1279 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",path);
1285 (void) CopyMagickString(component,path,MagickPathExtent);
1288 if (type != SubcanonicalPath)
1290 p=component+strlen(component)-1;
1291 if ((strlen(component) > 2) && (*p ==
']'))
1293 q=strrchr(component,
'[');
1294 if ((q != (
char *) NULL) && ((q == component) || (*(q-1) !=
']')) &&
1295 (IsPathAccessible(path) == MagickFalse))
1301 if ((IsSceneGeometry(q+1,MagickFalse) == MagickFalse) &&
1302 (IsGeometry(q+1) == MagickFalse))
1306 subimage_length=(size_t) (p-q);
1307 subimage_offset=(size_t) (q-component+1);
1317 for (p=component; *p !=
'\0'; p++)
1319 if ((*p ==
'%') && (*(p+1) ==
'['))
1324 for (p++; (*p !=
']') && (*p !=
'\0'); p++) ;
1328 if ((p != component) && (*p ==
':') && (IsPathDirectory(component) < 0) &&
1329 (IsPathAccessible(component) == MagickFalse))
1335 if (IsMagickConflict(component) != MagickFalse)
1339 magick_length=(size_t) (p-component+1);
1340 for (q=component; *(++p) !=
'\0'; q++)
1349 for (p=component+strlen(component)-1; p > component; p--)
1350 if (IsBasenameSeparator(*p) != MagickFalse)
1356 if (magick_length != 0)
1357 (void) CopyMagickString(component,path,magick_length);
1364 if (*component !=
'\0')
1366 for (p=component+(strlen(component)-1); p > component; p--)
1368 if (IsBasenameSeparator(*p) != MagickFalse)
1386 if (IsBasenameSeparator(*p) != MagickFalse)
1387 (void) CopyMagickString(component,p+1,MagickPathExtent);
1392 if (IsBasenameSeparator(*p) != MagickFalse)
1393 (void) CopyMagickString(component,p+1,MagickPathExtent);
1394 if (*component !=
'\0')
1395 for (p=component+(strlen(component)-1); p > component; p--)
1403 case BasePathSansCompressExtension:
1406 extension[MagickPathExtent];
1411 GetPathComponent(path,ExtensionPath,extension);
1412 if ((LocaleCompare(extension,
"bz2") == 0) ||
1413 (LocaleCompare(extension,
"gz") == 0) ||
1414 (LocaleCompare(extension,
"svgz") == 0) ||
1415 (LocaleCompare(extension,
"wmz") == 0) ||
1416 (LocaleCompare(extension,
"Z") == 0))
1417 GetPathComponent(path,BasePath,component);
1422 if (IsBasenameSeparator(*p) != MagickFalse)
1423 (void) CopyMagickString(component,p+1,MagickPathExtent);
1424 if (*component !=
'\0')
1425 for (p=component+strlen(component)-1; p > component; p--)
1430 (void) CopyMagickString(component,p+1,MagickPathExtent);
1436 if ((subimage_length != 0) && (magick_length < subimage_offset))
1437 (void) CopyMagickString(component,path+subimage_offset,subimage_length);
1440 case SubcanonicalPath:
1472MagickPrivate
char **GetPathComponents(
const char *path,
1473 size_t *number_components)
1485 if (path == (
char *) NULL)
1486 return((
char **) NULL);
1487 *number_components=1;
1488 for (p=path; *p !=
'\0'; p++)
1489 if (IsBasenameSeparator(*p))
1490 (*number_components)++;
1491 components=(
char **) AcquireQuantumMemory((
size_t) *number_components+1UL,
1492 sizeof(*components));
1493 if (components == (
char **) NULL)
1494 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1496 for (i=0; i < (ssize_t) *number_components; i++)
1498 for (q=p; *q !=
'\0'; q++)
1499 if (IsBasenameSeparator(*q))
1501 components[i]=(
char *) AcquireQuantumMemory((
size_t) (q-p)+MagickPathExtent,
1502 sizeof(**components));
1503 if (components[i] == (
char *) NULL)
1504 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1505 (void) CopyMagickString(components[i],p,(
size_t) (q-p+1));
1508 components[i]=(
char *) NULL;
1535MagickExport MagickBooleanType IsPathAccessible(
const char *path)
1543 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1544 return(MagickFalse);
1545 if (LocaleCompare(path,
"-") == 0)
1547 status=GetPathAttributes(path,&attributes);
1548 if (status == MagickFalse)
1550 if (S_ISREG(attributes.st_mode) == 0)
1551 return(MagickFalse);
1552 if (access_utf8(path,F_OK) != 0)
1553 return(MagickFalse);
1580static int IsPathDirectory(
const char *path)
1588 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1589 return(MagickFalse);
1590 status=GetPathAttributes(path,&attributes);
1591 if (status == MagickFalse)
1593 if (S_ISDIR(attributes.st_mode) == 0)
1633#if defined(__cplusplus) || defined(c_plusplus)
1637static int FileCompare(
const void *x,
const void *y)
1643 p=(
const char **) x;
1644 q=(
const char **) y;
1645 return(LocaleCompare(*p,*q));
1648#if defined(__cplusplus) || defined(c_plusplus)
1652MagickPrivate
char **ListFiles(
const char *directory,
const char *pattern,
1653 size_t *number_entries)
1671 assert(directory != (
const char *) NULL);
1672 assert(pattern != (
const char *) NULL);
1673 assert(number_entries != (
size_t *) NULL);
1674 if (IsEventLogging() != MagickFalse)
1675 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",directory);
1677 current_directory=opendir(directory);
1678 if (current_directory == (DIR *) NULL)
1679 return((
char **) NULL);
1684 filelist=(
char **) AcquireQuantumMemory((
size_t) max_entries,
1686 if (filelist == (
char **) NULL)
1688 (void) closedir(current_directory);
1689 return((
char **) NULL);
1694 buffer=(
struct dirent *) AcquireMagickMemory(
sizeof(*buffer)+FILENAME_MAX+1);
1695 if (buffer == (
struct dirent *) NULL)
1696 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1697 while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
1698 (entry != (
struct dirent *) NULL))
1700 if ((LocaleCompare(entry->d_name,
".") == 0) ||
1701 (LocaleCompare(entry->d_name,
"..") == 0))
1703 if ((IsPathDirectory(entry->d_name) > 0) ||
1704#
if defined(MAGICKCORE_WINDOWS_SUPPORT)
1705 (GlobExpression(entry->d_name,pattern,MagickTrue) != MagickFalse))
1707 (GlobExpression(entry->d_name,pattern,MagickFalse) != MagickFalse))
1710 if (*number_entries >= max_entries)
1716 filelist=(
char **) ResizeQuantumMemory(filelist,(
size_t)
1717 max_entries,
sizeof(*filelist));
1718 if (filelist == (
char **) NULL)
1726 p=strchr(entry->d_name,
';');
1729 if (*number_entries > 0)
1730 if (LocaleCompare(entry->d_name,filelist[*number_entries-1]) == 0)
1734 filelist[*number_entries]=(
char *) AcquireString(entry->d_name);
1735 (*number_entries)++;
1738 buffer=(
struct dirent *) RelinquishMagickMemory(buffer);
1739 (void) closedir(current_directory);
1740 if (filelist == (
char **) NULL)
1741 return((
char **) NULL);
1745 qsort((
void *) filelist,(
size_t) *number_entries,
sizeof(*filelist),
1774MagickExport
void MagickDelay(
const MagickSizeType milliseconds)
1776 if (milliseconds == 0)
1778#if defined(MAGICKCORE_HAVE_NANOSLEEP)
1783 timer.tv_sec=(time_t) (milliseconds/1000);
1784 timer.tv_nsec=(time_t) ((milliseconds % 1000)*1000*1000);
1785 (void) nanosleep(&timer,(
struct timespec *) NULL);
1787#elif defined(MAGICKCORE_HAVE_USLEEP)
1788 usleep(1000*milliseconds);
1789#elif defined(MAGICKCORE_HAVE_SELECT)
1794 timer.tv_sec=(long) milliseconds/1000;
1795 timer.tv_usec=(long) (milliseconds % 1000)*1000;
1796 (void) select(0,(XFD_SET *) NULL,(XFD_SET *) NULL,(XFD_SET *) NULL,&timer);
1798#elif defined(MAGICKCORE_HAVE_POLL)
1799 (void) poll((
struct pollfd *) NULL,0,(
int) milliseconds);
1800#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
1801 Sleep((
long) milliseconds);
1807 timer=milliseconds/1000.0;
1810#elif defined(__BEOS__)
1811 snooze(1000*milliseconds);
1817 time_end=clock()+milliseconds*CLOCKS_PER_SEC/1000;
1818 while (clock() < time_end)
1848MagickExport
size_t MultilineCensus(
const char *label)
1856 if (label == (
char *) NULL)
1858 for (number_lines=1; *label !=
'\0'; label++)
1861 return(number_lines);
1887MagickPrivate MagickBooleanType ShredFile(
const char *path)
1914 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1915 return(MagickFalse);
1922 property=GetEnvironmentValue(
"MAGICK_SHRED_PASSES");
1923 if (property != (
char *) NULL)
1925 passes=(ssize_t) StringToInteger(property);
1926 property=DestroyString(property);
1928 property=GetPolicyValue(
"system:shred");
1929 if (property != (
char *) NULL)
1931 passes=(ssize_t) StringToInteger(property);
1932 property=DestroyString(property);
1940 file=open_utf8(path,O_WRONLY | O_EXCL | O_BINARY,S_MODE);
1942 return(MagickFalse);
1943 quantum=(size_t) MagickMinBufferExtent;
1944 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1945 quantum=(size_t) MagickMin(file_stats.st_size,MagickMinBufferExtent);
1946 length=(MagickSizeType) file_stats.st_size;
1947 random_info=AcquireRandomInfo();
1948 key=GetRandomKey(random_info,quantum);
1949 for (i=0; i < passes; i++)
1957 if (lseek(file,0,SEEK_SET) < 0)
1959 for (j=0; j < (MagickOffsetType) length; j+=count)
1962 SetRandomKey(random_info,quantum,GetStringInfoDatum(key));
1963 count=write(file,GetStringInfoDatum(key),(
size_t)
1964 MagickMin((MagickOffsetType) quantum,(MagickOffsetType) length-j));
1972 if (j < (MagickOffsetType) length)
1975 key=DestroyStringInfo(key);
1976 random_info=DestroyRandomInfo(random_info);
1977 status=close_utf8(file);
1978 return((status == -1 || i < passes) ? MagickFalse : MagickTrue);