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,ReadPolicyRights |
187 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]=(q-Base64) << 2;
403 decode[i++]|=(q-Base64) >> 4;
404 decode[i]=((q-Base64) & 0x0f) << 4;
410 decode[i++]|=(q-Base64) >> 2;
411 decode[i]=((q-Base64) & 0x03) << 6;
417 decode[i++]|=(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);
756MagickExport MagickBooleanType ExpandFilenames(
int *number_arguments,
760 home_directory[MagickPathExtent],
777 assert(number_arguments != (
int *) NULL);
778 assert(arguments != (
char ***) NULL);
779 if (IsEventLogging() != MagickFalse)
780 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
781 vector=(
char **) AcquireQuantumMemory((
size_t) (*number_arguments+1),
783 if (vector == (
char **) NULL)
784 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
788 *home_directory=
'\0';
790 for (i=0; i < (ssize_t) *number_arguments; i++)
794 filename[MagickPathExtent],
795 magick[MagickPathExtent],
797 path[MagickPathExtent],
798 subimage[MagickPathExtent];
803 option=(*arguments)[i];
809 vector[count++]=ConstantString(option);
811 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
817 for (j=0; j < parameters; j++)
820 if (i == (ssize_t) *number_arguments)
822 option=(*arguments)[i];
823 vector[count++]=ConstantString(option);
827 if ((*option ==
'"') || (*option ==
'\''))
829 GetPathComponent(option,TailPath,filename);
830 GetPathComponent(option,MagickPath,magick);
831 if ((LocaleCompare(magick,
"CAPTION") == 0) ||
832 (LocaleCompare(magick,
"LABEL") == 0) ||
833 (LocaleCompare(magick,
"PANGO") == 0) ||
834 (LocaleCompare(magick,
"VID") == 0))
836 if ((IsGlob(filename) == MagickFalse) && (*option !=
'@'))
838 if (IsPathAccessible(option) != MagickFalse)
845 GetPathComponent(option,HeadPath,path);
846 GetPathComponent(option,SubimagePath,subimage);
847 ExpandFilename(path);
848 if (*home_directory ==
'\0')
849 getcwd_utf8(home_directory,MagickPathExtent-1);
850 filelist=ListFiles(*path ==
'\0' ? home_directory : path,filename,
867 exception=AcquireExceptionInfo();
868 files=FileToString(option,~0UL,exception);
869 exception=DestroyExceptionInfo(exception);
870 if (files == (
char *) NULL)
872 filelist=StringToArgv(files,&length);
873 if (filelist == (
char **) NULL)
875 files=DestroyString(files);
876 filelist[0]=DestroyString(filelist[0]);
877 for (j=0; j < (ssize_t) (length-1); j++)
878 filelist[j]=filelist[j+1];
879 number_files=(size_t) length-1;
881 if (filelist == (
char **) NULL)
883 for (j=0; j < (ssize_t) number_files; j++)
884 if (IsPathDirectory(filelist[j]) <= 0)
886 if (j == (ssize_t) number_files)
888 for (j=0; j < (ssize_t) number_files; j++)
889 filelist[j]=DestroyString(filelist[j]);
890 filelist=(
char **) RelinquishMagickMemory(filelist);
896 vector=(
char **) ResizeQuantumMemory(vector,(
size_t) ((ssize_t)
897 *number_arguments+count+(ssize_t) number_files+1),
sizeof(*vector));
898 if (vector == (
char **) NULL)
900 for (j=0; j < (ssize_t) number_files; j++)
901 filelist[j]=DestroyString(filelist[j]);
902 filelist=(
char **) RelinquishMagickMemory(filelist);
905 for (j=0; j < (ssize_t) number_files; j++)
908 parameters=ParseCommandOption(MagickCommandOptions,MagickFalse,option);
917 vector[count++]=ConstantString(option);
918 for (k=0; k < parameters; k++)
921 if (j == (ssize_t) number_files)
924 vector[count++]=ConstantString(option);
928 (void) CopyMagickString(filename,path,MagickPathExtent);
930 (void) ConcatenateMagickString(filename,DirectorySeparator,
932 if (filelist[j] != (
char *) NULL)
933 (
void) ConcatenateMagickString(filename,filelist[j],MagickPathExtent);
934 filelist[j]=DestroyString(filelist[j]);
935 if (strlen(filename) >= (MagickPathExtent-1))
936 ThrowFatalException(OptionFatalError,
"FilenameTruncated");
937 if (IsPathDirectory(filename) <= 0)
940 file_path[MagickPathExtent];
945 (void) ConcatenateMagickString(file_path,magick,
947 (void) ConcatenateMagickString(file_path,
":",MagickPathExtent);
949 (void) ConcatenateMagickString(file_path,filename,MagickPathExtent);
950 if (*subimage !=
'\0')
952 (void) ConcatenateMagickString(file_path,
"[",MagickPathExtent);
953 (void) ConcatenateMagickString(file_path,subimage,
955 (void) ConcatenateMagickString(file_path,
"]",MagickPathExtent);
957 if (strlen(file_path) >= (MagickPathExtent-1))
958 ThrowFatalException(OptionFatalError,
"FilenameTruncated");
959 if (destroy != MagickFalse)
962 vector[count]=DestroyString(vector[count]);
965 vector[count++]=ConstantString(file_path);
968 filelist=(
char **) RelinquishMagickMemory(filelist);
970 vector[count]=(
char *) NULL;
971 if (IsEventLogging() != MagickFalse)
976 command_line=AcquireString(vector[0]);
977 for (i=1; i < count; i++)
979 (void) ConcatenateString(&command_line,
" {");
980 (void) ConcatenateString(&command_line,vector[i]);
981 (void) ConcatenateString(&command_line,
"}");
983 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
984 "Command line: %s",command_line);
985 command_line=DestroyString(command_line);
987 *number_arguments=(int) count;
1017MagickPrivate MagickBooleanType GetExecutionPath(
char *path,
const size_t extent)
1023 directory=getcwd(path,(
unsigned long) extent);
1025#if defined(MAGICKCORE_HAVE_GETPID) && defined(MAGICKCORE_HAVE_READLINK) && defined(PATH_MAX)
1028 execution_path[PATH_MAX+1],
1029 link_path[MagickPathExtent];
1034 (void) FormatLocaleString(link_path,MagickPathExtent,
"/proc/%.20g/exe",
1036 count=readlink(link_path,execution_path,PATH_MAX);
1039 (void) FormatLocaleString(link_path,MagickPathExtent,
"/proc/%.20g/file",
1041 count=readlink(link_path,execution_path,PATH_MAX);
1043 if ((count > 0) && (count <= (ssize_t) PATH_MAX))
1045 execution_path[count]=
'\0';
1046 (void) CopyMagickString(path,execution_path,extent);
1050#if defined(MAGICKCORE_HAVE__NSGETEXECUTABLEPATH)
1053 executable_path[PATH_MAX << 1];
1058 length=
sizeof(executable_path);
1059 if (_NSGetExecutablePath(executable_path,&length) == 0)
1062 *real_path = realpath_utf8(executable_path);
1064 if (real_path != (
char *) NULL)
1066 (void) CopyMagickString(path,real_path,extent);
1067 real_path=DestroyString(real_path);
1072#if defined(MAGICKCORE_HAVE_GETEXECNAME)
1077 execution_path=(
const char *) getexecname();
1078 if (execution_path != (
const char *) NULL)
1080 if (*execution_path != *DirectorySeparator)
1081 (void) ConcatenateMagickString(path,DirectorySeparator,extent);
1082 (void) ConcatenateMagickString(path,execution_path,extent);
1086#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1087 NTGetExecutionPath(path,extent);
1098 program_name=program_invocation_name;
1099 if (*program_invocation_name !=
'/')
1104 extent=strlen(directory)+strlen(program_name)+2;
1105 program_name=AcquireQuantumMemory(extent,
sizeof(*program_name));
1106 if (program_name == (
char *) NULL)
1107 program_name=program_invocation_name;
1109 count=FormatLocaleString(program_name,extent,
"%s/%s",directory,
1110 program_invocation_name);
1115 *real_path = realpath_utf8(program_name);
1117 if (real_path != (
char *) NULL)
1119 (void) CopyMagickString(path,real_path,extent);
1120 real_path=DestroyString(real_path);
1123 if (program_name != program_invocation_name)
1124 program_name=(
char *) RelinquishMagickMemory(program_name);
1127#if defined(__OpenBSD__)
1132 (void) CopyMagickString(path,__progname,extent);
1135 return(IsPathAccessible(path));
1156MagickPrivate ssize_t GetMagickPageSize(
void)
1163#if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
1164 page_size=(ssize_t) sysconf(_SC_PAGE_SIZE);
1165#elif defined(MAGICKCORE_HAVE_GETPAGESIZE)
1166 page_size=(ssize_t) getpagesize();
1197MagickExport MagickBooleanType GetPathAttributes(
const char *path,
1203 if (path == (
const char *) NULL)
1206 return(MagickFalse);
1208 (void) memset(attributes,0,
sizeof(
struct stat));
1209 status=stat_utf8(path,(
struct stat *) attributes) == 0 ? MagickTrue :
1245MagickExport
void GetPathComponent(
const char *path,PathType type,
1259 assert(path != (
const char *) NULL);
1260 assert(component != (
char *) NULL);
1261 if (IsEventLogging() != MagickFalse)
1262 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",path);
1268 (void) CopyMagickString(component,path,MagickPathExtent);
1271 if (type != SubcanonicalPath)
1273 p=component+strlen(component)-1;
1274 if ((strlen(component) > 2) && (*p ==
']'))
1276 q=strrchr(component,
'[');
1277 if ((q != (
char *) NULL) && ((q == component) || (*(q-1) !=
']')) &&
1278 (IsPathAccessible(path) == MagickFalse))
1284 if ((IsSceneGeometry(q+1,MagickFalse) == MagickFalse) &&
1285 (IsGeometry(q+1) == MagickFalse))
1289 subimage_length=(size_t) (p-q);
1290 subimage_offset=(size_t) (q-component+1);
1300 for (p=component; *p !=
'\0'; p++)
1302 if ((*p ==
'%') && (*(p+1) ==
'['))
1307 for (p++; (*p !=
']') && (*p !=
'\0'); p++) ;
1311 if ((p != component) && (*p ==
':') && (IsPathDirectory(component) < 0) &&
1312 (IsPathAccessible(component) == MagickFalse))
1318 if (IsMagickConflict(component) != MagickFalse)
1322 magick_length=(size_t) (p-component+1);
1323 for (q=component; *(++p) !=
'\0'; q++)
1332 for (p=component+strlen(component)-1; p > component; p--)
1333 if (IsBasenameSeparator(*p) != MagickFalse)
1339 if (magick_length != 0)
1340 (void) CopyMagickString(component,path,magick_length);
1347 if (*component !=
'\0')
1349 for (p=component+(strlen(component)-1); p > component; p--)
1351 if (IsBasenameSeparator(*p) != MagickFalse)
1369 if (IsBasenameSeparator(*p) != MagickFalse)
1370 (
void) CopyMagickString(component,p+1,MagickPathExtent);
1375 if (IsBasenameSeparator(*p) != MagickFalse)
1376 (void) CopyMagickString(component,p+1,MagickPathExtent);
1377 if (*component !=
'\0')
1378 for (p=component+(strlen(component)-1); p > component; p--)
1386 case BasePathSansCompressExtension:
1389 extension[MagickPathExtent];
1394 GetPathComponent(path,ExtensionPath,extension);
1395 if ((LocaleCompare(extension,
"bz2") == 0) ||
1396 (LocaleCompare(extension,
"gz") == 0) ||
1397 (LocaleCompare(extension,
"svgz") == 0) ||
1398 (LocaleCompare(extension,
"wmz") == 0) ||
1399 (LocaleCompare(extension,
"Z") == 0))
1400 GetPathComponent(path,BasePath,component);
1405 if (IsBasenameSeparator(*p) != MagickFalse)
1406 (void) CopyMagickString(component,p+1,MagickPathExtent);
1407 if (*component !=
'\0')
1408 for (p=component+strlen(component)-1; p > component; p--)
1413 (void) CopyMagickString(component,p+1,MagickPathExtent);
1419 if ((subimage_length != 0) && (magick_length < subimage_offset))
1420 (void) CopyMagickString(component,path+subimage_offset,subimage_length);
1423 case SubcanonicalPath:
1455MagickPrivate
char **GetPathComponents(
const char *path,
1456 size_t *number_components)
1468 if (path == (
char *) NULL)
1469 return((
char **) NULL);
1470 *number_components=1;
1471 for (p=path; *p !=
'\0'; p++)
1472 if (IsBasenameSeparator(*p))
1473 (*number_components)++;
1474 components=(
char **) AcquireQuantumMemory((
size_t) *number_components+1UL,
1475 sizeof(*components));
1476 if (components == (
char **) NULL)
1477 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1479 for (i=0; i < (ssize_t) *number_components; i++)
1481 for (q=p; *q !=
'\0'; q++)
1482 if (IsBasenameSeparator(*q))
1484 components[i]=(
char *) AcquireQuantumMemory((
size_t) (q-p)+MagickPathExtent,
1485 sizeof(**components));
1486 if (components[i] == (
char *) NULL)
1487 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1488 (void) CopyMagickString(components[i],p,(
size_t) (q-p+1));
1491 components[i]=(
char *) NULL;
1518MagickExport MagickBooleanType IsPathAccessible(
const char *path)
1526 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1527 return(MagickFalse);
1528 if (LocaleCompare(path,
"-") == 0)
1530 status=GetPathAttributes(path,&attributes);
1531 if (status == MagickFalse)
1533 if (S_ISREG(attributes.st_mode) == 0)
1534 return(MagickFalse);
1535 if (access_utf8(path,F_OK) != 0)
1536 return(MagickFalse);
1563static int IsPathDirectory(
const char *path)
1571 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1572 return(MagickFalse);
1573 status=GetPathAttributes(path,&attributes);
1574 if (status == MagickFalse)
1576 if (S_ISDIR(attributes.st_mode) == 0)
1616#if defined(__cplusplus) || defined(c_plusplus)
1620static int FileCompare(
const void *x,
const void *y)
1626 p=(
const char **) x;
1627 q=(
const char **) y;
1628 return(LocaleCompare(*p,*q));
1631#if defined(__cplusplus) || defined(c_plusplus)
1635MagickPrivate
char **ListFiles(
const char *directory,
const char *pattern,
1636 size_t *number_entries)
1654 assert(directory != (
const char *) NULL);
1655 assert(pattern != (
const char *) NULL);
1656 assert(number_entries != (
size_t *) NULL);
1657 if (IsEventLogging() != MagickFalse)
1658 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",directory);
1660 current_directory=opendir(directory);
1661 if (current_directory == (DIR *) NULL)
1662 return((
char **) NULL);
1667 filelist=(
char **) AcquireQuantumMemory((
size_t) max_entries,
1669 if (filelist == (
char **) NULL)
1671 (void) closedir(current_directory);
1672 return((
char **) NULL);
1677 buffer=(
struct dirent *) AcquireMagickMemory(
sizeof(*buffer)+FILENAME_MAX+1);
1678 if (buffer == (
struct dirent *) NULL)
1679 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1680 while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
1681 (entry != (
struct dirent *) NULL))
1683 if ((LocaleCompare(entry->d_name,
".") == 0) ||
1684 (LocaleCompare(entry->d_name,
"..") == 0))
1686 if ((IsPathDirectory(entry->d_name) > 0) ||
1687#
if defined(MAGICKCORE_WINDOWS_SUPPORT)
1688 (GlobExpression(entry->d_name,pattern,MagickTrue) != MagickFalse))
1690 (GlobExpression(entry->d_name,pattern,MagickFalse) != MagickFalse))
1693 if (*number_entries >= max_entries)
1699 filelist=(
char **) ResizeQuantumMemory(filelist,(
size_t)
1700 max_entries,
sizeof(*filelist));
1701 if (filelist == (
char **) NULL)
1709 p=strchr(entry->d_name,
';');
1712 if (*number_entries > 0)
1713 if (LocaleCompare(entry->d_name,filelist[*number_entries-1]) == 0)
1717 filelist[*number_entries]=(
char *) AcquireString(entry->d_name);
1718 (*number_entries)++;
1721 buffer=(
struct dirent *) RelinquishMagickMemory(buffer);
1722 (void) closedir(current_directory);
1723 if (filelist == (
char **) NULL)
1724 return((
char **) NULL);
1728 qsort((
void *) filelist,(
size_t) *number_entries,
sizeof(*filelist),
1757MagickExport
void MagickDelay(
const MagickSizeType milliseconds)
1759 if (milliseconds == 0)
1761#if defined(MAGICKCORE_HAVE_NANOSLEEP)
1766 timer.tv_sec=(time_t) (milliseconds/1000);
1767 timer.tv_nsec=(time_t) ((milliseconds % 1000)*1000*1000);
1768 (void) nanosleep(&timer,(
struct timespec *) NULL);
1770#elif defined(MAGICKCORE_HAVE_USLEEP)
1771 usleep(1000*milliseconds);
1772#elif defined(MAGICKCORE_HAVE_SELECT)
1777 timer.tv_sec=(long) milliseconds/1000;
1778 timer.tv_usec=(long) (milliseconds % 1000)*1000;
1779 (void) select(0,(XFD_SET *) NULL,(XFD_SET *) NULL,(XFD_SET *) NULL,&timer);
1781#elif defined(MAGICKCORE_HAVE_POLL)
1782 (void) poll((
struct pollfd *) NULL,0,(int) milliseconds);
1783#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
1784 Sleep((
long) milliseconds);
1790 timer=milliseconds/1000.0;
1793#elif defined(__BEOS__)
1794 snooze(1000*milliseconds);
1800 time_end=clock()+milliseconds*CLOCKS_PER_SEC/1000;
1801 while (clock() < time_end)
1831MagickExport
size_t MultilineCensus(
const char *label)
1839 if (label == (
char *) NULL)
1841 for (number_lines=1; *label !=
'\0'; label++)
1844 return(number_lines);
1870MagickPrivate MagickBooleanType ShredFile(
const char *path)
1897 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1898 return(MagickFalse);
1905 property=GetEnvironmentValue(
"MAGICK_SHRED_PASSES");
1906 if (property != (
char *) NULL)
1908 passes=(ssize_t) StringToInteger(property);
1909 property=DestroyString(property);
1911 property=GetPolicyValue(
"system:shred");
1912 if (property != (
char *) NULL)
1914 passes=(ssize_t) StringToInteger(property);
1915 property=DestroyString(property);
1923 file=open_utf8(path,O_WRONLY | O_EXCL | O_BINARY,S_MODE);
1925 return(MagickFalse);
1926 quantum=(size_t) MagickMinBufferExtent;
1927 if ((fstat(file,&file_stats) == 0) && (file_stats.st_size > 0))
1928 quantum=(size_t) MagickMin(file_stats.st_size,MagickMinBufferExtent);
1929 length=(MagickSizeType) file_stats.st_size;
1930 random_info=AcquireRandomInfo();
1931 key=GetRandomKey(random_info,quantum);
1932 for (i=0; i < passes; i++)
1940 if (lseek(file,0,SEEK_SET) < 0)
1942 for (j=0; j < (MagickOffsetType) length; j+=count)
1945 SetRandomKey(random_info,quantum,GetStringInfoDatum(key));
1946 count=write(file,GetStringInfoDatum(key),(
size_t)
1947 MagickMin((MagickOffsetType) quantum,(MagickOffsetType) length-j));
1955 if (j < (MagickOffsetType) length)
1958 key=DestroyStringInfo(key);
1959 random_info=DestroyRandomInfo(random_info);
1960 status=close_utf8(file);
1961 return((status == -1 || i < passes) ? MagickFalse : MagickTrue);