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/policy-private.h"
58#include "MagickCore/random_.h"
59#include "MagickCore/registry.h"
60#include "MagickCore/resource_.h"
61#include "MagickCore/semaphore.h"
62#include "MagickCore/signature-private.h"
63#include "MagickCore/statistic.h"
64#include "MagickCore/string_.h"
65#include "MagickCore/string-private.h"
66#include "MagickCore/token.h"
67#include "MagickCore/token-private.h"
68#include "MagickCore/utility.h"
69#include "MagickCore/utility-private.h"
70#if defined(MAGICKCORE_HAVE_PROCESS_H)
73#if defined(MAGICKCORE_HAVE_MACH_O_DYLD_H)
74#include <mach-o/dyld.h>
81 Base64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
87 IsPathDirectory(
const char *);
112MagickExport MagickBooleanType AcquireUniqueFilename(
char *path)
117 file=AcquireUniqueFileResource(path);
120 file=close_utf8(file)-1;
154MagickExport MagickBooleanType AcquireUniqueSymbolicLink(
const char *source,
177 assert(source != (
const char *) NULL);
178 assert(destination != (
char *) NULL);
179#if defined(MAGICKCORE_HAVE_SYMLINK)
187 status=IsRightsAuthorizedByName(SystemPolicyDomain,
"symlink",(PolicyRights)
188 (ReadPolicyRights | WritePolicyRights),
"follow");
189 passes=GetPolicyValue(
"system:shred");
190 if ((passes != (
char *) NULL) || (status == MagickFalse))
191 passes=DestroyString(passes);
194 (void) AcquireUniqueFilename(destination);
195 (void) RelinquishUniqueFileResource(destination);
196 if (*source == *DirectorySeparator)
198 if (symlink(source,destination) == 0)
204 path[MagickPathExtent];
207 if (getcwd(path,MagickPathExtent) == (
char *) NULL)
209 (void) ConcatenateMagickString(path,DirectorySeparator,
211 (void) ConcatenateMagickString(path,source,MagickPathExtent);
212 if (symlink(path,destination) == 0)
221 destination_file=AcquireUniqueFileResource(destination);
222 if (destination_file == -1)
224 source_file=open_utf8(source,O_RDONLY | O_BINARY,0);
225 if (source_file == -1)
227 (void) close_utf8(destination_file);
228 (void) RelinquishUniqueFileResource(destination);
231 quantum=(size_t) MagickMaxBufferExtent;
232 if ((fstat(source_file,&attributes) == 0) && (attributes.st_size > 0))
233 quantum=(size_t) MagickMin(attributes.st_size,MagickMaxBufferExtent);
234 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
235 if (buffer == (
unsigned char *) NULL)
237 (void) close_utf8(source_file);
238 (void) close_utf8(destination_file);
239 (void) RelinquishUniqueFileResource(destination);
245 count=(ssize_t) read(source_file,buffer,quantum);
248 length=(size_t) count;
249 count=(ssize_t) write(destination_file,buffer,length);
250 if ((
size_t) count != length)
252 (void) RelinquishUniqueFileResource(destination);
257 (void) close_utf8(destination_file);
258 (void) close_utf8(source_file);
259 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
290MagickExport
void AppendImageFormat(
const char *format,
char *filename)
293 extension[MagickPathExtent],
294 root[MagickPathExtent];
296 assert(format != (
char *) NULL);
297 assert(filename != (
char *) NULL);
298 if (IsEventLogging() != MagickFalse)
299 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
300 if ((*format ==
'\0') || (*filename ==
'\0'))
302 if (LocaleCompare(filename,
"-") == 0)
305 message[MagickPathExtent];
307 (void) FormatLocaleString(message,MagickPathExtent,
"%s:%s",format,
309 (void) CopyMagickString(filename,message,MagickPathExtent);
312 GetPathComponent(filename,ExtensionPath,extension);
313 if ((LocaleCompare(extension,
"Z") == 0) ||
314 (LocaleCompare(extension,
"bz2") == 0) ||
315 (LocaleCompare(extension,
"gz") == 0) ||
316 (LocaleCompare(extension,
"wmz") == 0) ||
317 (LocaleCompare(extension,
"svgz") == 0))
319 GetPathComponent(filename,RootPath,root);
320 (void) CopyMagickString(filename,root,MagickPathExtent);
321 GetPathComponent(filename,RootPath,root);
322 (void) FormatLocaleString(filename,MagickPathExtent,
"%s.%s.%s",root,
326 GetPathComponent(filename,RootPath,root);
327 (void) FormatLocaleString(filename,MagickPathExtent,
"%s.%s",root,format);
356MagickExport
unsigned char *Base64Decode(
const char *source,
size_t *length)
371 assert(source != (
char *) NULL);
372 assert(length != (
size_t *) NULL);
373 if (IsEventLogging() != MagickFalse)
374 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
376 decode=(
unsigned char *) AcquireQuantumMemory((strlen(source)+3)/4,
378 if (decode == (
unsigned char *) NULL)
379 return((
unsigned char *) NULL);
382 for (p=source; *p !=
'\0'; p++)
384 if (isspace((
int) ((
unsigned char) *p)) != 0)
389 if (q == (
char *) NULL)
391 decode=(
unsigned char *) RelinquishMagickMemory(decode);
392 return((
unsigned char *) NULL);
398 decode[i]=(
unsigned char)((q-Base64) << 2);
404 decode[i++]|=(
unsigned char)((q-Base64) >> 4);
405 decode[i]=(
unsigned char)(((q-Base64) & 0x0f) << 4);
411 decode[i++]|=(
unsigned char)((q-Base64) >> 2);
412 decode[i]=(
unsigned char)(((q-Base64) & 0x03) << 6);
418 decode[i++]|=(
unsigned char)(q-Base64);
431 decode=(
unsigned char *) RelinquishMagickMemory(decode);
432 return((
unsigned char *) NULL);
446 decode=(
unsigned char *) RelinquishMagickMemory(decode);
447 return((
unsigned char *) NULL);
451 for ( ; *p !=
'\0'; p++)
452 if (isspace((
int) ((
unsigned char) *p)) == 0)
456 decode=(
unsigned char *) RelinquishMagickMemory(decode);
457 return((
unsigned char *) NULL);
463 for ( ; *p !=
'\0'; p++)
464 if (isspace((
int) ((
unsigned char) *p)) == 0)
466 decode=(
unsigned char *) RelinquishMagickMemory(decode);
467 return((
unsigned char *) NULL);
469 if ((
int) decode[i] != 0)
471 decode=(
unsigned char *) RelinquishMagickMemory(decode);
472 return((
unsigned char *) NULL);
512MagickExport
char *Base64Encode(
const unsigned char *blob,
513 const size_t blob_length,
size_t *encode_length)
527 assert(blob != (
const unsigned char *) NULL);
528 assert(blob_length != 0);
529 assert(encode_length != (
size_t *) NULL);
530 if (IsEventLogging() != MagickFalse)
531 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
533 encode=(
char *) AcquireQuantumMemory(blob_length/3+4,4*
sizeof(*encode));
534 if (encode == (
char *) NULL)
535 return((
char *) NULL);
537 for (p=blob; p < (blob+blob_length-2); p+=(ptrdiff_t) 3)
539 encode[i++]=Base64[(int) (*p >> 2)];
540 encode[i++]=Base64[(int) (((*p & 0x03) << 4)+(*(p+1) >> 4))];
541 encode[i++]=Base64[(int) (((*(p+1) & 0x0f) << 2)+(*(p+2) >> 6))];
542 encode[i++]=Base64[(int) (*(p+2) & 0x3f)];
544 remainder=blob_length % 3;
556 for (j=0; j < (ssize_t) remainder; j++)
558 encode[i++]=Base64[(int) (code[0] >> 2)];
559 encode[i++]=Base64[(int) (((code[0] & 0x03) << 4)+(code[1] >> 4))];
563 encode[i++]=Base64[(int) (((code[1] & 0x0f) << 2)+(code[2] >> 6))];
596MagickPrivate
void ChopPathComponents(
char *path,
const size_t components)
601 for (i=0; i < (ssize_t) components; i++)
602 GetPathComponent(path,HeadPath,path);
628MagickPrivate
void ExpandFilename(
char *path)
631 expand_path[MagickPathExtent];
633 if (path == (
char *) NULL)
637 (void) CopyMagickString(expand_path,path,MagickPathExtent);
638 if ((*(path+1) == *DirectorySeparator) || (*(path+1) ==
'\0'))
646 (void) CopyMagickString(expand_path,
".",MagickPathExtent);
647 (void) ConcatenateMagickString(expand_path,path+1,MagickPathExtent);
648 home=GetEnvironmentValue(
"HOME");
649 if (home == (
char *) NULL)
650 home=GetEnvironmentValue(
"USERPROFILE");
651 if (home != (
char *) NULL)
653 (void) CopyMagickString(expand_path,home,MagickPathExtent);
654 (void) ConcatenateMagickString(expand_path,path+1,MagickPathExtent);
655 home=DestroyString(home);
660#if defined(MAGICKCORE_POSIX_SUPPORT) && !defined(__OS2__)
662#if defined(MAGICKCORE_HAVE_GETPWNAM_R)
663 buffer[MagickPathExtent],
665 username[MagickPathExtent];
677 (void) CopyMagickString(username,path+1,MagickPathExtent);
678 p=strchr(username,
'/');
679 if (p != (
char *) NULL)
681#if !defined(MAGICKCORE_HAVE_GETPWNAM_R)
682 entry=getpwnam(username);
684 entry=(
struct passwd *) NULL;
685 if (getpwnam_r(username,&pwd,buffer,
sizeof(buffer),&entry) < 0)
688 if (entry == (
struct passwd *) NULL)
690 (void) CopyMagickString(expand_path,entry->pw_dir,MagickPathExtent);
691 if (p != (
char *) NULL)
693 (void) ConcatenateMagickString(expand_path,
"/",MagickPathExtent);
694 (void) ConcatenateMagickString(expand_path,p+1,MagickPathExtent);
698 (void) CopyMagickString(path,expand_path,MagickPathExtent);
757static inline void getcwd_utf8(
char *path,
size_t extent)
759#if !defined(MAGICKCORE_WINDOWS_SUPPORT) || defined(__CYGWIN__)
763 directory=getcwd(path,extent);
767 wide_path[MagickPathExtent];
769 (void) _wgetcwd(wide_path,MagickPathExtent-1);
770 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(
int) extent,NULL,NULL);
774MagickExport MagickBooleanType ExpandFilenames(
int *number_arguments,
778 home_directory[MagickPathExtent],
795 assert(number_arguments != (
int *) NULL);
796 assert(arguments != (
char ***) NULL);
797 if (IsEventLogging() != MagickFalse)
798 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
799 vector=(
char **) AcquireQuantumMemory((
size_t) (*number_arguments+1),
801 if (vector == (
char **) NULL)
802 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
806 *home_directory=
'\0';
808 for (i=0; i < (ssize_t) *number_arguments; i++)
812 filename[MagickPathExtent],
813 magick[MagickPathExtent],
815 path[MagickPathExtent],
816 subimage[MagickPathExtent];
821 option=(*arguments)[i];
827 vector[count++]=ConstantString(option);
829 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
835 for (j=0; j < parameters; j++)
838 if (i == (ssize_t) *number_arguments)
840 option=(*arguments)[i];
841 vector[count++]=ConstantString(option);
845 if ((*option ==
'"') || (*option ==
'\''))
847 GetPathComponent(option,TailPath,filename);
848 GetPathComponent(option,MagickPath,magick);
849 if ((LocaleCompare(magick,
"CAPTION") == 0) ||
850 (LocaleCompare(magick,
"LABEL") == 0) ||
851 (LocaleCompare(magick,
"PANGO") == 0) ||
852 (LocaleCompare(magick,
"VID") == 0))
854 if ((IsGlob(filename) == MagickFalse) && (*option !=
'@'))
856 if (IsPathAccessible(option) != MagickFalse)
863 GetPathComponent(option,HeadPath,path);
864 GetPathComponent(option,SubimagePath,subimage);
865 ExpandFilename(path);
866 if (*home_directory ==
'\0')
867 getcwd_utf8(home_directory,MagickPathExtent-1);
868 filelist=ListFiles(*path ==
'\0' ? home_directory : path,filename,
885 exception=AcquireExceptionInfo();
886 files=FileToString(option,~0UL,exception);
887 exception=DestroyExceptionInfo(exception);
888 if (files == (
char *) NULL)
890 filelist=StringToArgv(files,&length);
891 if (filelist == (
char **) NULL)
893 files=DestroyString(files);
894 filelist[0]=DestroyString(filelist[0]);
895 for (j=0; j < (ssize_t) (length-1); j++)
896 filelist[j]=filelist[j+1];
897 number_files=(size_t) length-1;
899 if (filelist == (
char **) NULL)
901 for (j=0; j < (ssize_t) number_files; j++)
902 if (IsPathDirectory(filelist[j]) <= 0)
904 if (j == (ssize_t) number_files)
906 for (j=0; j < (ssize_t) number_files; j++)
907 filelist[j]=DestroyString(filelist[j]);
908 filelist=(
char **) RelinquishMagickMemory(filelist);
914 vector=(
char **) ResizeQuantumMemory(vector,(
size_t) ((ssize_t)
915 *number_arguments+count+(ssize_t) number_files+1),
sizeof(*vector));
916 if (vector == (
char **) NULL)
918 for (j=0; j < (ssize_t) number_files; j++)
919 filelist[j]=DestroyString(filelist[j]);
920 filelist=(
char **) RelinquishMagickMemory(filelist);
923 for (j=0; j < (ssize_t) number_files; j++)
926 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
935 vector[count++]=ConstantString(option);
936 for (k=0; k < parameters; k++)
939 if (j == (ssize_t) number_files)
942 vector[count++]=ConstantString(option);
946 (void) CopyMagickString(filename,path,MagickPathExtent);
948 (void) ConcatenateMagickString(filename,DirectorySeparator,
950 if (filelist[j] != (
char *) NULL)
951 (void) ConcatenateMagickString(filename,filelist[j],MagickPathExtent);
952 filelist[j]=DestroyString(filelist[j]);
953 if (strlen(filename) >= (MagickPathExtent-1))
954 ThrowFatalException(OptionFatalError,
"FilenameTruncated");
955 if (IsPathDirectory(filename) <= 0)
958 file_path[MagickPathExtent];
963 (void) ConcatenateMagickString(file_path,magick,
965 (void) ConcatenateMagickString(file_path,
":",MagickPathExtent);
967 (void) ConcatenateMagickString(file_path,filename,MagickPathExtent);
968 if (*subimage !=
'\0')
970 (void) ConcatenateMagickString(file_path,
"[",MagickPathExtent);
971 (void) ConcatenateMagickString(file_path,subimage,
973 (void) ConcatenateMagickString(file_path,
"]",MagickPathExtent);
975 if (strlen(file_path) >= (MagickPathExtent-1))
976 ThrowFatalException(OptionFatalError,
"FilenameTruncated");
977 if (destroy != MagickFalse)
980 vector[count]=DestroyString(vector[count]);
983 vector[count++]=ConstantString(file_path);
986 filelist=(
char **) RelinquishMagickMemory(filelist);
988 vector[count]=(
char *) NULL;
989 if (IsEventLogging() != MagickFalse)
994 command_line=AcquireString(vector[0]);
995 for (i=1; i < count; i++)
997 (void) ConcatenateString(&command_line,
" {");
998 (void) ConcatenateString(&command_line,vector[i]);
999 (void) ConcatenateString(&command_line,
"}");
1001 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
1002 "Command line: %s",command_line);
1003 command_line=DestroyString(command_line);
1005 *number_arguments=(int) count;
1035MagickPrivate MagickBooleanType GetExecutionPath(
char *path,
const size_t extent)
1041 directory=getcwd(path,(
unsigned long) extent);
1043#if defined(MAGICKCORE_HAVE_GETPID) && defined(MAGICKCORE_HAVE_READLINK) && defined(PATH_MAX)
1046 execution_path[PATH_MAX+1],
1047 link_path[MagickPathExtent];
1052 (void) FormatLocaleString(link_path,MagickPathExtent,
"/proc/%.20g/exe",
1054 count=readlink(link_path,execution_path,PATH_MAX);
1057 (void) FormatLocaleString(link_path,MagickPathExtent,
"/proc/%.20g/file",
1059 count=readlink(link_path,execution_path,PATH_MAX);
1061 if ((count > 0) && (count <= (ssize_t) PATH_MAX))
1063 execution_path[count]=
'\0';
1064 (void) CopyMagickString(path,execution_path,extent);
1068#if defined(MAGICKCORE_HAVE__NSGETEXECUTABLEPATH)
1071 executable_path[PATH_MAX << 1];
1076 length=
sizeof(executable_path);
1077 if (_NSGetExecutablePath(executable_path,&length) == 0)
1080 *real_path = realpath_utf8(executable_path);
1082 if (real_path != (
char *) NULL)
1084 (void) CopyMagickString(path,real_path,extent);
1085 real_path=DestroyString(real_path);
1090#if defined(MAGICKCORE_HAVE_GETEXECNAME)
1095 execution_path=(
const char *) getexecname();
1096 if (execution_path != (
const char *) NULL)
1098 if (*execution_path != *DirectorySeparator)
1099 (void) ConcatenateMagickString(path,DirectorySeparator,extent);
1100 (void) ConcatenateMagickString(path,execution_path,extent);
1104#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1105 NTGetExecutionPath(path,extent);
1116 program_name=program_invocation_name;
1117 if (*program_invocation_name !=
'/')
1122 extent=strlen(directory)+strlen(program_name)+2;
1123 program_name=AcquireQuantumMemory(extent,
sizeof(*program_name));
1124 if (program_name == (
char *) NULL)
1125 program_name=program_invocation_name;
1127 count=FormatLocaleString(program_name,extent,
"%s/%s",directory,
1128 program_invocation_name);
1133 *real_path = realpath_utf8(program_name);
1135 if (real_path != (
char *) NULL)
1137 (void) CopyMagickString(path,real_path,extent);
1138 real_path=DestroyString(real_path);
1141 if (program_name != program_invocation_name)
1142 program_name=(
char *) RelinquishMagickMemory(program_name);
1145#if defined(__OpenBSD__)
1150 (void) CopyMagickString(path,__progname,extent);
1153 return(IsPathAccessible(path));
1174MagickPrivate ssize_t GetMagickPageSize(
void)
1181#if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
1182 page_size=(ssize_t) sysconf(_SC_PAGE_SIZE);
1183#elif defined(MAGICKCORE_HAVE_GETPAGESIZE)
1184 page_size=(ssize_t) getpagesize();
1215MagickExport MagickBooleanType GetPathAttributes(
const char *path,
1221 if (path == (
const char *) NULL)
1224 return(MagickFalse);
1226 (void) memset(attributes,0,
sizeof(
struct stat));
1227 status=stat_utf8(path,(
struct stat *) attributes) == 0 ? MagickTrue :
1263MagickExport
void GetPathComponent(
const char *path,PathType type,
1277 assert(path != (
const char *) NULL);
1278 assert(component != (
char *) NULL);
1279 if (IsEventLogging() != MagickFalse)
1280 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",path);
1286 (void) CopyMagickString(component,path,MagickPathExtent);
1289 if (type != SubcanonicalPath)
1291 p=component+strlen(component)-1;
1292 if ((strlen(component) > 2) && (*p ==
']'))
1294 ExceptionInfo *exception = AcquireExceptionInfo();
1295 char *literal = (
char *) GetImageRegistry(StringRegistryType,
1296 "filename:literal",exception);
1297 q=strrchr(component,
'[');
1298 if ((q != (
char *) NULL) &&
1299 ((IsStringTrue(literal) == MagickFalse) ||
1300 (IsPathAccessible(path) == MagickFalse)))
1306 if ((IsSceneGeometry(q+1,MagickFalse) == MagickFalse) &&
1307 (IsGeometry(q+1) == MagickFalse))
1311 subimage_length=(size_t) (p-q);
1312 subimage_offset=(size_t) (q-component+1);
1316 if (literal != (
char *) NULL)
1317 literal=DestroyString(literal);
1318 exception=DestroyExceptionInfo(exception);
1325 for (p=component; *p !=
'\0'; p++)
1327 if ((*p ==
'%') && (*(p+1) ==
'['))
1332 for (p++; (*p !=
']') && (*p !=
'\0'); p++) ;
1336 if ((p != component) && (*p ==
':') && (IsPathDirectory(component) < 0) &&
1337 (IsPathAccessible(component) == MagickFalse))
1343 if (IsMagickConflict(component) != MagickFalse)
1347 magick_length=(size_t) (p-component+1);
1348 for (q=component; *(++p) !=
'\0'; q++)
1357 for (p=component+strlen(component)-1; p > component; p--)
1358 if (IsBasenameSeparator(*p) != MagickFalse)
1364 if (magick_length != 0)
1365 (void) CopyMagickString(component,path,magick_length);
1372 if (*component !=
'\0')
1374 for (p=component+(strlen(component)-1); p > component; p--)
1376 if (IsBasenameSeparator(*p) != MagickFalse)
1394 if (IsBasenameSeparator(*p) != MagickFalse)
1395 (void) CopyMagickString(component,p+1,MagickPathExtent);
1400 if (IsBasenameSeparator(*p) != MagickFalse)
1401 (void) CopyMagickString(component,p+1,MagickPathExtent);
1402 if (*component !=
'\0')
1403 for (p=component+(strlen(component)-1); p > component; p--)
1411 case BasePathSansCompressExtension:
1414 extension[MagickPathExtent];
1419 GetPathComponent(path,ExtensionPath,extension);
1420 if ((LocaleCompare(extension,
"bz2") == 0) ||
1421 (LocaleCompare(extension,
"gz") == 0) ||
1422 (LocaleCompare(extension,
"svgz") == 0) ||
1423 (LocaleCompare(extension,
"wmz") == 0) ||
1424 (LocaleCompare(extension,
"Z") == 0))
1425 GetPathComponent(path,BasePath,component);
1430 if (IsBasenameSeparator(*p) != MagickFalse)
1431 (void) CopyMagickString(component,p+1,MagickPathExtent);
1432 if (*component !=
'\0')
1433 for (p=component+strlen(component)-1; p > component; p--)
1438 (void) CopyMagickString(component,p+1,MagickPathExtent);
1444 if ((subimage_length != 0) && (magick_length < subimage_offset))
1445 (void) CopyMagickString(component,path+subimage_offset,subimage_length);
1448 case SubcanonicalPath:
1480MagickPrivate
char **GetPathComponents(
const char *path,
1481 size_t *number_components)
1493 if (path == (
char *) NULL)
1494 return((
char **) NULL);
1495 *number_components=1;
1496 for (p=path; *p !=
'\0'; p++)
1497 if (IsBasenameSeparator(*p))
1498 (*number_components)++;
1499 components=(
char **) AcquireQuantumMemory((
size_t) *number_components+1UL,
1500 sizeof(*components));
1501 if (components == (
char **) NULL)
1502 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1504 for (i=0; i < (ssize_t) *number_components; i++)
1506 for (q=p; *q !=
'\0'; q++)
1507 if (IsBasenameSeparator(*q))
1509 components[i]=(
char *) AcquireQuantumMemory((
size_t) (q-p)+MagickPathExtent,
1510 sizeof(**components));
1511 if (components[i] == (
char *) NULL)
1512 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1513 (void) CopyMagickString(components[i],p,(
size_t) (q-p+1));
1516 components[i]=(
char *) NULL;
1543MagickExport MagickBooleanType IsPathAccessible(
const char *path)
1551 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1552 return(MagickFalse);
1553 if (LocaleCompare(path,
"-") == 0)
1555 status=GetPathAttributes(path,&attributes);
1556 if (status == MagickFalse)
1558 if (S_ISREG(attributes.st_mode) == 0)
1559 return(MagickFalse);
1560 if (access_utf8(path,F_OK) != 0)
1561 return(MagickFalse);
1588static int IsPathDirectory(
const char *path)
1596 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1597 return(MagickFalse);
1598 status=GetPathAttributes(path,&attributes);
1599 if (status == MagickFalse)
1601 if (S_ISDIR(attributes.st_mode) == 0)
1641#if defined(__cplusplus) || defined(c_plusplus)
1645static int FileCompare(
const void *x,
const void *y)
1651 p=(
const char **) x;
1652 q=(
const char **) y;
1653 return(LocaleCompare(*p,*q));
1656#if defined(__cplusplus) || defined(c_plusplus)
1660MagickPrivate
char **ListFiles(
const char *directory,
const char *pattern,
1661 size_t *number_entries)
1679 assert(directory != (
const char *) NULL);
1680 assert(pattern != (
const char *) NULL);
1681 assert(number_entries != (
size_t *) NULL);
1682 if (IsEventLogging() != MagickFalse)
1683 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",directory);
1685 current_directory=opendir(directory);
1686 if (current_directory == (DIR *) NULL)
1687 return((
char **) NULL);
1692 filelist=(
char **) AcquireQuantumMemory((
size_t) max_entries,
1694 if (filelist == (
char **) NULL)
1696 (void) closedir(current_directory);
1697 return((
char **) NULL);
1702 buffer=(
struct dirent *) AcquireMagickMemory(
sizeof(*buffer)+FILENAME_MAX+1);
1703 if (buffer == (
struct dirent *) NULL)
1704 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1705 while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
1706 (entry != (
struct dirent *) NULL))
1708 if ((LocaleCompare(entry->d_name,
".") == 0) ||
1709 (LocaleCompare(entry->d_name,
"..") == 0))
1711 if ((IsPathDirectory(entry->d_name) > 0) ||
1712#
if defined(MAGICKCORE_WINDOWS_SUPPORT)
1713 (GlobExpression(entry->d_name,pattern,MagickTrue) != MagickFalse))
1715 (GlobExpression(entry->d_name,pattern,MagickFalse) != MagickFalse))
1718 if (*number_entries >= max_entries)
1724 filelist=(
char **) ResizeQuantumMemory(filelist,(
size_t)
1725 max_entries,
sizeof(*filelist));
1726 if (filelist == (
char **) NULL)
1734 p=strchr(entry->d_name,
';');
1737 if (*number_entries > 0)
1738 if (LocaleCompare(entry->d_name,filelist[*number_entries-1]) == 0)
1742 filelist[*number_entries]=(
char *) AcquireString(entry->d_name);
1743 (*number_entries)++;
1746 buffer=(
struct dirent *) RelinquishMagickMemory(buffer);
1747 (void) closedir(current_directory);
1748 if (filelist == (
char **) NULL)
1749 return((
char **) NULL);
1753 qsort((
void *) filelist,(
size_t) *number_entries,
sizeof(*filelist),
1782MagickExport
void MagickDelay(
const MagickSizeType milliseconds)
1784 if (milliseconds == 0)
1786#if defined(MAGICKCORE_HAVE_NANOSLEEP)
1791 timer.tv_sec=(time_t) (milliseconds/1000);
1792 timer.tv_nsec=(time_t) ((milliseconds % 1000)*1000*1000);
1793 (void) nanosleep(&timer,(
struct timespec *) NULL);
1795#elif defined(MAGICKCORE_HAVE_USLEEP)
1796 usleep(1000*milliseconds);
1797#elif defined(MAGICKCORE_HAVE_SELECT)
1802 timer.tv_sec=(long) milliseconds/1000;
1803 timer.tv_usec=(long) (milliseconds % 1000)*1000;
1804 (void) select(0,(XFD_SET *) NULL,(XFD_SET *) NULL,(XFD_SET *) NULL,&timer);
1806#elif defined(MAGICKCORE_HAVE_POLL)
1807 (void) poll((
struct pollfd *) NULL,0,(
int) milliseconds);
1808#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
1809 Sleep((
long) milliseconds);
1815 timer=milliseconds/1000.0;
1818#elif defined(__BEOS__)
1819 snooze(1000*milliseconds);
1825 time_end=clock()+milliseconds*CLOCKS_PER_SEC/1000;
1826 while (clock() < time_end)
1856MagickExport
size_t MultilineCensus(
const char *label)
1864 if (label == (
char *) NULL)
1866 for (number_lines=1; *label !=
'\0'; label++)
1869 return(number_lines);
1895MagickPrivate MagickBooleanType ShredFile(
const char *path)
1922 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1923 return(MagickFalse);
1930 property=GetEnvironmentValue(
"MAGICK_SHRED_PASSES");
1931 if (property != (
char *) NULL)
1933 passes=(ssize_t) StringToInteger(property);
1934 property=DestroyString(property);
1936 property=GetPolicyValue(
"system:shred");
1937 if (property != (
char *) NULL)
1939 passes=(ssize_t) StringToInteger(property);
1940 property=DestroyString(property);
1948 file=open_utf8(path,O_WRONLY | O_EXCL | O_BINARY,S_MODE);
1950 return(MagickFalse);
1951 quantum=(size_t) MagickMinBufferExtent;
1952 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1953 quantum=(size_t) MagickMin(file_stats.st_size,MagickMinBufferExtent);
1954 length=(MagickSizeType) file_stats.st_size;
1955 random_info=AcquireRandomInfo();
1956 key=GetRandomKey(random_info,quantum);
1957 for (i=0; i < passes; i++)
1965 if (lseek(file,0,SEEK_SET) < 0)
1967 for (j=0; j < (MagickOffsetType) length; j+=count)
1970 SetRandomKey(random_info,quantum,GetStringInfoDatum(key));
1971 count=write(file,GetStringInfoDatum(key),(
size_t)
1972 MagickMin((MagickOffsetType) quantum,(MagickOffsetType) length-j));
1980 if (j < (MagickOffsetType) length)
1983 key=DestroyStringInfo(key);
1984 random_info=DestroyRandomInfo(random_info);
1985 status=close_utf8(file);
1986 return((status == -1 || i < passes) ? MagickFalse : MagickTrue);