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 q=strrchr(component,
'[');
1295 if ((q != (
char *) NULL) && ((q == component) || (*(q-1) !=
']')) &&
1296 (IsPathAccessible(path) == MagickFalse))
1302 if ((IsSceneGeometry(q+1,MagickFalse) == MagickFalse) &&
1303 (IsGeometry(q+1) == MagickFalse))
1307 subimage_length=(size_t) (p-q);
1308 subimage_offset=(size_t) (q-component+1);
1318 for (p=component; *p !=
'\0'; p++)
1320 if ((*p ==
'%') && (*(p+1) ==
'['))
1325 for (p++; (*p !=
']') && (*p !=
'\0'); p++) ;
1329 if ((p != component) && (*p ==
':') && (IsPathDirectory(component) < 0) &&
1330 (IsPathAccessible(component) == MagickFalse))
1336 if (IsMagickConflict(component) != MagickFalse)
1340 magick_length=(size_t) (p-component+1);
1341 for (q=component; *(++p) !=
'\0'; q++)
1350 for (p=component+strlen(component)-1; p > component; p--)
1351 if (IsBasenameSeparator(*p) != MagickFalse)
1357 if (magick_length != 0)
1358 (void) CopyMagickString(component,path,magick_length);
1365 if (*component !=
'\0')
1367 for (p=component+(strlen(component)-1); p > component; p--)
1369 if (IsBasenameSeparator(*p) != MagickFalse)
1387 if (IsBasenameSeparator(*p) != MagickFalse)
1388 (void) CopyMagickString(component,p+1,MagickPathExtent);
1393 if (IsBasenameSeparator(*p) != MagickFalse)
1394 (void) CopyMagickString(component,p+1,MagickPathExtent);
1395 if (*component !=
'\0')
1396 for (p=component+(strlen(component)-1); p > component; p--)
1404 case BasePathSansCompressExtension:
1407 extension[MagickPathExtent];
1412 GetPathComponent(path,ExtensionPath,extension);
1413 if ((LocaleCompare(extension,
"bz2") == 0) ||
1414 (LocaleCompare(extension,
"gz") == 0) ||
1415 (LocaleCompare(extension,
"svgz") == 0) ||
1416 (LocaleCompare(extension,
"wmz") == 0) ||
1417 (LocaleCompare(extension,
"Z") == 0))
1418 GetPathComponent(path,BasePath,component);
1423 if (IsBasenameSeparator(*p) != MagickFalse)
1424 (void) CopyMagickString(component,p+1,MagickPathExtent);
1425 if (*component !=
'\0')
1426 for (p=component+strlen(component)-1; p > component; p--)
1431 (void) CopyMagickString(component,p+1,MagickPathExtent);
1437 if ((subimage_length != 0) && (magick_length < subimage_offset))
1438 (void) CopyMagickString(component,path+subimage_offset,subimage_length);
1441 case SubcanonicalPath:
1473MagickPrivate
char **GetPathComponents(
const char *path,
1474 size_t *number_components)
1486 if (path == (
char *) NULL)
1487 return((
char **) NULL);
1488 *number_components=1;
1489 for (p=path; *p !=
'\0'; p++)
1490 if (IsBasenameSeparator(*p))
1491 (*number_components)++;
1492 components=(
char **) AcquireQuantumMemory((
size_t) *number_components+1UL,
1493 sizeof(*components));
1494 if (components == (
char **) NULL)
1495 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1497 for (i=0; i < (ssize_t) *number_components; i++)
1499 for (q=p; *q !=
'\0'; q++)
1500 if (IsBasenameSeparator(*q))
1502 components[i]=(
char *) AcquireQuantumMemory((
size_t) (q-p)+MagickPathExtent,
1503 sizeof(**components));
1504 if (components[i] == (
char *) NULL)
1505 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1506 (void) CopyMagickString(components[i],p,(
size_t) (q-p+1));
1509 components[i]=(
char *) NULL;
1536MagickExport MagickBooleanType IsPathAccessible(
const char *path)
1544 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1545 return(MagickFalse);
1546 if (LocaleCompare(path,
"-") == 0)
1548 status=GetPathAttributes(path,&attributes);
1549 if (status == MagickFalse)
1551 if (S_ISREG(attributes.st_mode) == 0)
1552 return(MagickFalse);
1553 if (access_utf8(path,F_OK) != 0)
1554 return(MagickFalse);
1581static int IsPathDirectory(
const char *path)
1589 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1590 return(MagickFalse);
1591 status=GetPathAttributes(path,&attributes);
1592 if (status == MagickFalse)
1594 if (S_ISDIR(attributes.st_mode) == 0)
1634#if defined(__cplusplus) || defined(c_plusplus)
1638static int FileCompare(
const void *x,
const void *y)
1644 p=(
const char **) x;
1645 q=(
const char **) y;
1646 return(LocaleCompare(*p,*q));
1649#if defined(__cplusplus) || defined(c_plusplus)
1653MagickPrivate
char **ListFiles(
const char *directory,
const char *pattern,
1654 size_t *number_entries)
1672 assert(directory != (
const char *) NULL);
1673 assert(pattern != (
const char *) NULL);
1674 assert(number_entries != (
size_t *) NULL);
1675 if (IsEventLogging() != MagickFalse)
1676 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",directory);
1678 current_directory=opendir(directory);
1679 if (current_directory == (DIR *) NULL)
1680 return((
char **) NULL);
1685 filelist=(
char **) AcquireQuantumMemory((
size_t) max_entries,
1687 if (filelist == (
char **) NULL)
1689 (void) closedir(current_directory);
1690 return((
char **) NULL);
1695 buffer=(
struct dirent *) AcquireMagickMemory(
sizeof(*buffer)+FILENAME_MAX+1);
1696 if (buffer == (
struct dirent *) NULL)
1697 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1698 while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
1699 (entry != (
struct dirent *) NULL))
1701 if ((LocaleCompare(entry->d_name,
".") == 0) ||
1702 (LocaleCompare(entry->d_name,
"..") == 0))
1704 if ((IsPathDirectory(entry->d_name) > 0) ||
1705#
if defined(MAGICKCORE_WINDOWS_SUPPORT)
1706 (GlobExpression(entry->d_name,pattern,MagickTrue) != MagickFalse))
1708 (GlobExpression(entry->d_name,pattern,MagickFalse) != MagickFalse))
1711 if (*number_entries >= max_entries)
1717 filelist=(
char **) ResizeQuantumMemory(filelist,(
size_t)
1718 max_entries,
sizeof(*filelist));
1719 if (filelist == (
char **) NULL)
1727 p=strchr(entry->d_name,
';');
1730 if (*number_entries > 0)
1731 if (LocaleCompare(entry->d_name,filelist[*number_entries-1]) == 0)
1735 filelist[*number_entries]=(
char *) AcquireString(entry->d_name);
1736 (*number_entries)++;
1739 buffer=(
struct dirent *) RelinquishMagickMemory(buffer);
1740 (void) closedir(current_directory);
1741 if (filelist == (
char **) NULL)
1742 return((
char **) NULL);
1746 qsort((
void *) filelist,(
size_t) *number_entries,
sizeof(*filelist),
1775MagickExport
void MagickDelay(
const MagickSizeType milliseconds)
1777 if (milliseconds == 0)
1779#if defined(MAGICKCORE_HAVE_NANOSLEEP)
1784 timer.tv_sec=(time_t) (milliseconds/1000);
1785 timer.tv_nsec=(time_t) ((milliseconds % 1000)*1000*1000);
1786 (void) nanosleep(&timer,(
struct timespec *) NULL);
1788#elif defined(MAGICKCORE_HAVE_USLEEP)
1789 usleep(1000*milliseconds);
1790#elif defined(MAGICKCORE_HAVE_SELECT)
1795 timer.tv_sec=(long) milliseconds/1000;
1796 timer.tv_usec=(long) (milliseconds % 1000)*1000;
1797 (void) select(0,(XFD_SET *) NULL,(XFD_SET *) NULL,(XFD_SET *) NULL,&timer);
1799#elif defined(MAGICKCORE_HAVE_POLL)
1800 (void) poll((
struct pollfd *) NULL,0,(
int) milliseconds);
1801#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
1802 Sleep((
long) milliseconds);
1808 timer=milliseconds/1000.0;
1811#elif defined(__BEOS__)
1812 snooze(1000*milliseconds);
1818 time_end=clock()+milliseconds*CLOCKS_PER_SEC/1000;
1819 while (clock() < time_end)
1849MagickExport
size_t MultilineCensus(
const char *label)
1857 if (label == (
char *) NULL)
1859 for (number_lines=1; *label !=
'\0'; label++)
1862 return(number_lines);
1888MagickPrivate MagickBooleanType ShredFile(
const char *path)
1915 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1916 return(MagickFalse);
1923 property=GetEnvironmentValue(
"MAGICK_SHRED_PASSES");
1924 if (property != (
char *) NULL)
1926 passes=(ssize_t) StringToInteger(property);
1927 property=DestroyString(property);
1929 property=GetPolicyValue(
"system:shred");
1930 if (property != (
char *) NULL)
1932 passes=(ssize_t) StringToInteger(property);
1933 property=DestroyString(property);
1941 file=open_utf8(path,O_WRONLY | O_EXCL | O_BINARY,S_MODE);
1943 return(MagickFalse);
1944 quantum=(size_t) MagickMinBufferExtent;
1945 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1946 quantum=(size_t) MagickMin(file_stats.st_size,MagickMinBufferExtent);
1947 length=(MagickSizeType) file_stats.st_size;
1948 random_info=AcquireRandomInfo();
1949 key=GetRandomKey(random_info,quantum);
1950 for (i=0; i < passes; i++)
1958 if (lseek(file,0,SEEK_SET) < 0)
1960 for (j=0; j < (MagickOffsetType) length; j+=count)
1963 SetRandomKey(random_info,quantum,GetStringInfoDatum(key));
1964 count=write(file,GetStringInfoDatum(key),(
size_t)
1965 MagickMin((MagickOffsetType) quantum,(MagickOffsetType) length-j));
1973 if (j < (MagickOffsetType) length)
1976 key=DestroyStringInfo(key);
1977 random_info=DestroyRandomInfo(random_info);
1978 status=close_utf8(file);
1979 return((status == -1 || i < passes) ? MagickFalse : MagickTrue);