42#include "magick/studio.h"
43#include "magick/property.h"
44#include "magick/blob.h"
45#include "magick/color.h"
46#include "magick/exception.h"
47#include "magick/exception-private.h"
48#include "magick/geometry.h"
49#include "magick/image-private.h"
50#include "magick/list.h"
51#include "magick/log.h"
52#include "magick/memory_.h"
53#include "magick/nt-base-private.h"
54#include "magick/option.h"
55#include "magick/policy.h"
56#include "magick/policy-private.h"
57#include "magick/random_.h"
58#include "magick/registry.h"
59#include "magick/resource_.h"
60#include "magick/semaphore.h"
61#include "magick/signature-private.h"
62#include "magick/statistic.h"
63#include "magick/string_.h"
64#include "magick/string-private.h"
65#include "magick/token.h"
66#include "magick/utility.h"
67#include "magick/utility-private.h"
68#if defined(MAGICKCORE_HAVE_PROCESS_H)
71#if defined(MAGICKCORE_HAVE_MACH_O_DYLD_H)
72#include <mach-o/dyld.h>
79 Base64[] =
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
85 IsPathDirectory(
const char *);
110MagickExport MagickBooleanType AcquireUniqueFilename(
char *path)
115 file=AcquireUniqueFileResource(path);
118 file=close_utf8(file)-1;
152MagickExport MagickBooleanType AcquireUniqueSymbolicLink(
const char *source,
175 assert(source != (
const char *) NULL);
176 assert(destination != (
char *) NULL);
177#if defined(MAGICKCORE_HAVE_SYMLINK)
185 status=IsRightsAuthorizedByName(SystemPolicyDomain,
"symlink",(PolicyRights)
186 (ReadPolicyRights | WritePolicyRights),
"follow");
187 passes=GetPolicyValue(
"system:shred");
188 if ((passes != (
char *) NULL) || (status == MagickFalse))
189 passes=DestroyString(passes);
192 (void) AcquireUniqueFilename(destination);
193 (void) RelinquishUniqueFileResource(destination);
194 if (*source == *DirectorySeparator)
196 if (symlink(source,destination) == 0)
205 if (getcwd(path,MaxTextExtent) == (
char *) NULL)
207 (void) ConcatenateMagickString(path,DirectorySeparator,
209 (void) ConcatenateMagickString(path,source,MaxTextExtent);
210 if (symlink(path,destination) == 0)
219 destination_file=AcquireUniqueFileResource(destination);
220 if (destination_file == -1)
222 source_file=open_utf8(source,O_RDONLY | O_BINARY,0);
223 if (source_file == -1)
225 (void) close(destination_file);
226 (void) RelinquishUniqueFileResource(destination);
229 quantum=(size_t) MagickMaxBufferExtent;
230 if ((fstat(source_file,&attributes) == 0) && (attributes.st_size > 0))
231 quantum=(size_t) MagickMin(attributes.st_size,MagickMaxBufferExtent);
232 buffer=(
unsigned char *) AcquireQuantumMemory(quantum,
sizeof(*buffer));
233 if (buffer == (
unsigned char *) NULL)
235 (void) close(source_file);
236 (void) close(destination_file);
237 (void) RelinquishUniqueFileResource(destination);
243 count=(ssize_t) read(source_file,buffer,quantum);
246 length=(size_t) count;
247 count=(ssize_t) write(destination_file,buffer,length);
248 if ((
size_t) count != length)
250 (void) RelinquishUniqueFileResource(destination);
255 (void) close(destination_file);
256 (void) close(source_file);
257 buffer=(
unsigned char *) RelinquishMagickMemory(buffer);
288MagickExport
void AppendImageFormat(
const char *format,
char *filename)
291 extension[MaxTextExtent],
294 assert(format != (
char *) NULL);
295 assert(filename != (
char *) NULL);
296 if (IsEventLogging() != MagickFalse)
297 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",filename);
298 if ((*format ==
'\0') || (*filename ==
'\0'))
300 if (LocaleCompare(filename,
"-") == 0)
303 message[MaxTextExtent];
305 (void) FormatLocaleString(message,MaxTextExtent,
"%s:%s",format,filename);
306 (void) CopyMagickString(filename,message,MaxTextExtent);
309 GetPathComponent(filename,ExtensionPath,extension);
310 if ((LocaleCompare(extension,
"Z") == 0) ||
311 (LocaleCompare(extension,
"bz2") == 0) ||
312 (LocaleCompare(extension,
"gz") == 0) ||
313 (LocaleCompare(extension,
"wmz") == 0) ||
314 (LocaleCompare(extension,
"svgz") == 0))
316 GetPathComponent(filename,RootPath,root);
317 (void) CopyMagickString(filename,root,MaxTextExtent);
318 GetPathComponent(filename,RootPath,root);
319 (void) FormatLocaleString(filename,MaxTextExtent,
"%s.%s.%s",root,format,
323 GetPathComponent(filename,RootPath,root);
324 (void) FormatLocaleString(filename,MaxTextExtent,
"%s.%s",root,format);
353MagickExport
unsigned char *Base64Decode(
const char *source,
size_t *length)
368 assert(source != (
char *) NULL);
369 assert(length != (
size_t *) NULL);
370 if (IsEventLogging() != MagickFalse)
371 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
373 decode=(
unsigned char *) AcquireQuantumMemory((strlen(source)+3)/4,
375 if (decode == (
unsigned char *) NULL)
376 return((
unsigned char *) NULL);
379 for (p=source; *p !=
'\0'; p++)
381 if (isspace((
int) ((
unsigned char) *p)) != 0)
386 if (q == (
char *) NULL)
388 decode=(
unsigned char *) RelinquishMagickMemory(decode);
389 return((
unsigned char *) NULL);
395 decode[i]=(q-Base64) << 2;
401 decode[i++]|=(q-Base64) >> 4;
402 decode[i]=((q-Base64) & 0x0f) << 4;
408 decode[i++]|=(q-Base64) >> 2;
409 decode[i]=((q-Base64) & 0x03) << 6;
415 decode[i++]|=(q-Base64);
428 decode=(
unsigned char *) RelinquishMagickMemory(decode);
429 return((
unsigned char *) NULL);
443 decode=(
unsigned char *) RelinquishMagickMemory(decode);
444 return((
unsigned char *) NULL);
448 for ( ; *p !=
'\0'; p++)
449 if (isspace((
int) ((
unsigned char) *p)) == 0)
453 decode=(
unsigned char *) RelinquishMagickMemory(decode);
454 return((
unsigned char *) NULL);
460 for ( ; *p !=
'\0'; p++)
461 if (isspace((
int) ((
unsigned char) *p)) == 0)
463 decode=(
unsigned char *) RelinquishMagickMemory(decode);
464 return((
unsigned char *) NULL);
466 if ((
int) decode[i] != 0)
468 decode=(
unsigned char *) RelinquishMagickMemory(decode);
469 return((
unsigned char *) NULL);
509MagickExport
char *Base64Encode(
const unsigned char *blob,
510 const size_t blob_length,
size_t *encode_length)
524 assert(blob != (
const unsigned char *) NULL);
525 assert(blob_length != 0);
526 assert(encode_length != (
size_t *) NULL);
527 if (IsEventLogging() != MagickFalse)
528 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"...");
530 encode=(
char *) AcquireQuantumMemory(blob_length/3+4,4*
sizeof(*encode));
531 if (encode == (
char *) NULL)
532 return((
char *) NULL);
534 for (p=blob; p < (blob+blob_length-2); p+=(ptrdiff_t) 3)
536 encode[i++]=Base64[(int) (*p >> 2)];
537 encode[i++]=Base64[(int) (((*p & 0x03) << 4)+(*(p+1) >> 4))];
538 encode[i++]=Base64[(int) (((*(p+1) & 0x0f) << 2)+(*(p+2) >> 6))];
539 encode[i++]=Base64[(int) (*(p+2) & 0x3f)];
541 remainder=blob_length % 3;
553 for (j=0; j < (ssize_t) remainder; j++)
555 encode[i++]=Base64[(int) (code[0] >> 2)];
556 encode[i++]=Base64[(int) (((code[0] & 0x03) << 4)+(code[1] >> 4))];
560 encode[i++]=Base64[(int) (((code[1] & 0x0f) << 2)+(code[2] >> 6))];
593MagickExport
void ChopPathComponents(
char *path,
const size_t components)
598 for (i=0; i < (ssize_t) components; i++)
599 GetPathComponent(path,HeadPath,path);
625MagickExport
void ExpandFilename(
char *path)
628 expand_path[MaxTextExtent];
630 if (path == (
char *) NULL)
634 (void) CopyMagickString(expand_path,path,MaxTextExtent);
635 if ((*(path+1) == *DirectorySeparator) || (*(path+1) ==
'\0'))
643 (void) CopyMagickString(expand_path,
".",MaxTextExtent);
644 (void) ConcatenateMagickString(expand_path,path+1,MaxTextExtent);
645 home=GetEnvironmentValue(
"HOME");
646 if (home == (
char *) NULL)
647 home=GetEnvironmentValue(
"USERPROFILE");
648 if (home != (
char *) NULL)
650 (void) CopyMagickString(expand_path,home,MaxTextExtent);
651 (void) ConcatenateMagickString(expand_path,path+1,MaxTextExtent);
652 home=DestroyString(home);
657#if defined(MAGICKCORE_POSIX_SUPPORT) && !defined(__OS2__)
659#if defined(MAGICKCORE_HAVE_GETPWNAM_R)
660 buffer[MagickPathExtent],
662 username[MaxTextExtent];
673 (void) CopyMagickString(username,path+1,MaxTextExtent);
674 p=strchr(username,
'/');
675 if (p != (
char *) NULL)
677#if !defined(MAGICKCORE_HAVE_GETPWNAM_R)
678 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,MaxTextExtent);
690 if (p != (
char *) NULL)
692 (void) ConcatenateMagickString(expand_path,
"/",MaxTextExtent);
693 (void) ConcatenateMagickString(expand_path,p+1,MaxTextExtent);
697 (void) CopyMagickString(path,expand_path,MaxTextExtent);
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[MaxTextExtent];
768 (void) _wgetcwd(wide_path,MaxTextExtent-1);
769 (void) WideCharToMultiByte(CP_UTF8,0,wide_path,-1,path,(
int) extent,NULL,NULL);
773MagickExport MagickBooleanType ExpandFilenames(
int *number_arguments,
777 home_directory[MaxTextExtent],
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[MaxTextExtent],
812 magick[MaxTextExtent],
815 subimage[MaxTextExtent];
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,MaxTextExtent-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) *number_arguments+
914 count+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,MaxTextExtent);
947 (void) ConcatenateMagickString(filename,DirectorySeparator,
949 if (filelist[j] != (
char *) NULL)
950 (void) ConcatenateMagickString(filename,filelist[j],MaxTextExtent);
951 filelist[j]=DestroyString(filelist[j]);
952 if (strlen(filename) >= (MaxTextExtent-1))
953 ThrowFatalException(OptionFatalError,
"FilenameTruncated");
954 if (IsPathDirectory(filename) <= 0)
962 (void) ConcatenateMagickString(path,magick,MaxTextExtent);
963 (void) ConcatenateMagickString(path,
":",MaxTextExtent);
965 (void) ConcatenateMagickString(path,filename,MaxTextExtent);
966 if (*subimage !=
'\0')
968 (void) ConcatenateMagickString(path,
"[",MaxTextExtent);
969 (void) ConcatenateMagickString(path,subimage,MaxTextExtent);
970 (void) ConcatenateMagickString(path,
"]",MaxTextExtent);
972 if (strlen(path) >= (MaxTextExtent-1))
973 ThrowFatalException(OptionFatalError,
"FilenameTruncated");
974 if (destroy != MagickFalse)
977 vector[count]=DestroyString(vector[count]);
980 vector[count++]=ConstantString(path);
983 filelist=(
char **) RelinquishMagickMemory(filelist);
985 vector[count]=(
char *) NULL;
986 if (IsEventLogging() != MagickFalse)
991 command_line=AcquireString(vector[0]);
992 for (i=1; i < count; i++)
994 (void) ConcatenateString(&command_line,
" {");
995 (void) ConcatenateString(&command_line,vector[i]);
996 (void) ConcatenateString(&command_line,
"}");
998 (void) LogMagickEvent(ConfigureEvent,GetMagickModule(),
999 "Command line: %s",command_line);
1000 command_line=DestroyString(command_line);
1002 *number_arguments=(int) count;
1032MagickExport MagickBooleanType GetExecutionPath(
char *path,
const size_t extent)
1038 directory=getcwd(path,(
unsigned long) extent);
1040#if defined(MAGICKCORE_HAVE_GETPID) && defined(MAGICKCORE_HAVE_READLINK) && defined(PATH_MAX)
1043 link_path[MaxTextExtent],
1044 execution_path[PATH_MAX+1];
1049 (void) FormatLocaleString(link_path,MaxTextExtent,
"/proc/%.20g/exe",
1051 count=readlink(link_path,execution_path,PATH_MAX);
1054 (void) FormatLocaleString(link_path,MaxTextExtent,
"/proc/%.20g/file",
1056 count=readlink(link_path,execution_path,PATH_MAX);
1058 if ((count > 0) && (count <= (ssize_t) PATH_MAX))
1060 execution_path[count]=
'\0';
1061 (void) CopyMagickString(path,execution_path,extent);
1065#if defined(MAGICKCORE_HAVE__NSGETEXECUTABLEPATH)
1068 executable_path[PATH_MAX << 1];
1073 length=
sizeof(executable_path);
1074 if (_NSGetExecutablePath(executable_path,&length) == 0)
1077 *real_path = realpath_utf8(executable_path);
1079 if (real_path != (
char *) NULL)
1081 (void) CopyMagickString(path,real_path,extent);
1082 real_path=DestroyString(real_path);
1087#if defined(MAGICKCORE_HAVE_GETEXECNAME)
1092 execution_path=(
const char *) getexecname();
1093 if (execution_path != (
const char *) NULL)
1095 if (*execution_path != *DirectorySeparator)
1096 (void) ConcatenateMagickString(path,DirectorySeparator,extent);
1097 (void) ConcatenateMagickString(path,execution_path,extent);
1101#if defined(MAGICKCORE_WINDOWS_SUPPORT)
1102 NTGetExecutionPath(path,extent);
1113 program_name=program_invocation_name;
1114 if (*program_invocation_name !=
'/')
1119 extent=strlen(directory)+strlen(program_name)+2;
1120 program_name=AcquireQuantumMemory(extent,
sizeof(*program_name));
1121 if (program_name == (
char *) NULL)
1122 program_name=program_invocation_name;
1124 count=FormatLocaleString(program_name,extent,
"%s/%s",directory,
1125 program_invocation_name);
1130 *real_path = realpath_utf8(program_name);
1132 if (real_path != (
char *) NULL)
1134 (void) CopyMagickString(path,real_path,extent);
1135 real_path=DestroyString(real_path);
1138 if (program_name != program_invocation_name)
1139 program_name=(
char *) RelinquishMagickMemory(program_name);
1142#if defined(__OpenBSD__)
1147 (void) CopyMagickString(path,__progname,extent);
1150 return(IsPathAccessible(path));
1171MagickExport ssize_t GetMagickPageSize(
void)
1178#if defined(MAGICKCORE_HAVE_SYSCONF) && defined(_SC_PAGE_SIZE)
1179 page_size=(ssize_t) sysconf(_SC_PAGE_SIZE);
1180#elif defined(MAGICKCORE_HAVE_GETPAGESIZE)
1181 page_size=(ssize_t) getpagesize();
1212MagickExport MagickBooleanType GetPathAttributes(
const char *path,
1218 if (path == (
const char *) NULL)
1221 return(MagickFalse);
1223 (void) memset(attributes,0,
sizeof(
struct stat));
1224 status=stat_utf8(path,(
struct stat *) attributes) == 0 ? MagickTrue :
1260MagickExport
void GetPathComponent(
const char *path,PathType type,
1274 assert(path != (
const char *) NULL);
1275 assert(component != (
char *) NULL);
1276 if (IsEventLogging() != MagickFalse)
1277 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",path);
1283 (void) CopyMagickString(component,path,MagickPathExtent);
1286 if (type != SubcanonicalPath)
1288 p=component+strlen(component)-1;
1289 if ((strlen(component) > 2) && (*p ==
']'))
1291 q=strrchr(component,
'[');
1292 if ((q != (
char *) NULL) && ((q == component) || (*(q-1) !=
']')) &&
1293 (IsPathAccessible(path) == MagickFalse))
1299 if ((IsSceneGeometry(q+1,MagickFalse) == MagickFalse) &&
1300 (IsGeometry(q+1) == MagickFalse))
1304 subimage_length=(size_t) (p-q);
1305 subimage_offset=(size_t) (q-component+1);
1315 for (p=component; *p !=
'\0'; p++)
1317 if ((*p ==
'%') && (*(p+1) ==
'['))
1322 for (p++; (*p !=
']') && (*p !=
'\0'); p++) ;
1326 if ((p != component) && (*p ==
':') && (IsPathDirectory(component) < 0) &&
1327 (IsPathAccessible(component) == MagickFalse))
1333 if (IsMagickConflict(component) != MagickFalse)
1337 magick_length=(size_t) (p-component+1);
1338 for (q=component; *(++p) !=
'\0'; q++)
1347 for (p=component+strlen(component)-1; p > component; p--)
1348 if (IsBasenameSeparator(*p) != MagickFalse)
1354 if (magick_length != 0)
1355 (void) CopyMagickString(component,path,magick_length);
1362 if (*component !=
'\0')
1364 for (p=component+(strlen(component)-1); p > component; p--)
1366 if (IsBasenameSeparator(*p) != MagickFalse)
1384 if (IsBasenameSeparator(*p) != MagickFalse)
1385 (void) CopyMagickString(component,p+1,MagickPathExtent);
1390 if (IsBasenameSeparator(*p) != MagickFalse)
1391 (void) CopyMagickString(component,p+1,MagickPathExtent);
1392 if (*component !=
'\0')
1393 for (p=component+(strlen(component)-1); p > component; p--)
1401 case BasePathSansCompressExtension:
1404 extension[MagickPathExtent];
1409 GetPathComponent(path,ExtensionPath,extension);
1410 if ((LocaleCompare(extension,
"bz2") == 0) ||
1411 (LocaleCompare(extension,
"gz") == 0) ||
1412 (LocaleCompare(extension,
"svgz") == 0) ||
1413 (LocaleCompare(extension,
"wmz") == 0) ||
1414 (LocaleCompare(extension,
"Z") == 0))
1415 GetPathComponent(path,BasePath,component);
1420 if (IsBasenameSeparator(*p) != MagickFalse)
1421 (void) CopyMagickString(component,p+1,MagickPathExtent);
1422 if (*component !=
'\0')
1423 for (p=component+strlen(component)-1; p > component; p--)
1428 (void) CopyMagickString(component,p+1,MagickPathExtent);
1434 if ((subimage_length != 0) && (magick_length < subimage_offset))
1435 (void) CopyMagickString(component,path+subimage_offset,subimage_length);
1438 case SubcanonicalPath:
1470MagickExport
char **GetPathComponents(
const char *path,
1471 size_t *number_components)
1483 if (path == (
char *) NULL)
1484 return((
char **) NULL);
1485 *number_components=1;
1486 for (p=path; *p !=
'\0'; p++)
1487 if (IsBasenameSeparator(*p))
1488 (*number_components)++;
1489 components=(
char **) AcquireQuantumMemory((
size_t) *number_components+1UL,
1490 sizeof(*components));
1491 if (components == (
char **) NULL)
1492 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1494 for (i=0; i < (ssize_t) *number_components; i++)
1496 for (q=p; *q !=
'\0'; q++)
1497 if (IsBasenameSeparator(*q))
1499 components[i]=(
char *) AcquireQuantumMemory((
size_t) (q-p)+MaxTextExtent,
1500 sizeof(**components));
1501 if (components[i] == (
char *) NULL)
1502 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1503 (void) CopyMagickString(components[i],p,(
size_t) (q-p+1));
1506 components[i]=(
char *) NULL;
1533MagickExport MagickBooleanType IsPathAccessible(
const char *path)
1541 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1542 return(MagickFalse);
1543 if (LocaleCompare(path,
"-") == 0)
1545 status=GetPathAttributes(path,&attributes);
1546 if (status == MagickFalse)
1548 if (S_ISREG(attributes.st_mode) == 0)
1549 return(MagickFalse);
1550 if (access_utf8(path,F_OK) != 0)
1551 return(MagickFalse);
1578static int IsPathDirectory(
const char *path)
1586 if ((path == (
const char *) NULL) || (*path ==
'\0'))
1587 return(MagickFalse);
1588 status=GetPathAttributes(path,&attributes);
1589 if (status == MagickFalse)
1591 if (S_ISDIR(attributes.st_mode) == 0)
1631#if defined(__cplusplus) || defined(c_plusplus)
1635static int FileCompare(
const void *x,
const void *y)
1641 p=(
const char **) x;
1642 q=(
const char **) y;
1643 return(LocaleCompare(*p,*q));
1646#if defined(__cplusplus) || defined(c_plusplus)
1650MagickExport
char **ListFiles(
const char *directory,
const char *pattern,
1651 size_t *number_entries)
1669 assert(directory != (
const char *) NULL);
1670 assert(pattern != (
const char *) NULL);
1671 assert(number_entries != (
size_t *) NULL);
1672 if (IsEventLogging() != MagickFalse)
1673 (void) LogMagickEvent(TraceEvent,GetMagickModule(),
"%s",directory);
1675 current_directory=opendir(directory);
1676 if (current_directory == (DIR *) NULL)
1677 return((
char **) NULL);
1682 filelist=(
char **) AcquireQuantumMemory((
size_t) max_entries,
1684 if (filelist == (
char **) NULL)
1686 (void) closedir(current_directory);
1687 return((
char **) NULL);
1692 buffer=(
struct dirent *) AcquireMagickMemory(
sizeof(*buffer)+FILENAME_MAX+1);
1693 if (buffer == (
struct dirent *) NULL)
1694 ThrowFatalException(ResourceLimitFatalError,
"MemoryAllocationFailed");
1695 while ((MagickReadDirectory(current_directory,buffer,&entry) == 0) &&
1696 (entry != (
struct dirent *) NULL))
1698 if ((LocaleCompare(entry->d_name,
".") == 0) ||
1699 (LocaleCompare(entry->d_name,
"..") == 0))
1701 if ((IsPathDirectory(entry->d_name) > 0) ||
1702#
if defined(MAGICKCORE_WINDOWS_SUPPORT)
1703 (GlobExpression(entry->d_name,pattern,MagickTrue) != MagickFalse))
1705 (GlobExpression(entry->d_name,pattern,MagickFalse) != MagickFalse))
1708 if (*number_entries >= max_entries)
1714 filelist=(
char **) ResizeQuantumMemory(filelist,(
size_t)
1715 max_entries,
sizeof(*filelist));
1716 if (filelist == (
char **) NULL)
1724 p=strchr(entry->d_name,
';');
1727 if (*number_entries > 0)
1728 if (LocaleCompare(entry->d_name,filelist[*number_entries-1]) == 0)
1732 filelist[*number_entries]=(
char *) AcquireString(entry->d_name);
1733 (*number_entries)++;
1736 buffer=(
struct dirent *) RelinquishMagickMemory(buffer);
1737 (void) closedir(current_directory);
1738 if (filelist == (
char **) NULL)
1739 return((
char **) NULL);
1743 qsort((
void *) filelist,(
size_t) *number_entries,
sizeof(*filelist),
1772MagickExport
void MagickDelay(
const MagickSizeType milliseconds)
1774 if (milliseconds == 0)
1776#if defined(MAGICKCORE_HAVE_NANOSLEEP)
1781 timer.tv_sec=(time_t) (milliseconds/1000);
1782 timer.tv_nsec=(milliseconds % 1000)*1000*1000;
1783 (void) nanosleep(&timer,(
struct timespec *) NULL);
1785#elif defined(MAGICKCORE_HAVE_USLEEP)
1786 usleep(1000*milliseconds);
1787#elif defined(MAGICKCORE_HAVE_SELECT)
1792 timer.tv_sec=(long) milliseconds/1000;
1793 timer.tv_usec=(long) (milliseconds % 1000)*1000;
1794 (void) select(0,(XFD_SET *) NULL,(XFD_SET *) NULL,(XFD_SET *) NULL,&timer);
1796#elif defined(MAGICKCORE_HAVE_POLL)
1797 (void) poll((
struct pollfd *) NULL,0,(
int) milliseconds);
1798#elif defined(MAGICKCORE_WINDOWS_SUPPORT)
1799 Sleep((
long) milliseconds);
1805 timer=milliseconds/1000.0;
1808#elif defined(__BEOS__)
1809 snooze(1000*milliseconds);
1815 time_end=clock()+milliseconds*CLOCKS_PER_SEC/1000;
1816 while (clock() < time_end)
1847MagickExport
size_t MultilineCensus(
const char *label)
1855 if (label == (
char *) NULL)
1857 for (number_lines=1; *label !=
'\0'; label++)
1860 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);
1937 file=open_utf8(path,O_WRONLY | O_EXCL | O_BINARY,S_MODE);
1939 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((MagickSizeType) quantum,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);